OSDN Git Service

e
[pettanr/pettanr.git] / app / models / panel.rb
index 7856a9b..1c81e69 100644 (file)
@@ -1,5 +1,6 @@
 #コマ
-class Panel < ActiveRecord::Base
+class Panel < Peta::Content
+  load_manifest
   belongs_to :author
   has_many :scroll_panels
   has_many :sheet_panels
@@ -18,36 +19,12 @@ class Panel < ActiveRecord::Base
   validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :publish, :presence => true, :numericality => true
   
-  cattr_reader :singular, :plural
-  @@singular = 'Panel'
-  @@plural = 'Panels'
-  def self.item_name
-    self.singular.underscore
-  end
-  
-  def item_name
-    self.class.item_name
-  end
-  
-  def self.path_name with_engine = false
-    self.plural.underscore
-  end
-  
-  def path_name 
-    self.class.path_name
-  end
-  
-  before_validation :valid_encode
-  
-  def valid_encode
-    ['caption'].each do |a|
-      next if attributes[a] == nil
-      raise Pettanr::BadRequest unless attributes[a].valid_encoding?
-    end
+  def self.valid_encode_columns
+    super + ['caption']
   end
   
   def self.each_element_class_names
-    Pettanr::Application.manifest.system_resources.elements.each do |k, n|
+    Manifest.manifest.system_resources.elements.each do |k, n|
       yield k
     end
   end
@@ -72,36 +49,32 @@ class Panel < ActiveRecord::Base
     self.publish = 0
   end
   
-  def overwrite au
-    self.author_id = au.id
-  end
-  
-  def own? roles
-    roles = [roles] unless roles.respond_to?(:each)
-    au = Panel.get_author_from_roles roles
-    return false unless au
-    self.author_id == au.id
+  def overwrite operators
+    return false unless operators.author
+    self.author_id = operators.author.id
   end
   
-  def visible? roles
-    if MagicNumber['run_mode'] == 0
-      return false unless guest_role_check(roles)
-    else
-      return false unless reader_role_check(roles)
-    end
-    return true if self.own?(roles)
+  def visible? operators
+    return true if super
     return true if self.new_record?
     self.publish?
   end
   
-  def usable? au
-    visible? au
+  def usable? operators
+    self.visible? operators
   end
   
   def publish?
     self.publish > 0
   end
   
+  # ground_picture element template 
+  def style_wh
+    {
+      'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px'
+    }
+  end
+  
   def tag_id c = nil
     'panel' + self.tag_panel_id + c.to_s
   end
@@ -122,13 +95,12 @@ class Panel < ActiveRecord::Base
   
   def select_tag_attributes(selected, column, opt = {})
     [
-      :last, :first, 
       {:html => {:selected => selected}}, 
       self.field_tag_attributes(column, opt)
     ]
   end
   
-  def field_tag_attributes column, opt = {}
+  def field_tag_attributes column, no_attr, opt = {}
     self.tag_attributes(column).merge(
       {:column => column}
     ).merge(opt)
@@ -150,91 +122,12 @@ class Panel < ActiveRecord::Base
       @render_count = render_count + 1
     end
     
-  def self.default_page_size
-    25
-  end
-  
-  def self.max_page_size
-    100
-  end
-  
-  def self.page prm = nil
-    page = prm.to_i
-    page = 1 if page < 1
-    page
-  end
-  
-  def self.page_size prm = self.default_page_size
-    page_size = prm.to_i
-    page_size = self.max_page_size if page_size > self.max_page_size
-    page_size = self.default_page_size if page_size < 1
-    page_size
-  end
-  
-  def self.list_order
-    'panels.updated_at desc'
-  end
-  
   def self.list_where
     'panels.publish > 0'
   end
   
-  def self.mylist_where au
-    ['panels.author_id = ?', au.id]
-  end
-  
-  def self.himlist_where au
-    ['panels.author_id = ? and panels.publish > 0', au.id]
-  end
-  
-  def self.list page = 1, page_size = self.default_page_size
-    Panel.where(self.list_where()).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
-  end
-  
-  def self.mylist au, page = 1, page_size = Author.default_panel_page_size
-    Panel.where(self.mylist_where(au)).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
-  end
-  
-  def self.himlist au, page = 1, page_size = Author.default_panel_page_size
-    Panel.where(self.himlist_where(au)).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
-  end
-  
-  def self.list_paginate page = 1, page_size = self.default_page_size
-    Kaminari.paginate_array(Array.new(Panel.where(self.list_where()).count, nil)).page(page).per(page_size)
-  end
-  
-  def self.mylist_paginate au, page = 1, page_size = Author.default_panel_page_size
-    Kaminari.paginate_array(Array.new(Panel.where(self.mylist_where(au)).count, nil)).page(page).per(page_size)
-  end
-  
-  def self.himlist_paginate au, page = 1, page_size = Author.default_panel_page_size
-    Kaminari.paginate_array(Array.new(Panel.where(self.himlist_where(au)).count, nil)).page(page).per(page_size)
-  end
-  
-  def self.list_by_scroll scroll_id, roles, page = 1, page_size = self.default_page_size
-    self.where(ScrollPanel.list_by_scroll_where(scroll_id)).includes(
-      {:scroll_panels => {}}
-    ).order('scroll_panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
-  end
-  
-  def self.list_by_sheet sheet_id, roles, page = 1, page_size = self.default_page_size
-    self.where(SheetPanel.list_by_sheet_where(sheet_id)).includes(
-      {:sheet_panels => {}}
-    ).order('sheet_panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
-  end
-  
-  def self.list_by_speech_balloon_template speech_balloon_template_id, roles, page = 1, page_size = self.default_page_size
-    self.where(SpeechBalloon.list_by_speech_balloon_template_where(speech_balloon_template_id)).includes(
-      {:speech_balloons => {}}
-    ).order('speech_balloons.updated_at desc').offset((page -1) * page_size).limit(page_size)
-  end
-  
-  def self.list_by_author_where author_id
-    ['panels.author_id = ?', author_id]
-  end
-  
-  def self.list_by_author author_id, roles, page = 1, page_size = self.default_page_size
-    self.where(self.list_by_author_where(author_id)).includes(self.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  def self.list_order
+    'panels.updated_at desc'
   end
   
   def self.list_opt
@@ -247,22 +140,6 @@ class Panel < ActiveRecord::Base
     r
   end
   
-  def self.show rid, roles
-    opt = {}
-    opt.merge!(Panel.show_opt)
-    res = Panel.find(rid, opt)
-    raise ActiveRecord::Forbidden unless res.visible?(roles)
-    res
-  end
-  
-  def self.edit rid, au
-    opt = {}
-    opt.merge!(Panel.show_opt)
-    res = Panel.find(rid, opt)
-    raise ActiveRecord::Forbidden unless res.own?(au)
-    res
-  end
-  
   def self.show_opt
     r = {
       :author => {}
@@ -401,13 +278,13 @@ class Panel < ActiveRecord::Base
     end
   end 
   
-  def store attr, au
+  def store attr, operators
     if attr == false
       self.errors.add :base, I18n.t('errors.invalid_json')
       return false
     end
     self.attributes = attr
-    self.overwrite au
+    self.overwrite operators
     res = false
     Panel.transaction do
       self.panel_elements.each do |elm|
@@ -425,7 +302,7 @@ class Panel < ActiveRecord::Base
     res
   end
   
-  def remove_element target, au
+  def remove_element target, operators
     ct = target.t
     cz = target.z
     panel_attributes = {}
@@ -443,7 +320,7 @@ class Panel < ActiveRecord::Base
       panel_attributes[elm.class.to_s.tableize + '_attributes'] ||= {}
       panel_attributes[elm.class.to_s.tableize + '_attributes'][elm.id] = attr
     end
-    self.store(panel_attributes, au)
+    self.store(panel_attributes, operators)
   end
   
   def destroy_with_elements