OSDN Git Service

Merge branch 'v05dev' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v05
[pettanr/pettanr.git] / app / models / panel_picture.rb
1 class PanelPicture < ActiveRecord::Base
2   belongs_to :panel
3   belongs_to :picture
4   
5   validates :panel_id, :numericality => {:allow_blank => true}
6   validates :picture_id, :numericality => true, :existence => {:both => false}
7   validates :link, :length => {:maximum => 200}, :url => {:allow_blank => true, :message => I18n.t('errors.messages.url')}
8   validates :x, :presence => true, :numericality => true
9   validates :y, :presence => true, :numericality => true
10   validates :width, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :resize => true, :sync_vh => true
11   validates :height, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :resize => true, :sync_vh => true
12   validates :z, :presence => true, :numericality => {:greater_than => 0}
13   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
14   
15   before_validation :valid_encode
16   
17   def valid_encode
18     ['link', 'caption'].each do |a|
19       next if attributes[a] == nil
20       raise Pettanr::BadRequest unless attributes[a].valid_encoding?
21     end
22   end
23   
24   def visible? roles
25     if MagicNumber['run_mode'] == 0
26       return false unless guest_role_check(roles)
27     else
28       return false unless reader_role_check(roles)
29     end
30     return true if self.panel.own?(roles)
31     self.panel.visible? roles
32   end
33   
34   def flip
35     res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h')
36     res += '/' unless res.empty?
37     res
38   end
39   
40   def filename
41     self.flip + self.picture.filename
42   end
43   
44   def url
45     '/pictures/' + self.filename
46   end
47   
48   def opt_img_tag
49     {:src => self.url, :vPicture => self.id, :width => self.width.abs, :height => self.height.abs, :alt => self.caption, :style => "top:#{self.y}px; left:#{self.x}px; z-index:#{self.z}; "}
50   end
51   
52   def tmb_opt_img_tag
53     tw, th = PettanImager.thumbnail_size(self.width.abs, self.height.abs)
54     {:src => self.url, :width => tw, :height => th, :alt => self.caption}
55   end
56   
57   def self.default_page_size
58     25
59   end
60   
61   def self.max_page_size
62     100
63   end
64   
65   def self.page prm = nil
66     page = prm.to_i
67     page = 1 if page < 1
68     page
69   end
70   
71   def self.page_size prm = self.default_page_size
72     page_size = prm.to_i
73     page_size = self.max_page_size if page_size > self.max_page_size
74     page_size = self.default_page_size if page_size < 1
75     page_size
76   end
77   
78   def self.list page = 1, page_size = self.default_page_size
79     opt = {}
80     opt.merge!(PanelPicture.list_opt)
81     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
82     opt.merge!({:conditions => 'panels.publish > 0', :order => 'panel_pictures.updated_at desc'})
83     PanelPicture.find(:all, opt)
84   end
85   
86   def self.list_opt
87     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
88   end
89   
90   def self.list_json_opt
91     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
92   end
93   
94   def self.mylist au, page = 1, page_size = Author.default_panel_picture_page_size
95     opt = {}
96     opt.merge!(PanelPicture.list_opt)
97     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
98     opt.merge!({:conditions => ['panels.author_id = ?', au.id], :order => 'panel_pictures.updated_at desc'})
99     PanelPicture.find(:all, opt)
100   end
101   
102   def self.show cid, au
103     opt = {}
104     opt.merge!(PanelPicture.show_opt)
105     res = PanelPicture.find(cid, opt)
106     raise ActiveRecord::Forbidden unless res.visible?(au)
107     res
108   end
109   
110   def self.show_opt
111     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
112   end
113   
114   def self.show_json_opt
115     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
116   end
117   
118 end