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?
29 opt.merge!(License.list_opt)
30 opt.merge!({:order => 'name'})
31 License.find(:all, opt)
35 {:include => {:license_group => {}}}
38 def self.list_json_opt
39 {:include => {:license_group => {}}}
44 opt.merge!(License.show_opt)
45 res = License.find(rid, opt)
46 # raise ActiveRecord::Forbidden unless res.visible?(au)
51 {:include => {:license_group => {}}}
54 def self.show_json_opt
55 {:include => {:license_group => {}}}
58 def self.list_by_name name
59 License.find :all, :conditions => ['licenses.name = ?', name], :order => 'licenses.updated_at desc'
62 def self.store name, attr
63 r = License.replace_system_picture attr
64 attr['credit_pictures'] = attr['credit_pictures'].to_json
65 l = License.modify_object name, attr
67 l.errors.add :base, SystemPicture.model_name.human + I18n.t('errors.not_create')
74 def self.stores attrs, lg_id
76 return 0 unless attrs.is_a?(Hash)
77 attrs.each do |name, attr|
78 attr["license_group_id"] = lg_id
79 l = License.store name, attr
80 res += 1 unless l.valid?
85 def credit_pictures_attr
86 return @credit_pictures_attr if @credit_pictures_attr
88 @credit_pictures_attr = JSON.parse(self.credit_pictures)
91 @credit_pictures_attr = {} unless @credit_pictures_attr
95 def self.export(dt = nil)
97 opt.merge!({:conditions => ['updated_at >= ?', dt]}) if dt
98 # opt.merge!({:order => 'name'})
99 License.find(:all, opt)