OSDN Git Service

Merge branch 'v05' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v05client
[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.store name, attr
50     r = License.replace_system_picture attr
51     attr['credit_pictures'] = attr['credit_pictures'].to_json
52     l = License.modify_object name, attr
53     if r == false
54       l.errors.add :base, SystemPicture.model_name.human + I18n.t('errors.not_create')
55     else
56       l.save
57     end
58     l
59   end
60   
61   def self.stores attrs, lg_id
62     res = 0
63     return 0 unless attrs.is_a?(Hash)
64     attrs.each do |name, attr|
65       attr["license_group_id"] = lg_id
66       l = License.store name, attr
67       res += 1 unless l.valid?
68     end
69     res
70   end
71   
72   def credit_pictures_attr
73     return @credit_pictures_attr if @credit_pictures_attr
74     begin
75       @credit_pictures_attr = JSON.parse(self.credit_pictures)
76     rescue 
77     end
78     @credit_pictures_attr = {} unless @credit_pictures_attr
79     @credit_pictures_attr
80   end
81   
82   def self.export(dt = nil)
83     opt = {}
84     opt.merge!({:conditions => ['updated_at >= ?', dt]}) if dt
85 #    opt.merge!({:order => 'name'})
86     License.find(:all, opt)
87   end
88   
89 end