OSDN Git Service

classname rename to module_name
[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.has_picture?
23     false
24   end
25   
26   def supply_default
27     self.code ||= 0
28     if self.panel
29       self.t = self.panel.new_t 
30       self.z = self.panel.new_z 
31     end
32   end
33   
34   def overwrite pid
35     self.panel_id = pid
36   end
37   
38   def div_offset
39     xy ? xy : 0
40   end
41   
42   def div_size
43     wh ? wh : 100 - self.div_offset
44   end
45   
46   def div_x
47     if self.orientation == 0
48       0
49     else
50       self.div_offset
51     end
52   end
53   
54   def div_y
55     if self.orientation == 0
56       self.div_offset
57     else
58       0
59     end
60   end
61   
62   def div_width
63     if self.orientation == 0
64       100
65     else
66       self.div_size
67     end
68   end
69   
70   def div_height 
71     if self.orientation == 0
72       self.div_size
73     else
74       100
75     end
76   end
77   
78   def style spot = nil, opacity = 20
79     r = {
80       'position' => 'absolute', 'z-index' => self.z, 
81       'top' => self.div_y.to_s + '%', 'left' => self.div_x.to_s + '%', 
82       'width' => self.div_width.to_s + '%', 'height' => self.div_height.to_s + '%', 
83       'background-color' => '#' + format("%06x", self.code)
84     }
85     self.merge_opacity(r, opacity) if spot and spot != self
86     r
87   end
88   
89   def self.public_list_where
90     'panels.publish > 0'
91   end
92   
93   def self.show_opt
94     {:include => {:panel => {:author => {}} }}
95   end
96   
97   def scenario
98     if caption.blank?
99       ''
100     else
101       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
102     end
103   end
104   
105   def plain_scenario
106     if caption.blank?
107       ''
108     else
109       self.caption + "\n"
110     end
111   end
112   
113 end