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}
18 def caption_with_group
19 self.license_group.caption + '/' + self.caption
31 {:include => {:license_group => {}}}
34 def self.show_json_opt
35 {:include => {:license_group => {}}}
38 def self.list_by_name name
39 License.find :all, :conditions => ['licenses.name = ?', name], :order => 'licenses.updated_at desc'
42 def self.store name, attr
43 r = License.replace_system_picture attr
44 attr['credit_picture_settings'] = attr['credit_picture_settings'].to_json
45 attr['license_group_settings'] = attr['license_group_settings'].to_json
46 l = License.modify_object name, attr
48 l.errors.add :base, SystemPicture.model_name.human + I18n.t('errors.not_create')
55 def self.stores attrs, lg_id
57 return 0 unless attrs.is_a?(Hash)
58 attrs.each do |name, attr|
59 attr["license_group_id"] = lg_id
60 l = License.store name, attr
61 res += 1 unless l.valid?
74 {:license_group => {}}
77 def self.list_json_opt
78 {:include => {:license_group => {}}}
81 def credit_pictures_attr
82 return @credit_pictures_attr if @credit_pictures_attr
84 @credit_pictures_attr = JSON.parse(self.credit_pictures)
87 @credit_pictures_attr = {} unless @credit_pictures_attr
92 def self.export(dt = nil)
94 opt.merge!({:conditions => ['updated_at >= ?', dt]}) if dt
95 # opt.merge!({:order => 'name'})
96 License.find(:all, opt)