Sha256: 83a76b285cc4ed57ee48c34e1202fcd14dd90cc05c43aced2586eceb1b4ed57c

Contents?: true

Size: 1.55 KB

Versions: 6

Compression:

Stored size: 1.55 KB

Contents

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

  def initialize name, format, options
    @name = name
    @format = format
    @options = options
    @attr_options = if self.format.boolean?
      {
        null: false,
        default: false
      }
    else
      {}
    end
  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

6 entries across 6 versions & 1 rubygems

Version Path
honey-cms-0.3.8 lib/cms/attribute.rb
honey-cms-0.3.7 lib/cms/attribute.rb
honey-cms-0.3.5 lib/cms/attribute.rb
honey-cms-0.3.4 lib/cms/attribute.rb
honey-cms-0.3.3 lib/cms/attribute.rb
honey-cms-0.3.2 lib/cms/attribute.rb