Sha256: 3bc14aeea073b96a7abdc6feda8be21859dfbc8d040d44a4a9f09d1d0f82594b

Contents?: true

Size: 1.88 KB

Versions: 7

Compression:

Stored size: 1.88 KB

Contents

module CustomFields

  class Field
    include ::Mongoid::Document
    include ::Mongoid::Timestamps
    
    # types ##
    include Types::Default
    include Types::String
    include Types::Text
    include Types::Category
    include Types::Boolean
    include Types::Date
    include Types::File
  
    ## fields ##
    field :label
    field :_alias
    field :_name
    field :kind
    field :hint
    field :position, :type => Integer, :default => 0
      
    ## validations ##
    validates_presence_of :label, :kind
    validate :uniqueness_of_label
      
    ## methods ##
    
    def field_type
      self.class.field_types[self.kind.downcase.to_sym]
    end
    
    def apply(klass)
      return unless self.valid?
      
      klass.field self._name, :type => self.field_type if self.field_type
      
      apply_method_name = :"apply_#{self.kind.downcase}_type"
      
      if self.respond_to?(apply_method_name)
        self.send(apply_method_name, klass)
      else
        apply_default_type(klass)
      end
    end
    
    def safe_alias
      self.set_alias
      self._alias 
    end
  
    protected
    
    def uniqueness_of_label
      duplicate = self.siblings.detect { |f| f.label == self.label && f._id != self._id }
      if not duplicate.nil?
        self.errors.add(:label, :taken)
      end
    end
  
    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._alias.blank? ? self.label : self._alias).parameterize('_').downcase
    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.send(self.association_name)
    end
  end
  
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
custom_fields-1.0.0.beta2 lib/custom_fields/field.rb
custom_fields-1.0.0.beta lib/custom_fields/field.rb
custom_fields-0.0.0.1 lib/custom_fields/field.rb
locomotive_cms-0.0.3.3 vendor/plugins/custom_fields/lib/custom_fields/field.rb
locomotive_cms-0.0.3.1 vendor/plugins/custom_fields/lib/custom_fields/field.rb
locomotive_cms-0.0.2.9 vendor/plugins/custom_fields/lib/custom_fields/field.rb
locomotive_cms-0.0.2.8 vendor/plugins/custom_fields/lib/custom_fields/field.rb