2 class License < ActiveRecord::Base
3 belongs_to :license_group
4 belongs_to :system_picture
6 validates :license_group_id, :presence => true, :numericality => true, :existence => {:both => false}
7 validates :name, :presence => true, :length => {:maximum => 50}
8 validates :caption, :presence => true, :length => {:maximum => 30}
9 validates :url, :presence => true, :length => {:maximum => 200}, :uniqueness => true, :url => {:message => I18n.t('errors.messages.url')} #{:allow_blank => true}
10 validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
12 before_validation :valid_encode
15 ['name', 'caption', 'url', 'settings', 'credit_pictures'].each do |a|
16 next if attributes[a] == nil
17 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
27 def caption_with_group
28 self.license_group.caption + '/' + self.caption
33 opt.merge!(License.list_opt)
34 opt.merge!({:order => 'name'})
35 License.find(:all, opt)
39 {:include => {:license_group => {}}}
42 def self.list_json_opt
43 {:include => {:license_group => {}}}
48 opt.merge!(License.show_opt)
49 res = License.find(rid, opt)
50 # raise ActiveRecord::Forbidden unless res.visible?(au)
55 {:include => {:license_group => {}}}
58 def self.show_json_opt
59 {:include => {:license_group => {}}}
62 def self.list_by_name name
63 License.find :all, :conditions => ['licenses.name = ?', name], :order => 'licenses.updated_at desc'
66 def self.store name, attr
67 r = License.replace_system_picture attr
68 attr['credit_pictures'] = attr['credit_pictures'].to_json
69 l = License.modify_object name, attr
71 l.errors.add :base, SystemPicture.model_name.human + I18n.t('errors.not_create')
78 def self.stores attrs, lg_id
80 return 0 unless attrs.is_a?(Hash)
81 attrs.each do |name, attr|
82 attr["license_group_id"] = lg_id
83 l = License.store name, attr
84 res += 1 unless l.valid?
89 def credit_pictures_attr
90 return @credit_pictures_attr if @credit_pictures_attr
92 @credit_pictures_attr = JSON.parse(self.credit_pictures)
95 @credit_pictures_attr = {} unless @credit_pictures_attr
99 def self.export(dt = nil)
101 opt.merge!({:conditions => ['updated_at >= ?', dt]}) if dt
102 # opt.merge!({:order => 'name'})
103 License.find(:all, opt)