OSDN Git Service

t#30277:change valid message for url
[pettanr/pettanr.git] / app / models / license.rb
index 11252f8..ff138c1 100644 (file)
@@ -1,79 +1,82 @@
+#
 class License < ActiveRecord::Base
-  has_one :original_license
-  has_one :common_license
-  validates :name, :presence => true, :length => {:maximum => 50}
-  validates :url, :presence => true, :length => {:maximum => 200}, :uniqueness => true
+  belongs_to :license_group
+  belongs_to :system_picture
   
-  def self.update_license cl
-    l = License.find_by_url cl.url
-    l = License.new unless l
-    l.attributes = {
-      :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, 
-      :no_resize => cl.no_resize, :no_flip => cl.no_flip, :no_convert => cl.no_convert, :keep_aspect_ratio => cl.keep_aspect_ratio
-    }
-    if cl.new_record? and l.new_record? == false
-      l.errors.add :base, 'dupulicate url'
-    end
-    l
-  end
+  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 => {:message => I18n.t('errors.messages.url')} #{:allow_blank => true}
+  validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
   
-  def self.default_page_size
-    25
+  def supply_default
   end
   
-  def self.max_page_size
-    100
+  def overwrite
   end
   
-  def self.page prm = nil
-    page = prm.to_i
-    page = 1 if page < 1
-    page
+  def self.list
+    opt = {}
+    opt.merge!(License.list_opt)
+    opt.merge!({:order => 'name'})
+    License.find(:all, opt)
   end
   
-  def self.page_size prm = self.default_page_size
-    page_size = prm.to_i
-    page_size = self.max_page_size if page_size > self.max_page_size
-    page_size = self.default_page_size if page_size < 1
-    page_size
+  def self.list_opt
+    {:include => {:license_group => {}}}
   end
   
-  def self.offset cnt, prm = nil
-    offset = prm.to_i
-    offset = cnt - 1 if offset >= cnt
-    offset = cnt - offset.abs if offset < 0
-    offset = 0 if offset < 0
-    offset
+  def self.list_json_opt
+    {:include => {:license_group => {}}}
   end
   
-  def self.list opt = {}, page = 1, page_size = self.default_page_size
-    opt.merge!(self.list_opt) unless opt[:include]
-    opt.merge!({:order => 'name', :limit => page_size, :offset => (page -1) * page_size})
-    License.find(:all, opt)
+  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.list_opt
-    {:include => {:common_license => {}, :original_license => {}}}
+  def self.show_opt
+    {:include => {:license_group => {}}}
   end
   
-  def self.list_json_opt
-    {:include => {:common_license => {}, :original_license => {}}}
+  def self.show_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
+  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'
+    else
+      l.save
+    end
+    l
   end
   
-  def self.show_include_opt opt = {}
-    res = [:common_license, :original_license]
-    res.push(opt[:include]) if opt[:include]
+  def self.stores attrs, lg_id
+    res = 0
+    return 0 unless attrs.is_a?(Hash)
+    attrs.each do |name, attr|
+      attr["license_group_id"] = lg_id
+      l = License.store name, attr
+      res += 1 unless l.valid?
+    end
     res
   end
   
-  def self.show_json_include_opt
-    {:include => {:common_license => {}, :original_license => {}}}
+  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
   
 end