OSDN Git Service

temp
[pettanr/pettanr.git] / app / models / panel.rb
index 71dabcb..d2bfdf7 100644 (file)
@@ -1,8 +1,8 @@
 #コマ
 class Panel < ActiveRecord::Base
   belongs_to :author
-  belongs_to :resource_picture
   has_many :scroll_panels
+  has_many :sheet_panels
   has_many :panel_pictures, :dependent => :destroy
   has_many :speech_balloons, :dependent => :destroy
   has_many :ground_pictures, :dependent => :destroy
@@ -15,9 +15,6 @@ class Panel < ActiveRecord::Base
   validates :width, :presence => true, :numericality => true, :natural_number => true
   validates :height, :presence => true, :numericality => true, :natural_number => true
   validates :border, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
-  validates :x, :numericality => {:allow_blank => true}
-  validates :y, :numericality => {:allow_blank => true}
-  validates :z, :numericality => {:allow_blank => true, :greater_than => 0}
   validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :publish, :presence => true, :numericality => true
   
@@ -31,7 +28,7 @@ class Panel < ActiveRecord::Base
   end
   
   def self.each_element_class_names
-    Pettanr::Application.elements.each do |k, n|
+    Pettanr::Application.manifest.system_resources.elements.each do |k, n|
       yield k
     end
   end
@@ -74,6 +71,7 @@ class Panel < ActiveRecord::Base
       return false unless reader_role_check(roles)
     end
     return true if self.own?(roles)
+    return true if self.new_record?
     self.publish?
   end
   
@@ -85,8 +83,8 @@ class Panel < ActiveRecord::Base
     self.publish > 0
   end
   
-  def tag_id
-    'panel' + self.tag_panel_id
+  def tag_id c = nil
+    'panel' + self.tag_panel_id + c.to_s
   end
   
   def tag_panel_id
@@ -97,6 +95,42 @@ class Panel < ActiveRecord::Base
     self.tag_id + f.to_s
   end
   
+  def tag_attributes column = nil, opt = {}
+    {
+      :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
@@ -154,6 +188,32 @@ class Panel < ActiveRecord::Base
     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 => {}