OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / app / models / license.rb
1 #
2 class License < Peta::SystemResource
3   load_manifest
4   belongs_to :license_group
5   belongs_to :system_picture
6   has_many :resource_pictures
7   
8   validates :license_group_id, :presence => true, :numericality => true, :existence => {:both => false}
9   validates :license_group_classname, :presence => true, :length => {:maximum => 50}
10   validates :name, :presence => true, :length => {:maximum => 50}
11   validates :caption, :presence => true, :length => {:maximum => 30}
12   validates :url, :presence => true, :length => {:maximum => 200}, :uniqueness => {:scope => :name}, :url => {:message => I18n.t('errors.messages.url')} #{:allow_blank => true}
13   validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
14   
15   def caption_with_group
16     self.license_group.caption + '/' + self.caption
17   end
18   
19   def self.list_where
20     ''
21   end
22   
23   def self.list_order
24     'licenses.name'
25   end
26   
27   def self.show_opt
28     {:include => {:license_group => {}}}
29   end
30   
31   def self.show_json_opt
32     {:include => {:license_group => {}}}
33   end
34   
35   def self.list_by_name name
36     License.find :all, :conditions => ['licenses.name = ?', name], :order => 'licenses.updated_at desc'
37   end
38   
39   def self.store name, attr
40     r = License.replace_system_picture attr
41     attr['credit_picture_settings'] = attr['credit_picture_settings'].to_json
42     attr['license_group_settings'] = attr['license_group_settings'].to_json
43     l = License.modify_object name, attr
44     if r == false
45       l.errors.add :base, SystemPicture.model_name.human + I18n.t('errors.not_create')
46     else
47       l.save
48     end
49     l
50   end
51   
52   def self.stores attrs, lg_id
53     res = 0
54     return 0 unless attrs.is_a?(Hash)
55     attrs.each do |name, attr|
56       attr["license_group_id"] = lg_id
57       l = License.store name, attr
58       res += 1 unless l.valid?
59     end
60     res
61   end
62   
63 =begin
64   def supply_default
65   end
66   
67   def overwrite
68   end
69   
70   def self.list_opt
71     {:license_group => {}}
72   end
73   
74   def self.list_json_opt
75     {:include => {:license_group => {}}}
76   end
77   
78   def credit_pictures_attr
79     return @credit_pictures_attr if @credit_pictures_attr
80     begin
81       @credit_pictures_attr = JSON.parse(self.credit_pictures)
82     rescue 
83     end
84     @credit_pictures_attr = {} unless @credit_pictures_attr
85     @credit_pictures_attr
86   end
87 =end
88   
89   def self.export(dt = nil)
90     opt = {}
91     opt.merge!({:conditions => ['updated_at >= ?', dt]}) if dt
92 #    opt.merge!({:order => 'name'})
93     License.find(:all, opt)
94   end
95   
96 end