Sha256: 71e8c5b53b2d2004a3eaef1873a2fcf18f3c0866954a20bbd6ed9a1558318bdc

Contents?: true

Size: 1.47 KB

Versions: 10

Compression:

Stored size: 1.47 KB

Contents

class CMS::Attribute
  attr_accessor :name, :format, :options

  def initialize name, format, options
    @name = name
    @format = format
    @options = options
    @attr_options = {
      # null: false
    }
  end

  def format
    @format_enquirer ||= ActiveSupport::StringInquirer.new(@format)
  end

  def reference? ; format.reference? end
  def orderable? ; format.orderable? end
  def file?      ; format.file? end

  def has_index?
    false
  end

  def has_uniq_index?
    false
  end

  def index_name
    format.reference? ? "#{name}_id" : name
  end

  def migration_type
    if format.reference?
      'belongs_to'
    elsif format.orderable?
      'integer'
    elsif format.file?
      'string'
    elsif format.html?
      'text'
    else
      format
    end
  end

  def form_type
    if format.orderable? || format.reference?
      'select'
    elsif format.text? or format.html?
      'text_area'
    elsif format.file?
      'file_field'
    elsif format.boolean?
      'check_box'
    else
      'text_field'
    end
  end

  def field_name
    if reference?
      name + '_id'
    else
      name
    end
  end

  def inject_options
    "".tap { |s| @attr_options.each { |k,v| s << ", :#{k} => #{v.inspect}" } }
  end

  def inject_index_options
    has_uniq_index? ? ", :unique => true" : ''
  end

  def reference_to
    if reference?
      @reference_to ||= CMS::Configuration.types.find{|t| t.name == @options['reference_to']}
    end
  end

  def to_s
    name
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
honey-cms-0.3.1 lib/cms/attribute.rb
honey-cms-0.3.0 lib/cms/attribute.rb
honey-cms-0.2.4 lib/cms/attribute.rb
honey-cms-0.2.3 lib/cms/attribute.rb
honey-cms-0.2.2 lib/cms/attribute.rb
honey-cms-0.2.1 lib/cms/attribute.rb
honey-cms-0.2.0 lib/cms/attribute.rb
honey-cms-0.1.2 lib/cms/attribute.rb
honey-cms-0.1.1 lib/cms/attribute.rb
honey-cms-0.1.0 lib/cms/attribute.rb