OSDN Git Service

fix: fetch fail
[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   scope :with_panel, -> do
15     includes(:panel)
16   end
17   
18   scope :find_index, -> do
19     with_panel.where(Panel.arel_table[:publish].gt 0).references(:panel)
20   end
21   
22   scope :find_private, -> (operators) do 
23     with_panel.where(Panel.arel_table[:author_id].eq operators.author.id).references(:panel)
24   end
25   
26   scope :find_by_panel, -> (panel_id) do 
27     find_index.where(panel_id: panel_id).references(:panel)
28   end
29   
30   scope :find_by_author, -> (author_id) do 
31     find_index.where(Panel.arel_table[:author_id].eq author_id).references(:panel)
32   end
33   
34   @@repeat_texts = ['repeat', 'repeat-x', 'repeat-y', 'no-repeat']
35   
36   def self.pickup_item_name
37     Picture.item_name
38   end
39   
40   def self.pickup_column_name
41     self.pickup_item_name + '_id'
42   end
43   
44   def pickup_id
45     # get :picture_id if head revision
46     self.attributes[self.pickup_column_name]
47   end
48   
49   def y
50     self.attributes['y']
51   end
52   
53   def self.by_author_list_includes
54     {
55       :panel => {
56         :author => {}
57       }
58     }
59   end
60   
61   def self.has_picture?
62     true
63   end
64   
65   def supply_default
66     self.x = 0
67     self.y = 0
68     self.repeat = 0
69     if self.panel
70       self.t = self.panel.new_t 
71       self.z = self.panel.new_z 
72     end
73   end
74   
75   def overwrite pid
76     self.panel_id = pid
77   end
78   
79   def style spot = nil, opacity = 20
80     r = {
81       'position' => 'absolute', 'top' => '0px', 'left' => '0px', 'z-index' => self.z, 
82       'background-image' => "url(#{self.picture.url})", 
83       'background-repeat' => self.repeat_text, 
84       'background-position' => "#{self.x}px #{self.y}px"
85     }
86     self.merge_opacity(r, opacity) if spot and spot != self
87     r
88   end
89   
90   def tmb_opt_img_tag
91     tw, th = PettanImager.thumbnail_size(self.picture.width, self.picture.height)
92     {:src => self.picture.url, :width => tw, :height => th, :alt => self.caption}
93   end
94   
95   def symbol_option
96     self.tmb_opt_img_tag
97   end
98   
99   def repeat_text
100     @@repeat_texts[self.repeat]
101   end
102   
103   def self.public_list_where list
104     'panels.publish > 0'
105   end
106   
107   def self.show_opt
108     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
109   end
110   
111   def scenario
112     if caption.blank?
113       ''
114     else
115       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
116     end
117   end
118   
119   def plain_scenario
120     if caption.blank?
121       ''
122     else
123       self.caption + "\n"
124     end
125   end
126   
127 end