OSDN Git Service

t#32402:add file prof
[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   before_validation :valid_encode
13   
14   def 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?
18     end
19   end
20   
21   def supply_default
22   end
23   
24   def overwrite
25   end
26   
27   def caption_with_group
28     self.license_group.caption + '/' + self.caption
29   end
30   
31   def self.list
32     opt = {}
33     opt.merge!(License.list_opt)
34     opt.merge!({:order => 'name'})
35     License.find(:all, opt)
36   end
37   
38   def self.list_opt
39     {:include => {:license_group => {}}}
40   end
41   
42   def self.list_json_opt
43     {:include => {:license_group => {}}}
44   end
45   
46   def self.show rid
47     opt = {}
48     opt.merge!(License.show_opt)
49     res = License.find(rid, opt)
50 #    raise ActiveRecord::Forbidden unless res.visible?(au)
51     res
52   end
53   
54   def self.show_opt
55     {:include => {:license_group => {}}}
56   end
57   
58   def self.show_json_opt
59     {:include => {:license_group => {}}}
60   end
61   
62   def self.list_by_name name
63     License.find :all, :conditions => ['licenses.name = ?', name], :order => 'licenses.updated_at desc'
64   end
65   
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
70     if r == false
71       l.errors.add :base, SystemPicture.model_name.human + I18n.t('errors.not_create')
72     else
73       l.save
74     end
75     l
76   end
77   
78   def self.stores attrs, lg_id
79     res = 0
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?
85     end
86     res
87   end
88   
89   def credit_pictures_attr
90     return @credit_pictures_attr if @credit_pictures_attr
91     begin
92       @credit_pictures_attr = JSON.parse(self.credit_pictures)
93     rescue 
94     end
95     @credit_pictures_attr = {} unless @credit_pictures_attr
96     @credit_pictures_attr
97   end
98   
99   def self.export(dt = nil)
100     opt = {}
101     opt.merge!({:conditions => ['updated_at >= ?', dt]}) if dt
102 #    opt.merge!({:order => 'name'})
103     License.find(:all, opt)
104   end
105   
106 end