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_module_name, :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   validates :license_group_settings, :boost => {:boost_name => :license_group}
15   validates :credit_picture_settings, :boost => {:boost_name => :credit_picture}
16   
17   def overwrite 
18   end
19   
20   def caption_with_group
21     self.license_group.caption + '/' + self.caption
22   end
23   
24   def self.index_list_where list
25     ''
26   end
27   
28   def self.show_opt
29     {:include => {:license_group => {}}}
30   end
31   
32   def self.list_by_name name
33     License.find :all, :conditions => ['licenses.name = ?', name], :order => 'licenses.updated_at desc'
34   end
35   
36   def self.store name, attr
37     r = License.replace_system_picture attr
38     attr['credit_picture_settings'] = attr['credit_picture_settings'].to_json
39     attr['license_group_settings'] = attr['license_group_settings'].to_json
40     l = License.modify_object name, attr
41     l.boosts 'post'
42     if r == false
43       l.errors.add :base, SystemPicture.model_name.human + I18n.t('errors.not_create')
44     else
45       l.save
46     end
47     l
48   end
49   
50   def self.stores attrs, lg_id
51     res = 0
52     return 0 unless attrs.is_a?(Hash)
53     attrs.each do |name, attr|
54       attr["license_group_id"] = lg_id
55       l = License.store name, attr
56       res += 1 unless l.valid?
57     end
58     res
59   end
60   
61   def self.export(dt = nil)
62     opt = {}
63     opt.merge!({:conditions => ['updated_at >= ?', dt]}) if dt
64 #    opt.merge!({:order => 'name'})
65     License.find(:all, opt)
66   end
67   
68 end