first draft of custom fields

This commit is contained in:
dinedine 2010-05-18 00:51:53 +02:00
parent c15c635efe
commit 277b531449
5 changed files with 88 additions and 92 deletions

View File

@ -14,6 +14,9 @@ class AssetCollection
embeds_many :asset_fields # FIXME (custom fields) embeds_many :asset_fields # FIXME (custom fields)
## behaviours ##
accepts_nested_attributes_for :asset_fields # FIXME (custom fields)
## callbacks ## ## callbacks ##
before_validate :normalize_slug before_validate :normalize_slug
before_save :store_asset_positions! before_save :store_asset_positions!

View File

@ -8,15 +8,7 @@ class AssetField
field :_name, :type => String field :_name, :type => String
field :kind, :type => String field :kind, :type => String
field :position, :type => Integer, :default => 0 field :position, :type => Integer, :default => 0
## associations ##
embedded_in :collection, :class_name => 'AssetCollection', :inverse_of => :asset_fields
## callbacks ##
before_save :set_alias
# before_create :add_to_list_bottom => FIXME _index does the trick actually
# before_save :set_unique_name!
## validations ## ## validations ##
validates_presence_of :label, :kind validates_presence_of :label, :kind
@ -31,18 +23,20 @@ class AssetField
end end
def apply(object, association_name) def apply(object, association_name)
puts "applying...#{self._name} / #{self._alias}" # puts "applying...#{self._name} / #{self._alias}"
object.class.send(:set_field, self._name, { :type => self.field_type }) object.class.send(:set_field, self._name, { :type => self.field_type })
object.class_eval <<-EOF object.class_eval <<-EOF
alias :#{self._alias} :#{self._name} alias :#{self.safe_alias} :#{self._name}
alias :#{self.safe_alias}= :#{self._name}=
EOF EOF
end end
protected def safe_alias
self.set_alias
self._alias
end
# def add_to_list_bottom protected
# self.position = (self.siblings.map(&:position).max || 0) + 1
# end
def set_unique_name! def set_unique_name!
self._name = "custom_field_#{self.increment_counter!}" self._name = "custom_field_#{self.increment_counter!}"
@ -50,7 +44,6 @@ class AssetField
def set_alias def set_alias
return if self.label.blank? && self._alias.blank? return if self.label.blank? && self._alias.blank?
puts "set_alias !!!"
self._alias ||= self.label.clone self._alias ||= self.label.clone
self._alias.slugify!(:downcase => true, :underscore => true) self._alias.slugify!(:downcase => true, :underscore => true)
end end

View File

@ -25,6 +25,8 @@ module Mongoid #:nodoc:
alias_method_chain :build, :custom_field_settings alias_method_chain :build, :custom_field_settings
def target_custom_field_association? def target_custom_field_association?
return unless @association_name.ends_with?('_fields')
target_name = @association_name.gsub(/_fields$/, '').pluralize target_name = @association_name.gsub(/_fields$/, '').pluralize
# puts "target_name = #{target_name} / #{@parent.associations.key?(target_name).inspect} / #{@parent.inspect} / #{@parent.associations.inspect}" # puts "target_name = #{target_name} / #{@parent.associations.key?(target_name).inspect} / #{@parent.inspect} / #{@parent.associations.inspect}"
if @parent.associations.key?(target_name) if @parent.associations.key?(target_name)
@ -47,13 +49,13 @@ module Mongoid #:nodoc:
if self.custom_fields?(object, association_name) if self.custom_fields?(object, association_name)
# puts "custom fields = #{object.asset_fields.inspect}" # puts "custom fields = #{object.asset_fields.inspect}"
puts "((((((((" # puts "(((((((("
object.send(self.custom_fields_association_name(association_name)).each do |field| object.send(self.custom_fields_association_name(association_name)).each do |field|
# puts "field = #{field.inspect}" # puts "field = #{field.inspect}"
# self.class.send(:set_field, field.name, { :type => field.field_type }) # self.class.send(:set_field, field.name, { :type => field.field_type })
field.apply(self, association_name) field.apply(self, association_name)
end end
puts "))))))))" # puts "))))))))"
end end
end end

View File

@ -50,12 +50,13 @@ x domain scoping when authenticating
- assets uploader: - assets uploader:
- remove old files if new one - remove old files if new one
- custom fields: - custom fields:
- renaming fields x renaming fields
- ui - ui
- rename asset_field - rename asset_field
- apply in asset_field - apply in asset_field
- extract a plugin from custom fields - extract a plugin from custom fields
- field position - field position
- nested attributes
BACKLOG: BACKLOG:
- liquid rendering engine - liquid rendering engine

View File

@ -2,98 +2,95 @@ require 'spec_helper'
describe AssetCollection do describe AssetCollection do
it 'should have a valid factory' do # it 'should have a valid factory' do
Factory.build(:asset_collection).should be_valid # Factory.build(:asset_collection).should be_valid
end # end
describe 'custom fields (beta)' do describe 'custom fields (beta)' do
before(:each) do before(:each) do
@collection = Factory.build(:asset_collection) @collection = Factory.build(:asset_collection, :site => nil)
@collection.asset_fields.build :label => 'My Description', :_alias => 'description', :kind => 'Text' @collection.asset_fields.build :label => 'My Description', :_alias => 'description', :kind => 'Text'
@collection.asset_fields.build :label => 'Active', :kind => 'Boolean' @collection.asset_fields.build :label => 'Active', :kind => 'Boolean'
end end
context 'define attributes' do context 'define core attributes' do
# it 'should have an unique name' do it 'should have an unique name' do
# @collection.asset_fields.first._name.should == "custom_field_1" @collection.asset_fields.first._name.should == "custom_field_1"
# @collection.asset_fields.last._name.should == "custom_field_2" @collection.asset_fields.last._name.should == "custom_field_2"
# end end
it 'should have an unique alias' do it 'should have an unique alias' do
@collection.save
@collection.asset_fields.first._alias.should == "description" @collection.asset_fields.first._alias.should == "description"
@collection.asset_fields.last._alias.should == "active" @collection.asset_fields.last._alias.should == "active"
end end
# end
# it 'should define a position according to siblings'
context 'build and save' do
it 'should build asset' do
asset = @collection.assets.build
lambda {
asset.description
asset.active
}.should_not raise_error
end
it 'should assign values to newly built asset' do
asset = build_asset(@collection)
asset.description.should == 'Lorem ipsum'
asset.active.should == true
end
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
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
end end
# context 'build and save' do
#
# it 'should build asset' do
# asset = @collection.assets.build
# lambda {
# asset.description
# asset.active
# }.should_not raise_error
# end
#
# it 'should assign values to newly built asset' do
# asset = build_asset(@collection)
# asset.description.should == 'Lorem ipsum'
# asset.active.should == true
# end
#
# 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
#
# 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
#
# end
#
context 'modifying fields' do context 'modifying fields' do
# before(:each) do before(:each) do
# @asset = build_asset(@collection).save @asset = build_asset(@collection).save
# end end
#
# it 'should add new field' do
# @collection.asset_fields.build :label => 'Active at', :name => 'active_at', :kind => 'Date'
# @collection.save
#
# # puts "association = #{@collection.send(:associations)['assets'].options.inspect}"
#
# # @collection = AssetCollection.first
# # @collection.flush_cache(:asset_fields)
# @collection.reload
# # puts "# fields #{@collection.asset_fields.size.inspect}"
# # puts "============================"
# # puts "@collection asset fields ==> #{@collection.asset_fields.inspect}"
# asset = @collection.assets.first
# lambda { asset.active_at }.should_not raise_error
# end
# it 'should remove field' do it 'should add new field' do
# @collection.asset_fields.delete_all :name => 'active' @collection.asset_fields.build :label => 'Active at', :name => 'active_at', :kind => 'Date'
# @collection.save @collection.upsert(false)
# lambda { asset.active }.should raise_error @collection.reload
# end asset = @collection.assets.first
# lambda { asset.active_at }.should_not raise_error
# it 'should be able to change field name' end
it 'should remove field' do
@collection.asset_fields.clear
@collection.upsert(false)
@collection.reload
asset = @collection.assets.first
lambda { asset.active }.should raise_error
end
it 'should rename field label' do
@collection.asset_fields.first.label = 'Simple description'
@collection.asset_fields.first._alias = nil
@collection.upsert(false)
@collection.reload
asset = @collection.assets.first
asset.simple_description.should == 'Lorem ipsum'
end
end end