OSDN Git Service

v07
[pettanr/pettanr.git] / app / models / panel.rb
1 #コマ
2 class Panel < Peta::Root
3   load_manifest
4   belongs_to :author
5   has_many :scroll_panels
6   has_many :sheet_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
15
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
21   
22   scope :find_index, -> do
23     where(arel_table[:publish].gt 0)
24   end
25   
26   scope :find_private, -> (operators) do 
27     where(author_id: operators.author.id)
28   end
29   
30   scope :find_by_author, -> (author_id) do 
31     find_index.where(author_id: author_id)
32   end
33   
34   scope :with_scrolls, -> do
35     includes(scroll_panels: :scroll)
36   end
37   
38   scope :find_by_scroll, -> (scroll_id) do 
39     with_scrolls.find_index.where(Scroll.arel_table[:id].eq scroll_id).references(:scroll)
40   end
41   
42   scope :with_sheets, -> do
43     includes(sheet_panels: :sheet)
44   end
45   
46   scope :find_by_sheet, -> (sheet_id) do 
47     with_sheets.find_index.where(Sheet.arel_table[:id].eq sheet_id).references(:sheet)
48   end
49   
50   scope :with_speech_balloons, -> do
51     includes(:speech_balloons)
52   end
53   
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)
56   end
57   
58   def supply_default
59     self.border = 2
60     self.publish = 0
61   end
62   
63   def overwrite operators
64     return false unless operators.author
65     self.author_id = operators.author.id
66   end
67   
68   def visible? operators
69     case super
70     when nil # super return
71       return true
72     when false
73       return false
74     else
75       return true if self.new_record?
76       self.publish?
77     end
78   end
79   
80   def usable? operators
81     self.visible? operators
82   end
83   
84   def publish?
85     self.publish > 0
86   end
87   
88   def style
89     {
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'
93     }
94   end
95   
96   # ground_picture element template 
97   def style_wh
98     {
99       'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px'
100     }
101   end
102   
103   def self.public_list_where list
104     'panels.publish > 0'
105   end
106   
107   def self.show_opt
108     r = {
109       :author => {}
110     }
111     self.child_models.each do |child_model|
112       r.merge!(child_model.show_opt_for_panel)
113     end
114     {:include => r}
115   end
116   
117   def scenario
118     scenario_elements.map { |e|
119       e.scenario
120     }.join
121   end
122   
123   def plain_scenario
124     scenario_elements.map { |e|
125       e.plain_scenario
126     }.join
127   end
128   
129   def self.licensed_pictures panels
130     r = {}
131     panels.each do |panel|
132       r.merge!(panel.licensed_pictures)
133     end
134     r
135   end
136   
137   def licensed_pictures
138     r = {}
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]
142     end
143     r
144   end
145   
146 end