2 class LicenseGroup < Peta::Template
4 has_many :resource_pictures
7 validates :name, :presence => true, :length => {:maximum => 50}, :uniqueness => true
8 validates :module_name, :presence => true, :length => {:maximum => 50}
9 validates :caption, :presence => true, :length => {:maximum => 30}
10 validates :url, :presence => true, :length => {:maximum => 200}, :url => {:message => I18n.t('errors.messages.url')}
12 scope :find_index, -> do
23 {:include => {:licenses => {}}}
26 def self.store name, attr
27 #ライセンスデータがあるとライセンスグループのデータ生成で邪魔するので移しておく
28 lattr = attr["licenses_attributes"]
29 attr.delete "licenses_attributes"
30 #ライセンスグループを先に保存してidを決める
31 r = LicenseGroup.modify_object name, attr
33 #取っておいたライセンスデータとidでライセンス作成
34 if (c = License.stores(lattr, r.id)) > 0
35 r.errors.add :base, License.model_name.human + I18n.t('errors.not_create')
40 # destroy with licenses
43 self.class.transaction do
44 self.licenses.each do |license|
45 raise ActiveRecord::Rollback unless license.destroy
48 raise ActiveRecord::Rollback unless res
53 def self.import filename
54 LicenseGroup.import_file(filename) {|name, attr| LicenseGroup.store(name, attr)}
57 def self.disp_import_error r
59 puts "json file error. file does not exist or broken"
63 put lg.errors.full_messages