OSDN Git Service

t#30138:create original pictures status
[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 => {:both => false}
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 unpublished?
60     self.pictures.empty?
61   end
62   
63   def stopped?
64     self.pictures.any? and self.resource_picture == nil
65   end
66   
67   def unlicensed?
68     self.pictures.any? and self.resource_picture and self.updated_at > self.pictures.first.head.updated_at
69   end
70   
71   def published?
72     self.pictures.any? and self.resource_picture and self.updated_at < self.pictures.first.head.updated_at
73   end
74   
75   def self.default_page_size
76     25
77   end
78   
79   def self.max_page_size
80     100
81   end
82   
83   def self.page prm = nil
84     page = prm.to_i
85     page = 1 if page < 1
86     page
87   end
88   
89   def self.page_size prm = self.default_page_size
90     page_size = prm.to_i
91     page_size = self.max_page_size if page_size > self.max_page_size
92     page_size = self.default_page_size if page_size < 1
93     page_size
94   end
95   
96   def self.mylist artist_id, page = 1, page_size = self.default_page_size
97     opt = {}
98     opt.merge!(self.list_opt)
99     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
100     opt.merge!({:conditions => ['original_pictures.artist_id = ?', artist_id], :order => 'original_pictures.updated_at desc'})
101     OriginalPicture.find(:all, opt)
102   end
103   
104   def history 
105     Picture.find(:all, {:conditions => ['pictures.original_picture_id = ?', self.id], :order => 'pictures.revision desc'} )
106   end
107   
108   def self.list_opt
109     {:include => {:resource_picture => {}, :pictures => {}}}
110   end
111   
112   def self.list_json_opt
113     {:include => {:resource_picture => {}, :pictures => {}}}
114   end
115   
116   def self.show cid, ar
117     opt = {}
118     opt.merge!(self.show_opt)
119     res = OriginalPicture.find(cid, opt)
120     raise ActiveRecord::Forbidden unless res.visible?(ar)
121     res
122   end
123   
124   def self.edit cid, ar
125     opt = {}
126     opt.merge!(self.show_opt)
127     res = OriginalPicture.find(cid, opt)
128     raise ActiveRecord::Forbidden unless res.own?(ar)
129     res
130   end
131   
132   def self.show_opt
133     {:include => {:resource_picture => {}, :pictures => {}}}
134   end
135   
136   def self.show_json_opt
137     {:include => {:resource_picture => {}, :pictures => {}}}
138   end
139   
140   def destroy_with_file
141     PictureIO.original_picture_io.delete self.filename
142     self.resource_picture.destroy
143   end
144   
145   def store(imager)
146     unless imager
147       self.errors.add :base, I18n.t('errors.invalid_image')
148       return false
149     end
150     res = false
151     self.attributes = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
152     OriginalPicture.transaction do
153       if res = self.save
154         begin
155           res = PictureIO.original_picture_io.put(imager.binary, self.filename)
156         rescue PictureIO::Error
157           res = false
158           self.errors.add :base, I18n.t('picture_io.error')
159           raise ActiveRecord::Rollback
160         end
161       end
162     end
163     res
164   end
165   
166   def restore(subdir = nil)
167     PictureIO.original_picture_io.get self.filename, subdir
168   end
169   
170   def self.export ar
171     l = LicenseGroup.list
172     op = OriginalPicture.list ar.id
173     {:license_groups => l, :original_pictures => op}
174   end
175   
176 end