#素材
-class ResourcePicture < ActiveRecord::Base
+class ResourcePicture < Pettanr::Content
belongs_to :artist
belongs_to :license
belongs_to :picture
- has_many :panel_pictures
belongs_to :original_picture
validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
validates :classname, :presence => true, :length => {:maximum => 50}
validates :picture_id, :presence => true, :numericality => true, :existence => {:both => false}
+ def self.owner_type
+ :artist
+ end
+
+ def self.valid_encode_columns
+ super + ['artist_name', 'classname', 'credit', 'settings']
+ end
+
def supply_default
end
self.attributes = attr
end
- def own? ar
- return false unless ar.is_a?(Artist)
- ar.id == self.artist_id
- end
-
- def visible? au
- if au == nil
- return false if MagicNumber['run_mode'] == 1
- elsif au.is_a?(Author)
- return true
- else
- return false
- end
+ def visible? operators
+ return false unless super
true
end
{:src => self.url('full'), :width => self.width, :height => self.height}
end
- def self.default_page_size
- 25
- end
-
- def self.max_page_size
- 100
+ def symbol_option
+ self.tmb_opt_img_tag
end
- def self.page prm = nil
- page = prm.to_i
- page = 1 if page < 1
- page
+ def self.list_order
+ 'resource_pictures.updated_at desc'
end
- def self.page_size prm = self.default_page_size
- page_size = prm.to_i
- page_size = self.max_page_size if page_size > self.max_page_size
- page_size = self.default_page_size if page_size < 1
- page_size
- end
-
- def self.list page = 1, page_size = self.default_page_size
- opt = {}
- opt.merge!(self.list_opt)
- opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
- opt.merge!({:order => 'updated_at desc'})
- ResourcePicture.find(:all, opt)
+ def self.list_where
+ ''
end
def self.list_opt
- {:include => {:license => {}, :artist => {}, :picture => {}} }
+ {:license => {}, :artist => {}, :picture => {} }
end
def self.list_json_opt
{:include => {:license => {}, :artist => {}, :picture => {}} }
end
- def self.show rid, au
- opt = {}
- opt.merge!(self.show_opt)
- r = ResourcePicture.find(rid, opt)
- raise ActiveRecord::Forbidden unless r.visible?(au)
- r
- end
-
def self.show_opt
{:include => {:license => {}, :artist => {}, :picture => {}} }
end
{:include => {:license => {}, :artist => {}, :picture => {}} }
end
- def self.edit rid, ar
- opt = {}
- opt.merge!(self.show_opt)
- r = ResourcePicture.find(rid, opt)
- raise ActiveRecord::Forbidden unless r.own?(ar)
- r
- end
-
def new_picture imager
pc = Picture.new
pc.supply_default
pc.overwrite self
r = pc.store imager
return pc if r
- self.errors.add :base, 'picture does not create' + pc.errors.full_messages.join("\n")
+ self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
false
end
res = false
self.overwrite self.original_picture
ResourcePicture.transaction do
+ self.original_picture.published_at = Time.now
+ self.original_picture.stopped_at = nil
+ raise ActiveRecord::Rollback unless self.original_picture.save
pc = self.new_picture imager
if pc
self.picture_id = pc.id
if res = self.store_picture(imager, self.filename)
if self.to_gif?
if gifimager = imager.to_gif
- res = self.store_picture(gifimager, self.gifname)
+ if res = self.store_picture(gifimager, self.gifname)
+ else
+ self.errors.add :base, I18n.t('picture_io.error')
+ end
else
- self.errors.add :base, 'picture data can not conv to gif'
+ self.errors.add :base, I18n.t('errors.not_convert')
res = false
end
end
else
- self.errors.add :base, 'resource picture io does not work'
+ self.errors.add :base, I18n.t('picture_io.error')
end
res
end
res = false
thumbnail_imager = self.flag_thumbnail >= 0 ? imager.to_thumbnail : imager
return false unless thumbnail_imager
- return false unless PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
- return false unless PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
- true
+ begin
+ PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
+ PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
+ res = true
+ rescue PictureIO::Error
+ res = false
+ end
+ res
end
def restore(subdir = nil)
end
def unpublish
- r = true
+ res = false
ResourcePicture.transaction do
- self.destroy
- PictureIO.resource_picture_io.delete(self.filename)
- PictureIO.resource_picture_io.delete(self.filename, 'full')
+ self.original_picture.published_at = nil
+ self.original_picture.stopped_at = Time.now
+ raise ActiveRecord::Rollback unless self.original_picture.save
+ begin
+ PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
+ PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
+ rescue PictureIO::Error
+ res = false
+ raise ActiveRecord::Rollback
+ end
+ res = self.destroy
+ raise ActiveRecord::Rollback unless res
end
- r
+ res
end
def self.visible_count
ResourcePicture.count
end
+ def picture_data
+ Base64.encode64(self.restore 'full')
+ end
+
def credit_template
"#{self.classname.tableize}/attributes/credit"
end