OSDN Git Service

Merge branch 'v05' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v05
[pettanr/pettanr.git] / app / models / license.rb
index 704e348..6607b20 100644 (file)
@@ -3,17 +3,59 @@ class License < ActiveRecord::Base
   belongs_to :license_group
   belongs_to :system_picture
   
-  validates :license_group_id, :presence => true, :numericality => true, :existence => true
+  validates :license_group_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :name, :presence => true, :length => {:maximum => 50}
   validates :caption, :presence => true, :length => {:maximum => 30}
-  validates :url, :presence => true, :length => {:maximum => 200}, :uniqueness => true, :url => true #{:allow_blank => true}
-  validates :system_picture_id, :presence => true, :numericality => true, :existence => true
+  validates :url, :presence => true, :length => {:maximum => 200}, :uniqueness => true, :url => {:message => I18n.t('errors.messages.url')} #{:allow_blank => true}
+  validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
+  
+  def supply_default
+  end
+  
+  def overwrite
+  end
+  
+  def self.list
+    opt = {}
+    opt.merge!(License.list_opt)
+    opt.merge!({:order => 'name'})
+    License.find(:all, opt)
+  end
+  
+  def self.list_opt
+    {:include => {:license_group => {}}}
+  end
+  
+  def self.list_json_opt
+    {:include => {:license_group => {}}}
+  end
+  
+  def self.show rid
+    opt = {}
+    opt.merge!(License.show_opt)
+    res = License.find(rid, opt)
+#    raise ActiveRecord::Forbidden unless res.visible?(au)
+    res
+  end
+  
+  def self.show_opt
+    {:include => {:license_group => {}}}
+  end
+  
+  def self.show_json_opt
+    {:include => {:license_group => {}}}
+  end
+  
+  def self.list_by_name name
+    License.find :all, :conditions => ['licenses.name = ?', name], :order => 'licenses.updated_at desc'
+  end
   
   def self.store name, attr
     r = License.replace_system_picture attr
+    attr['credit_pictures'] = attr['credit_pictures'].to_json
     l = License.modify_object name, attr
     if r == false
-      l.errors.add :base, 'system picture can not create'
+      l.errors.add :base, SystemPicture.model_name.human + I18n.t('errors.not_create')
     else
       l.save
     end
@@ -31,34 +73,21 @@ class License < ActiveRecord::Base
     res
   end
   
-  def self.list opt = {}
-    opt.merge!(self.list_opt) unless opt[:include]
-    opt.merge!({:order => 'name'})
-    License.find(:all, opt)
-  end
-  
-  def self.list_opt
-    {:include => {:license_group => {}}}
-  end
-  
-  def self.list_json_opt
-    {:include => {:license_group => {}}}
-  end
-  
-  def self.show rid, opt = {}
-    r = License.find(rid, :include => self.show_include_opt(opt))
-#    raise ActiveRecord::Forbidden unless c.visible?(au)
-    r
-  end
-  
-  def self.show_include_opt opt = {}
-    res = [:license_group]
-    res.push(opt[:include]) if opt[:include]
-    res
+  def credit_pictures_attr
+    return @credit_pictures_attr if @credit_pictures_attr
+    begin
+      @credit_pictures_attr = JSON.parse(self.credit_pictures)
+    rescue 
+    end
+    @credit_pictures_attr = {} unless @credit_pictures_attr
+    @credit_pictures_attr
   end
   
-  def self.show_json_include_opt
-    {:include => {:license_group => {}}}
+  def self.export(dt = nil)
+    opt = {}
+    opt.merge!({:conditions => ['updated_at >= ?', dt]}) if dt
+#    opt.merge!({:order => 'name'})
+    License.find(:all, opt)
   end
   
 end