OSDN Git Service

test speech balloon extended
[pettanr/pettanr.git] / app / models / ground_picture.rb
1 class GroundPicture < Peta::Element
2   load_manifest
3   belongs_to :panel
4   belongs_to :picture
5   
6   validates :panel_id, :numericality => {:allow_blank => true}
7   validates :repeat, :numericality => true, :inclusion => { :in => 0..3 }
8   validates :x, :numericality => true
9   validates :y, :numericality => true
10   validates :picture_id, :numericality => true, :existence => {:both => false}
11   validates :z, :presence => true, :numericality => {:greater_than => 0}
12   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
13   
14   @@repeat_texts = ['repeat', 'repeat-x', 'repeat-y', 'no-repeat']
15   
16   def self.by_author_list_includes
17     {
18       :panel => {
19         :author => {}
20       }
21     }
22   end
23   
24   def self.list_opt_for_panel
25     {
26       :ground_pictures => {
27         :picture => {:artist => {}, :license => {}}
28       }
29     }
30   end
31   
32   def self.show_opt_for_panel
33     {
34       :ground_pictures => {
35         :picture => {:artist => {}, :license => {}}
36       }
37     }
38   end
39   
40   def self.json_opt_for_panel
41     {
42       :picture => {:artist => {}, :license => {}}
43     }
44   end
45   
46   def self.has_picture?
47     true
48   end
49   
50   def supply_default
51     self.x = 0
52     self.y = 0
53     self.repeat = 0
54     if self.panel
55       self.t = self.panel.new_t 
56       self.z = self.panel.new_z 
57     end
58   end
59   
60   def overwrite pid
61     self.panel_id = pid
62   end
63   
64   def style spot = nil, opacity = 20
65     r = {
66       'position' => 'absolute', 'top' => '0px', 'left' => '0px', 'z-index' => self.z, 
67       'background-image' => "url(#{self.picture.url})", 
68       'background-repeat' => self.repeat_text, 
69       'background-position' => "#{self.x}px #{self.y}px"
70     }
71     self.merge_opacity(r, opacity) if spot and spot != self
72     r
73   end
74   
75   def tmb_opt_img_tag
76     tw, th = PettanImager.thumbnail_size(self.picture.width, self.picture.height)
77     {:src => self.picture.url, :width => tw, :height => th, :alt => self.caption}
78   end
79   
80   def symbol_option
81     self.tmb_opt_img_tag
82   end
83   
84   def repeat_text
85     @@repeat_texts[self.repeat]
86   end
87   
88   def self.public_list_where
89     'panels.publish > 0'
90   end
91   
92   def self.list_order
93     'ground_pictures.updated_at desc'
94   end
95   
96   def self.list_opt
97     {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
98   end
99   
100   def self.list_json_opt
101     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
102   end
103   
104   def self.show_opt
105     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
106   end
107   
108   def self.show_json_opt
109     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
110   end
111   
112   def store operators
113     if self.new_record?
114       self.panel.ground_pictures.build(self.attributes)
115     else
116       self.panel.ground_pictures.each do |ground_picture|
117         next unless ground_picture == self
118         attr = self.attributes
119         attr.delete 'id'
120         ground_picture.attributes = attr
121         break
122       end
123     end
124     self.panel.store({}, operators)
125   end
126   
127   def remove operators
128     self.panel.remove_element(self, operators)
129   end
130   
131   def scenario
132     if caption.blank?
133       ''
134     else
135       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
136     end
137   end
138   
139   def plain_scenario
140     if caption.blank?
141       ''
142     else
143       self.caption + "\n"
144     end
145   end
146   
147 end