2 class License < Peta::Item
4 belongs_to :license_group
5 belongs_to :system_picture
6 has_many :resource_pictures
8 validates :license_group_id, :presence => true, :numericality => true, :existence => {:both => false}
9 validates :name, :presence => true, :length => {:maximum => 50}
10 validates :caption, :presence => true, :length => {:maximum => 30}
11 validates :url, :presence => true, :length => {:maximum => 200}, :uniqueness => true, :url => {:message => I18n.t('errors.messages.url')} #{:allow_blank => true}
12 validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
20 def caption_with_group
21 self.license_group.caption + '/' + self.caption
33 {:license_group => {}}
36 def self.list_json_opt
37 {:include => {:license_group => {}}}
41 {:include => {:license_group => {}}}
44 def self.show_json_opt
45 {:include => {:license_group => {}}}
48 def self.list_by_name name
49 License.find :all, :conditions => ['licenses.name = ?', name], :order => 'licenses.updated_at desc'
52 def self.store name, attr
53 r = License.replace_system_picture attr
54 attr['credit_pictures'] = attr['credit_pictures'].to_json
55 l = License.modify_object name, attr
57 l.errors.add :base, SystemPicture.model_name.human + I18n.t('errors.not_create')
64 def self.stores attrs, lg_id
66 return 0 unless attrs.is_a?(Hash)
67 attrs.each do |name, attr|
68 attr["license_group_id"] = lg_id
69 l = License.store name, attr
70 res += 1 unless l.valid?
75 def credit_pictures_attr
76 return @credit_pictures_attr if @credit_pictures_attr
78 @credit_pictures_attr = JSON.parse(self.credit_pictures)
81 @credit_pictures_attr = {} unless @credit_pictures_attr
85 def self.export(dt = nil)
87 opt.merge!({:conditions => ['updated_at >= ?', dt]}) if dt
88 # opt.merge!({:order => 'name'})
89 License.find(:all, opt)