OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / app / models / ground_color.rb
1 class GroundColor < Peta::Element
2   load_manifest
3   belongs_to :panel
4   belongs_to :color
5   
6   validates :panel_id, :numericality => {:allow_blank => true}
7   validates :code, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 0x1000000}
8   validates :orientation, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
9   validates :xy, :numericality => {:greater_than_or_equal_to => 0, :allow_blank => true}
10   validates :wh, :numericality => {:greater_than_or_equal_to => 0, :allow_blank => true}
11   validates :z, :presence => true, :numericality => {:greater_than => 0}
12   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
13   
14   def self.by_author_list_includes
15     {
16       :panel => {
17         :author => {}
18       }
19     }
20   end
21   
22   def self.list_opt_for_panel
23     {
24       :ground_colors => {
25       }
26     }
27   end
28   
29   def self.show_opt_for_panel
30     {
31       :ground_colors => {
32       }
33     }
34   end
35   
36   def self.json_opt_for_panel
37     {
38     }
39   end
40   
41   def self.has_picture?
42     false
43   end
44   
45   def supply_default
46     self.code ||= 0
47     if self.panel
48       self.t = self.panel.new_t 
49       self.z = self.panel.new_z 
50     end
51   end
52   
53   def overwrite pid
54     self.panel_id = pid
55   end
56   
57   def div_offset
58     xy ? xy : 0
59   end
60   
61   def div_size
62     wh ? wh : 100 - self.div_offset
63   end
64   
65   def div_x
66     if self.orientation == 0
67       0
68     else
69       self.div_offset
70     end
71   end
72   
73   def div_y
74     if self.orientation == 0
75       self.div_offset
76     else
77       0
78     end
79   end
80   
81   def div_width
82     if self.orientation == 0
83       100
84     else
85       self.div_size
86     end
87   end
88   
89   def div_height 
90     if self.orientation == 0
91       self.div_size
92     else
93       100
94     end
95   end
96   
97   def style spot = nil, opacity = 20
98     r = {
99       'position' => 'absolute', 'z-index' => self.z, 
100       'top' => self.div_y.to_s + '%', 'left' => self.div_x.to_s + '%', 
101       'width' => self.div_width.to_s + '%', 'height' => self.div_height.to_s + '%', 
102       'background-color' => '#' + format("%06x", self.code)
103     }
104     self.merge_opacity(r, opacity) if spot and spot != self
105     r
106   end
107   
108   def self.public_list_where
109     'panels.publish > 0'
110   end
111   
112   def self.list_order
113     'ground_colors.updated_at desc'
114   end
115   
116   def self.list_opt
117     {:panel => {:author => {}} }
118   end
119   
120   def self.list_json_opt
121     {:include => {:panel => {:include => {:author => {}}} }}
122   end
123   
124   def self.show_opt
125     {:include => {:panel => {:author => {}} }}
126   end
127   
128   def self.show_json_opt
129     {:include => {:panel => {:include => {:author => {}}} }}
130   end
131   
132   def store operators
133     if self.new_record?
134       self.panel.ground_colors.build(self.attributes)
135     else
136       self.panel.ground_colors.each do |ground_color|
137         next unless ground_color == self
138         attr = self.attributes
139         attr.delete 'id'
140         ground_color.attributes = attr
141         break
142       end
143     end
144     self.panel.store({}, operators)
145   end
146   
147   def remove operators
148     self.panel.remove_element(self, operators)
149   end
150   
151   def scenario
152     if caption.blank?
153       ''
154     else
155       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
156     end
157   end
158   
159   def plain_scenario
160     if caption.blank?
161       ''
162     else
163       self.caption + "\n"
164     end
165   end
166   
167 end