OSDN Git Service

classname rename to module_name
[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   def supply_default
23     self.border = 2
24     self.publish = 0
25   end
26   
27   def overwrite operators
28     return false unless operators.author
29     self.author_id = operators.author.id
30   end
31   
32   def visible? operators
33     case super
34     when nil # super return
35       return true
36     when false
37       return false
38     else
39       return true if self.new_record?
40       self.publish?
41     end
42   end
43   
44   def usable? operators
45     self.visible? operators
46   end
47   
48   def publish?
49     self.publish > 0
50   end
51   
52   def style
53     {
54       'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px', 
55       'border-style' => 'solid', 'border-width' => self.border.to_s + 'px', 
56       'border-color' => 'black', 'background-color' => 'white'
57     }
58   end
59   
60   # ground_picture element template 
61   def style_wh
62     {
63       'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px'
64     }
65   end
66   
67   def self.public_list_where
68     'panels.publish > 0'
69   end
70   
71   def self.show_opt
72     r = {
73       :author => {}
74     }
75     self.child_models.each do |child_model|
76       r.merge!(child_model.show_opt_for_panel)
77     end
78     {:include => r}
79   end
80   
81   def scenario
82     panel_elements.map { |e|
83       e.scenario
84     }.join
85   end
86   
87   def plain_scenario
88     panel_elements.map { |e|
89       e.plain_scenario
90     }.join
91   end
92   
93   def self.licensed_pictures panels
94     r = {}
95     panels.each do |panel|
96       r.merge!(panel.licensed_pictures)
97     end
98     r
99   end
100   
101   def licensed_pictures
102     r = {}
103     self.scenario_elements.each do |elm|
104       next unless elm.class.has_picture?
105       r[elm.picture_id] = elm.picture unless r[elm.picture_id]
106     end
107     r
108   end
109   
110 end