-class License < ActiveRecord::Base
- has_one :original_license
- has_one :common_license
- accepts_nested_attributes_for :original_license
- accepts_nested_attributes_for :common_license
+#
+class License < Peta::SystemResource
+ load_manifest
+ belongs_to :license_group
+ belongs_to :system_picture
+ has_many :resource_pictures
+
+ validates :license_group_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :license_group_module_name, :presence => true, :length => {:maximum => 50}
+ validates :name, :presence => true, :length => {:maximum => 50}
+ validates :caption, :presence => true, :length => {:maximum => 30}
+ validates :url, :presence => true, :length => {:maximum => 200}, :uniqueness => {:scope => :name}, :url => {:message => I18n.t('errors.messages.url')} #{:allow_blank => true}
+ validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :license_group_settings, :boost => {:boost_name => :license_group}
+ validates :credit_picture_settings, :boost => {:boost_name => :credit_picture}
+
+ scope :find_index, -> do
+ self.all
+ end
+
+ scope :find_by_license_group, -> (license_group_id) do
+ where(license_group_id: license_group_id)
+ end
+
+ scope :find_by_system_picture, -> (system_picture_id) do
+ where(system_picture_id: system_picture_id)
+ end
+
+ def overwrite
+ end
+
+ def caption_with_group
+ self.license_group.caption + '/' + self.caption
+ end
+
+ def self.index_list_where list
+ ''
+ end
+
+ def self.show_opt
+ {:include => {:license_group => {}}}
+ end
+
+ def self.list_by_name name
+ License.where(name: name).order(updated_at: :desc)
+ end
+
+ def self.store name, attr
+ r = License.replace_system_picture attr
+ attr['credit_picture_settings'] = attr['credit_picture_settings'].to_json
+ attr['license_group_settings'] = attr['license_group_settings'].to_json
+ l = License.modify_object name, attr
+ l.boosts 'post'
+ if r == false
+ l.errors.add :base, SystemPicture.model_name.human + I18n.t('errors.not_create')
+ else
+ l.save
+ end
+ l
+ end
+
+ 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.export(dt = nil)
+ licenses = License.all
+ licenses = licenses.where(['licenses.updated_at >= ?', dt]) if dt
+ licenses.order(:id)
+ end
end