remove references from CustomField proof of concept

This commit is contained in:
dinedine 2010-05-21 02:41:33 +02:00
parent e09e881bbe
commit 10ffc2e8bb
4 changed files with 12 additions and 80 deletions

View File

@ -6,20 +6,14 @@ class AssetCollection
## fields ## ## fields ##
field :name, :type => String field :name, :type => String
field :slug, :type => String field :slug, :type => String
# field :asset_fields_counter, :type => Integer, :default => 0 # FIXME (custom fields)
## associations ## ## associations ##
belongs_to_related :site belongs_to_related :site
embeds_many :assets #, :custom_fields => true # FIXME (custom fields) embeds_many :assets
# embeds_many :asset_fields # FIXME (custom fields)
## behaviours ## ## behaviours ##
custom_fields_for :assets custom_fields_for :assets
# accepts_nested_attributes_for :asset_fields, :allow_destroy => true # FIXME (custom fields)
## callbacks ## ## callbacks ##
before_validate :normalize_slug before_validate :normalize_slug
before_save :store_asset_positions! before_save :store_asset_positions!
@ -42,8 +36,8 @@ class AssetCollection
@assets_order = order @assets_order = order
end end
def ordered_asset_fields # FIXME (custom fields) def ordered_asset_custom_fields # FIXME (custom fields)
self.asset_fields.sort { |a, b| (a.position || 0) <=> (b.position || 0) } self.asset_custom_fields.sort { |a, b| (a.position || 0) <=> (b.position || 0) }
end end
protected protected

View File

@ -1,62 +0,0 @@
class AssetField
include Mongoid::Document
include Mongoid::Timestamps
## fields ##
field :label, :type => String
field :_alias, :type => String # need it for instance in: > asset.description (description being a custom field)
field :_name, :type => String
field :kind, :type => String
field :position, :type => Integer, :default => 0
## validations ##
validates_presence_of :label, :kind
embedded_in :asset_collection, :inverse_of => :asset_custom_fields
## methods ##
def field_type
case self.kind
when 'String', 'Text', 'Email' then String
else
self.kind.constantize
end
end
def apply(object, association_name)
# puts "applying...#{self._name} / #{self._alias}"
object.class.send(:set_field, self._name, { :type => self.field_type })
object.class_eval <<-EOF
alias :#{self.safe_alias} :#{self._name}
alias :#{self.safe_alias}= :#{self._name}=
EOF
end
def safe_alias
self.set_alias
self._alias
end
protected
def set_unique_name!
self._name = "custom_field_#{self.increment_counter!}"
end
def set_alias
return if self.label.blank? && self._alias.blank?
self._alias ||= self.label.clone
self._alias.slugify!(:downcase => true, :underscore => true)
end
def increment_counter!
next_value = (self._parent.send(:"#{self.association_name}_counter") || 0) + 1
self._parent.send(:"#{self.association_name}_counter=", next_value)
next_value
end
def siblings
self._parent.associations[self.association_name]
end
end

View File

@ -1,6 +1,6 @@
= f.foldable_inputs :name => :custom_fields, :class => 'editable-list fields off' do = f.foldable_inputs :name => :custom_fields, :class => 'editable-list fields off' do
- f.object.ordered_asset_fields.each do |field| - f.object.ordered_asset_custom_fields.each do |field|
= f.fields_for :asset_fields, field, :child_index => field._index do |g| = f.fields_for :asset_custom_fields, field, :child_index => field._index do |g|
%li{ :class => "item added #{'error' unless field.errors.empty?}"} %li{ :class => "item added #{'error' unless field.errors.empty?}"}
%span.handle %span.handle
= image_tag 'admin/form/icons/drag.png' = image_tag 'admin/form/icons/drag.png'
@ -20,7 +20,7 @@
%span.actions %span.actions
= link_to image_tag('admin/form/icons/trash.png'), '#', :class => 'remove first', :confirm => t('admin.messages.confirm') = link_to image_tag('admin/form/icons/trash.png'), '#', :class => 'remove first', :confirm => t('admin.messages.confirm')
= f.fields_for :asset_fields, @collection.asset_fields.build(:label => 'field name'), :child_index => '-1' do |g| = f.fields_for :asset_custom_fields, @collection.asset_custom_fields.build(:label => 'field name'), :child_index => '-1' do |g|
%li{ :class => 'item template' } %li{ :class => 'item template' }
%span.handle %span.handle
= image_tag 'admin/form/icons/drag.png' = image_tag 'admin/form/icons/drag.png'

View File

@ -51,11 +51,11 @@ x domain scoping when authenticating
- remove old files if new one - remove old files if new one
- custom fields: - custom fields:
x renaming fields x renaming fields
- ui x extract a plugin from custom fields
- rename asset_field x ui
x field position
x rename asset_field
- apply in asset_field - apply in asset_field
- extract a plugin from custom fields
- field position
- nested attributes - nested attributes
- keep tracks of all custom fields (adding / editing assets) - keep tracks of all custom fields (adding / editing assets)
- custom fields -> metadata keys - custom fields -> metadata keys