2 class License < ActiveRecord::Base
3 belongs_to :license_group
4 belongs_to :system_picture
5 has_many :resource_pictures
7 validates :license_group_id, :presence => true, :numericality => true, :existence => {:both => false}
8 validates :name, :presence => true, :length => {:maximum => 50}
9 validates :caption, :presence => true, :length => {:maximum => 30}
10 validates :url, :presence => true, :length => {:maximum => 200}, :uniqueness => true, :url => {:message => I18n.t('errors.messages.url')} #{:allow_blank => true}
11 validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
13 before_validation :valid_encode
16 ['name', 'caption', 'url', 'settings', 'credit_pictures'].each do |a|
17 next if attributes[a] == nil
18 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
28 def caption_with_group
29 self.license_group.caption + '/' + self.caption
37 self.where(self.list_where()).includes(self.list_opt).order('licenses.name')
40 def self.list_by_license_group_where license_group_id
41 ['licenses.license_group_id = ?', license_group_id]
44 def self.list_by_license_group license_group_id, roles, page = 1, page_size = self.default_page_size
45 self.where(self.list_by_license_group_where(license_group_id)).includes(self.list_opt).order('licenses.updated_at desc').offset((page -1) * page_size).limit(page_size)
49 {:license_group => {}}
52 def self.list_json_opt
53 {:include => {:license_group => {}}}
56 def self.show rid, roles = nil
58 opt.merge!(License.show_opt)
59 res = License.find(rid, opt)
60 # raise ActiveRecord::Forbidden unless res.visible?(au)
65 {:include => {:license_group => {}}}
68 def self.show_json_opt
69 {:include => {:license_group => {}}}
72 def self.list_by_name name
73 License.find :all, :conditions => ['licenses.name = ?', name], :order => 'licenses.updated_at desc'
76 def self.store name, attr
77 r = License.replace_system_picture attr
78 attr['credit_pictures'] = attr['credit_pictures'].to_json
79 l = License.modify_object name, attr
81 l.errors.add :base, SystemPicture.model_name.human + I18n.t('errors.not_create')
88 def self.stores attrs, lg_id
90 return 0 unless attrs.is_a?(Hash)
91 attrs.each do |name, attr|
92 attr["license_group_id"] = lg_id
93 l = License.store name, attr
94 res += 1 unless l.valid?
99 def credit_pictures_attr
100 return @credit_pictures_attr if @credit_pictures_attr
102 @credit_pictures_attr = JSON.parse(self.credit_pictures)
105 @credit_pictures_attr = {} unless @credit_pictures_attr
106 @credit_pictures_attr
109 def self.export(dt = nil)
111 opt.merge!({:conditions => ['updated_at >= ?', dt]}) if dt
112 # opt.merge!({:order => 'name'})
113 License.find(:all, opt)