OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / app / models / panel.rb
index 33a99b6..bbdf9ed 100644 (file)
@@ -1,5 +1,6 @@
 #コマ
-class Panel < Pettanr::Item
+class Panel < Peta::Root
+  load_manifest
   belongs_to :author
   has_many :scroll_panels
   has_many :sheet_panels
@@ -18,255 +19,81 @@ class Panel < Pettanr::Item
   validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :publish, :presence => true, :numericality => true
   
-  def self.singular
-    'Panel'
-  end
-  
-  def self.plural
-    'Panels'
-  end
-  
-  def self.owner_type
-    :author
-  end
-  
-  def self.valid_encode_columns
-    super.merge ['caption']
-  end
-  
-  def self.each_element_class_names
-    Pettanr::Application.manifest.system_resources.elements.each do |k, n|
-      yield k
-    end
-  end
-  
-  def self.class_name_to_class k
-    Object.const_get k
-  end
-  
-  def self.each_element_classes
-    self.each_element_class_names do |k|
-      e = self.class_name_to_class k
-      yield e
-    end
-  end
-  
-  def elements_by_class_name class_name
-    self.__send__ class_name.tableize
-  end
-  
   def supply_default
     self.border = 2
     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)
+  def visible? operators
+    case super
+    when nil # super return
+      return true
+    when false
+      return false
     else
-      return false unless reader_role_check(roles)
+      return true if self.new_record?
+      self.publish?
     end
-    return true if self.own?(roles)
-    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
   
-  def tag_id c = nil
-    'panel' + self.tag_panel_id + c.to_s
-  end
-  
-  def tag_panel_id
-    self.new_record? ? '0' : self.id.to_s
-  end
-  
-  def field_tag_id f
-    self.tag_id + f.to_s
-  end
-  
-  def tag_attributes column = nil, opt = {}
+  def style
     {
-      :id => self.field_tag_id(column), :panel_id => self.tag_panel_id
-    }.merge(opt)
-  end
-  
-  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 = {}
-    self.tag_attributes(column).merge(
-      {:column => column}
-    ).merge(opt)
-  end
-  
-  def tag_attr column = nil, opt = {}
-    self.tag_attributes(column, opt).to_attr
-  end
-  
-  def field_tag_attr column, no_attr, opt = {}
-    self.field_tag_attributes(column, no_attr, opt).to_attr
-  end
-  
-    def render_count
-      @render_count ||= 1
-    end
-    
-    def rendered
-      @render_count = render_count + 1
-    end
-    
-  def self.default_page_size
-    25
-  end
-  
-  def self.max_page_size
-    100
+      'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px', 
+      'border-style' => 'solid', 'border-width' => self.border.to_s + 'px', 
+      'border-color' => 'black', 'background-color' => 'white'
+    }
   end
   
-  def self.page prm = nil
-    page = prm.to_i
-    page = 1 if page < 1
-    page
+  # ground_picture element template 
+  def style_wh
+    {
+      'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px'
+    }
   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
+  def self.public_list_where
+    'panels.publish > 0'
   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)
-  end
-  
   def self.list_opt
     r = {
       :author => {}
     }
-    self.each_element_classes do |e|
-      r.merge!(e.list_opt_for_panel)
+    self.child_models.each do |child_model|
+      r.merge!(child_model.list_opt_for_panel)
     end
     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 => {}
     }
-    self.each_element_classes do |e|
-      r.merge!(e.show_opt_for_panel)
+    self.child_models.each do |child_model|
+      r.merge!(child_model.show_opt_for_panel)
     end
     {:include => r}
   end
   
   def parts_element
-    r = []
-    self.class.each_element_class_names do |k|
-      r += (self.elements_by_class_name(k) || [])
-    end
-    r
+    self.elements_items
   end
   
   def zorderd_elements
@@ -335,14 +162,12 @@ class Panel < Pettanr::Item
   
   def self.collect_element_value elements, name
     elements.map {|e|
-      e.map {|o|
-        if o['_destroy'] or o[:_destroy]
-          nil
-        else
-          o[name]
-        end
-      }.compact
-    }.flatten
+      if e['_destroy'] or e[:_destroy]
+        nil
+      else
+        e[name]
+      end
+    }
   end
   
   def self.validate_serial ary, offset = 0
@@ -365,13 +190,9 @@ class Panel < Pettanr::Item
   end
   
   def validate_serial_list
-    l = []
-    self.class.each_element_class_names do |k|
-      l << self.elements_by_class_name(k)
-    end
     [
-      {:elements => l, :name => :t, :offset => 0}, 
-      {:elements => l, :name => :z, :offset => 1}
+      {:elements => self.elements_items, :name => :t, :offset => 0}, 
+      {:elements => self.elements_items, :name => :z, :offset => 1}
     ]
   end
   def validate_child
@@ -379,29 +200,16 @@ class Panel < Pettanr::Item
     Panel.validate_elements_serial validate_serial_list
   end
   
-  def boost
-    @new_element_index = 0
-    self.panel_elements.each do |elm|
-      if elm.new_record?
-        elm.new_index = @new_element_index
-        @new_element_index += 1
-      end
-    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|
-        elm.new_panel = self
-        elm.boost
-      end
+      self.boosts 'post'
 #self.publish = nil
       res = self.save
       unless validate_child
@@ -413,7 +221,7 @@ class Panel < Pettanr::Item
     res
   end
   
-  def remove_element target, au
+  def remove_element target, operators
     ct = target.t
     cz = target.z
     panel_attributes = {}
@@ -431,7 +239,7 @@ class Panel < Pettanr::Item
       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
@@ -446,23 +254,6 @@ class Panel < Pettanr::Item
     res
   end
   
-  def copy
-    attr = self.copy_attributes
-    Panel.each_element_class_names do |n|
-      attr.merge! Panel.class_name_to_class(n).panelize(self.elements_by_class_name(n).map {|elm|  elm.copy_attributes})
-    end
-    attr
-  end
-  
-  def copy_attributes
-    r = self.attributes
-    r.delete 'id'
-    r.delete 'author_id'
-    r.delete 'created_at'
-    r.delete 'updated_at'
-    r
-  end
-  
   def self.panelize panel
     attr = panel.attributes
     attr.delete 'id'