2 class License < Peta::Item
3 belongs_to :license_group
4 belongs_to :system_picture
5 has_many :resource_pictures
7 validates :license_group_id, :presence => true, :numericality => true, :existence => {:both => false}
8 validates :name, :presence => true, :length => {:maximum => 50}
9 validates :caption, :presence => true, :length => {:maximum => 30}
10 validates :url, :presence => true, :length => {:maximum => 200}, :uniqueness => true, :url => {:message => I18n.t('errors.messages.url')} #{:allow_blank => true}
11 validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
13 def self.valid_encode_columns
14 super + ['name', 'caption', 'url', 'settings', 'credit_pictures']
23 def caption_with_group
24 self.license_group.caption + '/' + self.caption
36 {:license_group => {}}
39 def self.list_json_opt
40 {:include => {:license_group => {}}}
44 {:include => {:license_group => {}}}
47 def self.show_json_opt
48 {:include => {:license_group => {}}}
51 def self.list_by_name name
52 License.find :all, :conditions => ['licenses.name = ?', name], :order => 'licenses.updated_at desc'
55 def self.store name, attr
56 r = License.replace_system_picture attr
57 attr['credit_pictures'] = attr['credit_pictures'].to_json
58 l = License.modify_object name, attr
60 l.errors.add :base, SystemPicture.model_name.human + I18n.t('errors.not_create')
67 def self.stores attrs, lg_id
69 return 0 unless attrs.is_a?(Hash)
70 attrs.each do |name, attr|
71 attr["license_group_id"] = lg_id
72 l = License.store name, attr
73 res += 1 unless l.valid?
78 def credit_pictures_attr
79 return @credit_pictures_attr if @credit_pictures_attr
81 @credit_pictures_attr = JSON.parse(self.credit_pictures)
84 @credit_pictures_attr = {} unless @credit_pictures_attr
88 def self.export(dt = nil)
90 opt.merge!({:conditions => ['updated_at >= ?', dt]}) if dt
91 # opt.merge!({:order => 'name'})
92 License.find(:all, opt)