+#
class License < ActiveRecord::Base
belongs_to :license_group
belongs_to :system_picture
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.stores attrs
+ res = 0
+ return 0 unless attrs.is_a?(Hash)
+ attrs.each do |name, attr|
+ l = License.store name, attr
+ res += 1 unless l.valid?
+ end
+ res
+ end
+
def self.list lg_id, opt = {}
opt.merge!(self.list_opt) unless opt[:include]
opt.merge!({:conditions => ['licenses.license_group_id = ?', lg_id], :order => 'name'})
validates :url, :presence => true, :length => {:maximum => 200}, :url => true
def self.store name, attr
+ lattr = attr["licenses_attributes"]
+ attr.delete "licenses_attributes"
r = LicenseGroup.modify_object name, attr
+ if (c = License.stores(lattr)) > 0
+ r.errors.add :base, 'licenses can not create'
+ end
r.save
r
end
"name": "PublicDomain", \r
"classname": "PublicDomain", \r
"caption": "Public Domain", \r
- "url": "http://test.com/"\r
+ "url": "http://test.com/",\r
+ "licenses_attributes": {\r
+ "PublicDomain": {\r
+ "caption": "Public Domain", \r
+ "url": "http://test.com/",\r
+ "system_picture": "Data"\r
+ }\r
+ }\r
}\r
}\r
"Unknown": {\r
"classname": "Unknown", \r
"caption": "Unknown owner", \r
- "url": "http://test.uk/"\r
+ "url": "http://test.uk/",\r
+ "licenses_attributes": {\r
+ "UnknownF1": {\r
+ "caption": "Unknown Flag1", \r
+ "url": "http://test.com/f1",\r
+ "system_picture": "Dataf1"\r
+ },\r
+ "UnknownF2": {\r
+ "caption": "Unknown Flag2", \r
+ "url": "http://test.com/f2",\r
+ "system_picture": "Dataf2"\r
+ }\r
+ }\r
}\r
}\r
it 'データ更新準備を依頼する' do
LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
LicenseGroup.should_receive(:modify_object).with(any_args).exactly(1)
+ License.stub(:stores).with(any_args).and_return(0)
+ LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
+ r = LicenseGroup.store(@n, @a)
+ end
+ it 'ライセンスに複数の更新を依頼する' do
+ LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
+ License.stub(:stores).with(any_args).and_return(0)
+ License.should_receive(:stores).with(any_args).exactly(1)
LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
r = LicenseGroup.store(@n, @a)
end
it '保存を依頼する' do
LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
+ License.stub(:stores).with(any_args).and_return(0)
LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
LicenseGroup.any_instance.should_receive(:save).with(any_args).exactly(1)
r = LicenseGroup.store(@n, @a)
r.name.should eq @n
r.url.should eq @a["url"]
end
+ it 'カラム値からlicenses_attributesが削除されている' do
+ @a["licenses_attributes"].should_not be_nil
+ r = LicenseGroup.store(@n, @a)
+ @a["licenses_attributes"].should be_nil
+ end
+ end
+ context 'ライセンス複数更新が失敗するとき' do
+ before do
+ LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
+ License.stub(:stores).with(any_args).and_return(1)
+ LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
+ end
+ it '全体エラーメッセージがセットされている' do
+ r = LicenseGroup.store(@n, @a)
+ r.errors[:base].should_not be_blank
+ end
+ it 'ライセンスが作成されていない' do
+ lambda {
+ r = LicenseGroup.store(@n, @a)
+ }.should_not change License, :count
+ end
+ it 'ライセンスグループが作成されていない' do
+ lambda {
+ r = LicenseGroup.store(@n, @a)
+ }.should_not change LicenseGroup, :count
+ end
end
end
end
end
+ describe '更新に於いて' do
+ before do
+ @lg = Factory :license_group
+ @f = Rails.root + 'spec/json/license_group.json'
+ @t = File.open(@f, 'r').read
+ @j = JSON.parse @t
+ @n = @j.keys.first
+ @a = @j.values.first
+ @attr = @a["licenses_attributes"]
+ @ln = @attr.keys.first
+ @la = @attr.values.first
+ @la["license_group_id"] = @lg.id
+ end
+ context 'つつがなく終わるとき' do
+ it 'システム画像置換を依頼する' do
+ License.stub(:replace_system_picture).with(any_args).and_return(true)
+ License.should_receive(:replace_system_picture).with(any_args).exactly(1)
+ License.stub(:modify_object).with(any_args).and_return(License.new)
+ License.any_instance.stub(:save).with(any_args).and_return(true)
+ r = License.store(@ln, @la)
+ end
+ it 'データ更新準備を依頼する' do
+ License.stub(:replace_system_picture).with(any_args).and_return(true)
+ License.stub(:modify_object).with(any_args).and_return(License.new)
+ License.should_receive(:modify_object).with(any_args).exactly(1)
+ License.any_instance.stub(:save).with(any_args).and_return(true)
+ r = License.store(@ln, @la)
+ end
+ it '保存を依頼する' do
+ License.stub(:replace_system_picture).with(any_args).and_return(true)
+ License.stub(:modify_object).with(any_args).and_return(License.new)
+ License.any_instance.stub(:save).with(any_args).and_return(true)
+ License.any_instance.should_receive(:save).with(any_args).exactly(1)
+ r = License.store(@ln, @la)
+ end
+ it 'オブジェクトを返す' do
+ r = License.store(@ln, @la)
+ r.is_a?(License).should be_true
+ r.name.should eq @ln
+ r.url.should eq @la["url"]
+ end
+ it 'ライセンスが作成されている' do
+ lambda {
+ r = License.store(@ln, @la)
+ }.should change License, :count
+ end
+ it 'システム画像が作成されている' do
+ lambda {
+ r = License.store(@ln, @la)
+ }.should change SystemPicture, :count
+ end
+ end
+ context 'システム画像置換が失敗するとき' do
+ before do
+ License.stub(:replace_system_picture).with(any_args).and_return(false)
+ License.stub(:modify_object).with(any_args).and_return(License.new)
+ end
+ it '全体エラーメッセージがセットされている' do
+ r = License.store(@ln, @la)
+ r.errors[:base].should_not be_blank
+ end
+ it 'ライセンスが作成されていない' do
+ lambda {
+ r = License.store(@ln, @la)
+ }.should_not change License, :count
+ end
+ end
+ end
+
+ describe '複数の更新に於いて' do
+ before do
+ @fs = Rails.root + 'spec/json/license_groups.json'
+ @ts = File.open(@fs, 'r').read
+ @js = JSON.parse @ts
+ @n = @js.keys.last
+ @a = @js.values.last
+ @attr = @a["licenses_attributes"]
+ end
+ context '2件データでつつがなく終わるとき' do
+ it '更新を2回依頼する' do
+ License.stub(:store).with(any_args).and_return(License.new)
+ License.should_receive(:store).with(any_args).exactly(2)
+ License.any_instance.stub(:valid?).with(any_args).and_return(true)
+ r = License.stores(@attr)
+ end
+ it '失敗件数0を返す' do
+ License.stub(:store).with(any_args).and_return(License.new)
+ License.any_instance.stub(:valid?).with(any_args).and_return(true)
+ r = License.stores(@attr)
+ r.should eq 0
+ end
+ end
+ context '2件データで失敗するとき' do
+ it '更新を2回依頼する' do
+ License.stub(:store).with(any_args).and_return(License.new)
+ License.should_receive(:store).with(any_args).exactly(2)
+ License.any_instance.stub(:valid?).with(any_args).and_return(false)
+ r = License.stores(@attr)
+ end
+ it '失敗件数2を返す' do
+ License.stub(:store).with(any_args).and_return(License.new)
+ License.any_instance.stub(:valid?).with(any_args).and_return(false)
+ r = License.stores(@attr)
+ r.should eq 2
+ end
+ end
+ context 'attrsがnilなどのHashでないとき' do
+ it '処理にかけず0を返す' do
+ r = License.stores(nil)
+ r.should eq 0
+ end
+ end
+ end
+
describe '単体取得に於いて' do
before do
@sp = Factory :system_picture