OSDN Git Service

t#31018:add search om license
[pettanr/pettanr.git] / app / models / license.rb
1 #
2 class License < ActiveRecord::Base
3   belongs_to :license_group
4   belongs_to :system_picture
5   
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}
11   
12   def supply_default
13   end
14   
15   def overwrite
16   end
17   
18   def self.list
19     opt = {}
20     opt.merge!(License.list_opt)
21     opt.merge!({:order => 'name'})
22     License.find(:all, opt)
23   end
24   
25   def self.list_opt
26     {:include => {:license_group => {}}}
27   end
28   
29   def self.list_json_opt
30     {:include => {:license_group => {}}}
31   end
32   
33   def self.show rid
34     opt = {}
35     opt.merge!(License.show_opt)
36     res = License.find(rid, opt)
37 #    raise ActiveRecord::Forbidden unless res.visible?(au)
38     res
39   end
40   
41   def self.show_opt
42     {:include => {:license_group => {}}}
43   end
44   
45   def self.show_json_opt
46     {:include => {:license_group => {}}}
47   end
48   
49   def self.list_by_name name
50     License.find :all, :conditions => ['licenses.name = ?', name], :order => 'licenses.updated_at desc'
51   end
52   
53   def self.store name, attr
54     r = License.replace_system_picture attr
55     attr['credit_pictures'] = attr['credit_pictures'].to_json
56     l = License.modify_object name, attr
57     if r == false
58       l.errors.add :base, SystemPicture.model_name.human + I18n.t('errors.not_create')
59     else
60       l.save
61     end
62     l
63   end
64   
65   def self.stores attrs, lg_id
66     res = 0
67     return 0 unless attrs.is_a?(Hash)
68     attrs.each do |name, attr|
69       attr["license_group_id"] = lg_id
70       l = License.store name, attr
71       res += 1 unless l.valid?
72     end
73     res
74   end
75   
76   def credit_pictures_attr
77     return @credit_pictures_attr if @credit_pictures_attr
78     begin
79       @credit_pictures_attr = JSON.parse(self.credit_pictures)
80     rescue 
81     end
82     @credit_pictures_attr = {} unless @credit_pictures_attr
83     @credit_pictures_attr
84   end
85   
86   def self.export(dt = nil)
87     opt = {}
88     opt.merge!({:conditions => ['updated_at >= ?', dt]}) if dt
89 #    opt.merge!({:order => 'name'})
90     License.find(:all, opt)
91   end
92   
93 end