OSDN Git Service

t#30102:update i18n op rp au ar gc gp pc
[pettanr/pettanr.git] / app / models / original_picture.rb
1 class OriginalPicture < ActiveRecord::Base
2   belongs_to :artist
3   belongs_to :original_picture_license_group
4   has_one :resource_picture
5   has_many :pictures
6   
7   validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
8   validates :width, :presence => true, :numericality => true, :natural_number => true
9   validates :height, :presence => true, :numericality => true, :natural_number => true
10   validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
11   validates :artist_id, :presence => true, :numericality => true, :existence => true
12   validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
13   
14   before_destroy :destroy_with_file
15   
16   def supply_default
17   end
18   
19   def overwrite ar
20     self.artist_id = ar.id
21   end
22   
23   def own? ar
24     if ar.is_a?(Author)
25       self.artist_id == ar.artist.id
26     elsif ar.is_a?(Artist)
27       self.artist_id == ar.id
28     else
29       false
30     end
31   end
32   
33   def visible? ar
34     return true if ar.is_a?(Admin)
35     self.own?(ar)
36   end
37   
38   def filename
39     "#{self.id}.#{self.ext}"
40   end
41   
42   def mime_type
43     "image/#{self.ext}"
44   end
45   
46   def url
47     '/original_pictures/' + filename
48   end
49   
50   def opt_img_tag
51     {:src => self.url, :width => self.width, :height => self.height}
52   end
53   
54   def tmb_opt_img_tag
55     tw, th = PettanImager.thumbnail_size(self.width, self.height)
56     {:src => self.url, :width => tw, :height => th}
57   end
58   
59   def self.default_page_size
60     25
61   end
62   
63   def self.max_page_size
64     100
65   end
66   
67   def self.page prm = nil
68     page = prm.to_i
69     page = 1 if page < 1
70     page
71   end
72   
73   def self.page_size prm = self.default_page_size
74     page_size = prm.to_i
75     page_size = self.max_page_size if page_size > self.max_page_size
76     page_size = self.default_page_size if page_size < 1
77     page_size
78   end
79   
80   def self.mylist artist_id, page = 1, page_size = self.default_page_size
81     opt = {}
82     opt.merge!(self.list_opt)
83     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
84     opt.merge!({:conditions => ['original_pictures.artist_id = ?', artist_id], :order => 'original_pictures.updated_at desc'})
85     OriginalPicture.find(:all, opt)
86   end
87   
88   def history 
89     Picture.find(:all, {:conditions => ['pictures.original_picture_id = ?', self.id], :order => 'pictures.revision desc'} )
90   end
91   
92   def self.list_opt
93     {:include => {:resource_picture => {}, :pictures => {}}}
94   end
95   
96   def self.list_json_opt
97     {:include => {:resource_picture => {}, :pictures => {}}}
98   end
99   
100   def self.show cid, ar
101     opt = {}
102     opt.merge!(self.show_opt)
103     res = OriginalPicture.find(cid, opt)
104     raise ActiveRecord::Forbidden unless res.visible?(ar)
105     res
106   end
107   
108   def self.edit cid, ar
109     opt = {}
110     opt.merge!(self.show_opt)
111     res = OriginalPicture.find(cid, opt)
112     raise ActiveRecord::Forbidden unless res.own?(ar)
113     res
114   end
115   
116   def self.show_opt
117     {:include => {:resource_picture => {}, :pictures => {}}}
118   end
119   
120   def self.show_json_opt
121     {:include => {:resource_picture => {}, :pictures => {}}}
122   end
123   
124   def destroy_with_file
125     PictureIO.original_picture_io.delete self.filename
126     self.resource_picture.destroy
127   end
128   
129   def store(imager)
130     unless imager
131       self.errors.add :base, 'illegal picture data'
132       return false
133     end
134     res = false
135     self.attributes = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
136     OriginalPicture.transaction do
137       if res = self.save
138         if res = PictureIO.original_picture_io.put(imager.binary, self.filename)
139           res = true
140         else
141           self.errors.add :base, 'original picture io does not work'
142           raise ActiveRecord::Rollback
143         end
144       end
145     end
146     res
147   end
148   
149   def restore(subdir = nil)
150     PictureIO.original_picture_io.get self.filename, subdir
151   end
152   
153   def self.export ar
154     l = LicenseGroup.list
155     op = OriginalPicture.list ar.id
156     {:license_groups => l, :original_pictures => op}
157   end
158   
159 end