OSDN Git Service

build license model
[pettanr/pettanr.git] / app / models / license.rb
1 class License < ActiveRecord::Base
2   belongs_to :license_group
3   belongs_to :system_picture
4   
5   validates :license_group_id, :presence => true, :numericality => true, :existence => true
6   validates :name, :presence => true, :length => {:maximum => 50}
7   validates :caption, :presence => true, :length => {:maximum => 30}
8   validates :url, :presence => true, :length => {:maximum => 200}, :uniqueness => true, :url => true #{:allow_blank => true}
9   validates :system_picture_id, :presence => true, :numericality => true, :existence => true
10   
11   def self.update_license cl
12     l = License.find_by_url cl.url
13     l = License.new unless l
14     l.attributes = {
15       :name => cl.name, :url => cl.url, :cc_by => cl.cc_by, :cc_sa => cl.cc_sa, :cc_nd => cl.cc_nd, :cc_nc => cl.cc_nc, 
16       :no_resize => cl.no_resize, :no_flip => cl.no_flip, :no_convert => cl.no_convert, :keep_aspect_ratio => cl.keep_aspect_ratio
17     }
18     if cl.new_record? and l.new_record? == false
19       l.errors.add :base, 'dupulicate url'
20     end
21     l
22   end
23   
24   def self.list lg_id, opt = {}
25     opt.merge!(self.list_opt) unless opt[:include]
26     opt.merge!({:conditions => ['licenses.license_group_id = ?', lg_id], :order => 'name'})
27     License.find(:all, opt)
28   end
29   
30   def self.list_opt
31     {:include => {:license_group => {}}}
32   end
33   
34   def self.list_json_opt
35     {:include => {:license_group => {}}}
36   end
37   
38   def self.show rid, opt = {}
39     r = License.find(rid, :include => self.show_include_opt(opt))
40 #    raise ActiveRecord::Forbidden unless c.visible?(au)
41     r
42   end
43   
44   def self.show_include_opt opt = {}
45     res = [:license_group]
46     res.push(opt[:include]) if opt[:include]
47     res
48   end
49   
50   def self.show_json_include_opt
51     {:include => {:license_group => {}}}
52   end
53   
54 end