2 class License < Peta::SystemResource
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 :license_group_classname, :presence => true, :length => {:maximum => 50}
10 validates :name, :presence => true, :length => {:maximum => 50}
11 validates :caption, :presence => true, :length => {:maximum => 30}
12 validates :url, :presence => true, :length => {:maximum => 200}, :uniqueness => {:scope => :name}, :url => {:message => I18n.t('errors.messages.url')} #{:allow_blank => true}
13 validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
21 def caption_with_group
22 self.license_group.caption + '/' + self.caption
34 {:license_group => {}}
37 def self.list_json_opt
38 {:include => {:license_group => {}}}
42 {:include => {:license_group => {}}}
45 def self.show_json_opt
46 {:include => {:license_group => {}}}
49 def self.list_by_name name
50 License.find :all, :conditions => ['licenses.name = ?', name], :order => 'licenses.updated_at desc'
53 def self.store name, attr
54 r = License.replace_system_picture attr
55 attr['credit_picture_settings'] = attr['credit_picture_settings'].to_json
56 attr['license_group_settings'] = attr['license_group_settings'].to_json
57 l = License.modify_object name, attr
59 l.errors.add :base, SystemPicture.model_name.human + I18n.t('errors.not_create')
66 def self.stores attrs, lg_id
68 return 0 unless attrs.is_a?(Hash)
69 attrs.each do |name, attr|
70 attr["license_group_id"] = lg_id
71 l = License.store name, attr
72 res += 1 unless l.valid?
77 def credit_pictures_attr
78 return @credit_pictures_attr if @credit_pictures_attr
80 @credit_pictures_attr = JSON.parse(self.credit_pictures)
83 @credit_pictures_attr = {} unless @credit_pictures_attr
87 def self.export(dt = nil)
89 opt.merge!({:conditions => ['updated_at >= ?', dt]}) if dt
90 # opt.merge!({:order => 'name'})
91 License.find(:all, opt)