2010-05-17 20:46:41 +00:00
|
|
|
require 'spec_helper'
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-17 20:46:41 +00:00
|
|
|
describe AssetCollection do
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-17 22:51:53 +00:00
|
|
|
# it 'should have a valid factory' do
|
|
|
|
# Factory.build(:asset_collection).should be_valid
|
|
|
|
# end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-17 20:46:41 +00:00
|
|
|
describe 'custom fields (beta)' do
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-17 20:46:41 +00:00
|
|
|
before(:each) do
|
2010-05-24 00:18:23 +00:00
|
|
|
Site.any_instance.stubs(:create_default_pages!).returns(true)
|
2010-05-22 14:46:32 +00:00
|
|
|
site = Factory.build(:site)
|
|
|
|
Site.stubs(:find).returns(site)
|
|
|
|
@collection = Factory.build(:asset_collection, :site => site)
|
2010-05-21 00:34:32 +00:00
|
|
|
@collection.asset_custom_fields.build :label => 'My Description', :_alias => 'description', :kind => 'Text'
|
|
|
|
@collection.asset_custom_fields.build :label => 'Active', :kind => 'Boolean'
|
2010-07-20 10:15:53 +00:00
|
|
|
# AssetCollection.logger = Logger.new($stdout)
|
2010-06-29 22:09:47 +00:00
|
|
|
# AssetCollection.db.connection.instance_variable_set(:@logger, Logger.new($stdout))
|
2010-05-22 14:46:32 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-22 14:46:32 +00:00
|
|
|
context 'unit' do
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-22 14:46:32 +00:00
|
|
|
before(:each) do
|
2010-06-08 00:45:49 +00:00
|
|
|
@field = CustomFields::Field.new(:kind => 'String')
|
2010-05-22 14:46:32 +00:00
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-22 14:46:32 +00:00
|
|
|
it 'should tell if it is a String' do
|
|
|
|
@field.string?.should be_true
|
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-22 14:46:32 +00:00
|
|
|
it 'should tell if it is a Text' do
|
|
|
|
@field.kind = 'Text'
|
|
|
|
@field.text?.should be_true
|
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-22 14:46:32 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-22 14:46:32 +00:00
|
|
|
context 'validation' do
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-22 14:46:32 +00:00
|
|
|
%w{label kind}.each do |key|
|
|
|
|
it "should validate presence of #{key}" do
|
|
|
|
field = @collection.asset_custom_fields.build({ :label => 'Shortcut', :kind => 'String' }.merge(key.to_sym => nil))
|
|
|
|
field.should_not be_valid
|
|
|
|
field.errors[key.to_sym].should == ["can't be blank"]
|
|
|
|
end
|
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-22 14:46:32 +00:00
|
|
|
it 'should not have unique label' do
|
|
|
|
field = @collection.asset_custom_fields.build :label => 'Active', :kind => 'Boolean'
|
|
|
|
field.should_not be_valid
|
|
|
|
field.errors[:label].should == ["is already taken"]
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-22 14:46:32 +00:00
|
|
|
it 'should invalidate parent if custom field is not valid' do
|
|
|
|
field = @collection.asset_custom_fields.build
|
|
|
|
@collection.should_not be_valid
|
|
|
|
@collection.asset_custom_fields.last.errors[:label].should == ["can't be blank"]
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-17 20:46:41 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-19 23:42:19 +00:00
|
|
|
context 'define core attributes' do
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-19 23:42:19 +00:00
|
|
|
it 'should have an unique name' do
|
2010-05-21 00:34:32 +00:00
|
|
|
@collection.asset_custom_fields.first._name.should == "custom_field_1"
|
2010-07-23 20:09:54 +00:00
|
|
|
@collection.asset_custom_fields.last._name.should == "custom_field_2"
|
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-19 23:42:19 +00:00
|
|
|
it 'should have an unique alias' do
|
2010-06-10 14:50:50 +00:00
|
|
|
@collection.asset_custom_fields.first.safe_alias.should == "description"
|
|
|
|
@collection.asset_custom_fields.last.safe_alias.should == "active"
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-19 23:42:19 +00:00
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-19 23:42:19 +00:00
|
|
|
context 'build and save' do
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-19 23:42:19 +00:00
|
|
|
it 'should build asset' do
|
|
|
|
asset = @collection.assets.build
|
|
|
|
lambda {
|
|
|
|
asset.description
|
|
|
|
asset.active
|
2010-05-22 14:46:32 +00:00
|
|
|
asset.custom_fields.size.should == 2
|
2010-05-19 23:42:19 +00:00
|
|
|
}.should_not raise_error
|
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-19 23:42:19 +00:00
|
|
|
it 'should assign values to newly built asset' do
|
|
|
|
asset = build_asset(@collection)
|
|
|
|
asset.description.should == 'Lorem ipsum'
|
|
|
|
asset.active.should == true
|
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-19 23:42:19 +00:00
|
|
|
it 'should save asset' do
|
|
|
|
asset = build_asset(@collection)
|
|
|
|
asset.save and @collection.reload
|
|
|
|
asset = @collection.assets.first
|
|
|
|
asset.description.should == 'Lorem ipsum'
|
|
|
|
asset.active.should == true
|
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-19 23:42:19 +00:00
|
|
|
it 'should not modify assets from another collection' do
|
|
|
|
asset = build_asset(@collection)
|
|
|
|
asset.save and @collection.reload
|
|
|
|
new_collection = AssetCollection.new
|
|
|
|
lambda { new_collection.assets.build.description }.should raise_error
|
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-19 23:42:19 +00:00
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-19 23:42:19 +00:00
|
|
|
context 'modifying fields' do
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-17 22:51:53 +00:00
|
|
|
before(:each) do
|
2010-05-19 23:42:19 +00:00
|
|
|
@asset = build_asset(@collection).save
|
2010-05-17 22:51:53 +00:00
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-19 23:42:19 +00:00
|
|
|
it 'should add new field' do
|
2010-05-21 00:34:32 +00:00
|
|
|
@collection.asset_custom_fields.build :label => 'Active at', :name => 'active_at', :kind => 'Date'
|
2010-07-26 02:49:35 +00:00
|
|
|
@collection.upsert(:validate => false)
|
2010-05-19 23:42:19 +00:00
|
|
|
@collection.reload
|
|
|
|
asset = @collection.assets.first
|
|
|
|
lambda { asset.active_at }.should_not raise_error
|
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-19 23:42:19 +00:00
|
|
|
it 'should remove field' do
|
2010-05-21 00:34:32 +00:00
|
|
|
@collection.asset_custom_fields.clear
|
2010-07-26 02:49:35 +00:00
|
|
|
@collection.upsert(:validate => false)
|
2010-05-19 23:42:19 +00:00
|
|
|
@collection.reload
|
|
|
|
asset = @collection.assets.first
|
|
|
|
lambda { asset.active }.should raise_error
|
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-19 23:42:19 +00:00
|
|
|
it 'should rename field label' do
|
2010-05-21 00:34:32 +00:00
|
|
|
@collection.asset_custom_fields.first.label = 'Simple description'
|
|
|
|
@collection.asset_custom_fields.first._alias = nil
|
2010-07-26 02:49:35 +00:00
|
|
|
@collection.upsert(:validate => false)
|
2010-05-19 23:42:19 +00:00
|
|
|
@collection.reload
|
|
|
|
asset = @collection.assets.first
|
|
|
|
asset.simple_description.should == 'Lorem ipsum'
|
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-19 23:42:19 +00:00
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-19 23:42:19 +00:00
|
|
|
context 'managing from hash' do
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-07-20 10:15:53 +00:00
|
|
|
it 'adds new field' do
|
2010-05-21 00:34:32 +00:00
|
|
|
@collection.asset_custom_fields.clear
|
|
|
|
@collection.asset_custom_fields.build :label => 'Title'
|
|
|
|
@collection.asset_custom_fields_attributes = { '0' => { 'label' => 'A title', 'kind' => 'String' }, '-1' => { 'label' => 'Tagline', 'kind' => 'String' } }
|
|
|
|
@collection.asset_custom_fields.size.should == 2
|
|
|
|
@collection.asset_custom_fields.first.label.should == 'A title'
|
|
|
|
@collection.asset_custom_fields.last.label.should == 'Tagline'
|
2010-05-19 23:42:19 +00:00
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-07-20 10:15:53 +00:00
|
|
|
it 'updates/removes fields' do
|
2010-05-21 00:34:32 +00:00
|
|
|
@collection.asset_custom_fields.build :label => 'Title', :kind => 'String'
|
2010-05-19 23:42:19 +00:00
|
|
|
@collection.save; @collection = AssetCollection.first
|
2010-07-23 20:09:54 +00:00
|
|
|
@collection.update_attributes(:asset_custom_fields_attributes => {
|
|
|
|
'0' => { 'label' => 'My Description', 'kind' => 'Text', '_destroy' => '1' },
|
2010-07-26 02:49:35 +00:00
|
|
|
'1' => { 'label' => 'Active', 'kind' => 'Boolean', '_destroy' => '1' },
|
2010-06-29 22:09:47 +00:00
|
|
|
'2' => { 'label' => 'My Title !', 'kind' => 'String' },
|
2010-07-23 20:09:54 +00:00
|
|
|
'new_record' => { 'label' => 'Published at', 'kind' => 'String' }
|
2010-05-19 16:17:45 +00:00
|
|
|
})
|
|
|
|
@collection = AssetCollection.first
|
2010-06-29 22:09:47 +00:00
|
|
|
@collection.asset_custom_fields.size.should == 2
|
2010-05-21 00:34:32 +00:00
|
|
|
@collection.asset_custom_fields.first.label.should == 'My Title !'
|
2010-05-17 22:51:53 +00:00
|
|
|
end
|
2010-09-01 14:26:00 +00:00
|
|
|
|
2010-05-17 20:46:41 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-17 20:46:41 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-17 20:46:41 +00:00
|
|
|
def build_asset(collection)
|
|
|
|
collection.assets.build(:name => 'Asset on steroids', :description => 'Lorem ipsum', :active => true)
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
|
|
|
end
|