2 class Panel < Peta::Root
5 has_many :scroll_panels
7 has_many :panel_pictures, :dependent => :destroy
8 has_many :speech_balloons, :dependent => :destroy
9 has_many :ground_pictures, :dependent => :destroy
10 has_many :ground_colors, :dependent => :destroy
11 accepts_nested_attributes_for :panel_pictures, :allow_destroy => true
12 accepts_nested_attributes_for :speech_balloons, :allow_destroy => true
13 accepts_nested_attributes_for :ground_pictures, :allow_destroy => true
14 accepts_nested_attributes_for :ground_colors, :allow_destroy => true
16 validates :width, :presence => true, :numericality => true, :natural_number => true
17 validates :height, :presence => true, :numericality => true, :natural_number => true
18 validates :border, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
19 validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
20 validates :publish, :presence => true, :numericality => true
22 scope :find_index, -> do
23 where(arel_table[:publish].gt 0)
26 scope :find_private, -> (operators) do
27 where(author_id: operators.author.id)
30 scope :find_by_author, -> (author_id) do
31 find_index.where(author_id: author_id)
34 scope :with_scrolls, -> do
35 includes(scroll_panels: :scroll)
38 scope :find_by_scroll, -> (scroll_id) do
39 with_scrolls.find_index.where(Scroll.arel_table[:id].eq scroll_id).references(:scroll)
42 scope :with_sheets, -> do
43 includes(sheet_panels: :sheet)
46 scope :find_by_sheet, -> (sheet_id) do
47 with_sheets.find_index.where(Sheet.arel_table[:id].eq sheet_id).references(:sheet)
50 scope :with_speech_balloons, -> do
51 includes(:speech_balloons)
54 scope :find_by_speech_balloon_template, -> (speech_balloon_template_id) do
55 with_speech_balloons.find_index.where(SpeechBalloon.arel_table[:speech_balloon_template_id].eq speech_balloon_template_id).references(:speech_balloon)
63 def overwrite operators
64 return false unless operators.author
65 self.author_id = operators.author.id
68 def visible? operators
70 when nil # super return
75 return true if self.new_record?
81 self.visible? operators
90 'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px',
91 'border-style' => 'solid', 'border-width' => self.border.to_s + 'px',
92 'border-color' => 'black', 'background-color' => 'white'
96 # ground_picture element template
99 'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px'
103 def self.public_list_where list
111 self.child_models.each do |child_model|
112 r.merge!(child_model.show_opt_for_panel)
118 scenario_elements.map { |e|
124 scenario_elements.map { |e|
129 def self.licensed_pictures panels
131 panels.each do |panel|
132 r.merge!(panel.licensed_pictures)
137 def licensed_pictures
139 self.scenario_elements.each do |elm|
140 next unless elm.class.has_picture?
141 r[elm.picture_id] = elm.picture unless r[elm.picture_id]