OSDN Git Service

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