1 class SheetPanel < Peta::Element
5 accepts_nested_attributes_for :panel, :allow_destroy => true
6 #alias_attribute :attr_y, :y
8 validates :sheet_id, :numericality => {:allow_blank => true}
9 validates :panel_id, :numericality => {:allow_blank => true}
10 validates :x, :presence => true, :numericality => true
11 validates :y, :presence => true, :numericality => true
12 validates :z, :presence => true, :numericality => {:greater_than => 0}
13 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
15 scope :with_sheet, -> do
19 scope :find_index, -> do
20 with_sheet.where(Sheet.arel_table[:visible].gt 0).references(:sheet)
23 scope :find_private, -> (operators) do
24 with_sheet.where(Sheet.arel_table[:author_id].eq operators.author.id).references(:sheet)
27 scope :by_sheet, -> (sheet_id) do
28 where(sheet_id: sheet_id)
31 scope :find_by_sheet, -> (sheet_id) do
32 find_index.by_sheet(sheet_id).references(:sheet)
35 scope :find_by_panel, -> (panel_id) do
36 find_index.where(panel_id: panel_id).references(:sheet)
39 scope :find_by_author, -> (author_id) do
40 find_index.where(Sheet.arel_table[:author_id].eq author_id).references(:sheet)
43 scope :find_play, -> (sheet_id) do
44 find_by_sheet(sheet_id)
47 scope :find_private_play, -> (sheet_id, operators) do
48 find_private(operators).by_sheet(sheet_id)
59 self.t = self.sheet.new_t
60 self.z = self.sheet.new_z
72 def self.by_author_list_includes
87 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
88 :speech_balloons =>{:balloon => {}, :speech => {}}
97 def sheet_panel_as_json au
98 panel_include = if self.panel and self.panel.visible?(au)
99 {:include => {}, :methods => :elements}
103 self.to_json({:include => {:sheet => {:include => {:author => {}}}, :panel => panel_include}})
106 def self.list_as_json_text ary, au
107 '[' + ary.map {|i| i.sheet_panel_as_json(au) }.join(',') + ']'
110 def licensed_pictures
112 self.panel.licensed_pictures
119 if self.panel and self.panel.publish?
127 return nil if self.sheet_id == nil or self.panel_id == nil
128 self.sheet.own?(operators) and self.panel.usable?(operators)
131 def store operators, old_t = nil
133 self.class.transaction do
134 case self.allow? operators
138 raise ActiveRecord::Forbidden
142 raise ActiveRecord::Rollback unless res
143 res = self.class.validate_t(self.sheet_id)
145 self.errors.add :t, 'unserialized'
146 raise ActiveRecord::Rollback