X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fmodels%2Flicense.rb;h=d150059ee79d9ab6d9e5c1070a85adf3c4bf2b8c;hb=ba0d8bd772bb5eafa9fe770a52bf0d09899d4571;hp=3226b2d6953f403983d13e398aa3b6a766a97f15;hpb=d70238912c25c4102d986b39240e1d6e3502db5c;p=pettanr%2Fpettanr.git diff --git a/app/models/license.rb b/app/models/license.rb index 3226b2d6..d150059e 100644 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -1,63 +1,48 @@ +# class License < ActiveRecord::Base - has_one :original_license - has_one :common_license + belongs_to :license_group + belongs_to :system_picture + + validates :license_group_id, :presence => true, :numericality => true, :existence => true validates :name, :presence => true, :length => {:maximum => 50} - validates :url, :presence => true, :length => {:maximum => 200}, :uniqueness => true, :url => {:allow_blank => true} + 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 - 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' + def self.store name, attr + r = License.replace_system_picture attr + 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.default_page_size - 25 - end - - def self.max_page_size - 100 - end - - def self.page prm = nil - page = prm.to_i - page = 1 if page < 1 - page - 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 - 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.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.list opt = {}, page = 1, page_size = self.default_page_size + def self.list lg_id, opt = {} opt.merge!(self.list_opt) unless opt[:include] - opt.merge!({:order => 'name', :limit => page_size, :offset => (page -1) * page_size}) + opt.merge!({:conditions => ['licenses.license_group_id = ?', lg_id], :order => 'name'}) License.find(:all, opt) end def self.list_opt - {:include => {:common_license => {}, :original_license => {}}} + {:include => {:license_group => {}}} end def self.list_json_opt - {:include => {:common_license => {}, :original_license => {}}} + {:include => {:license_group => {}}} end def self.show rid, opt = {} @@ -67,13 +52,13 @@ class License < ActiveRecord::Base end def self.show_include_opt opt = {} - res = [:common_license, :original_license] + res = [:license_group] res.push(opt[:include]) if opt[:include] res end def self.show_json_include_opt - {:include => {:common_license => {}, :original_license => {}}} + {:include => {:license_group => {}}} end end