Class: Field

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/fields/field.rb

Overview

Schema Information

Table name: fields

id             :integer         not null, primary key
type           :string(255)
field_group_id :integer
position       :integer
name           :string(64)
label          :string(128)
hint           :string(255)
placeholder    :string(255)
as             :string(32)
collection     :text
disabled       :boolean
required       :boolean
maxlength      :integer
created_at     :datetime
updated_at     :datetime

Direct Known Subclasses

CoreField, CustomField

Constant Summary collapse

KLASSES =
[Campaign, Lead, Contact, Account, Opportunity]
FIELD_TYPES =
{
  'string'      => :string,
  'text'        => :text,
  'email'       => :string,
  'url'         => :string,
  'tel'         => :string,
  'select'      => :string,
  'radio'       => :string,
  'check_boxes' => :text,
  'boolean'     => :boolean,
  'date'        => :date,
  'datetime'    => :timestamp,
  'decimal'     => [:decimal, {:precision => 15, :scale => 2}],
  'integer'     => :integer,
  'float'       => :float
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.field_typesObject



76
77
78
79
80
81
82
83
# File 'app/models/fields/field.rb', line 76

def self.field_types
  # Expands concise FIELD_TYPES into a more usable hash
  @field_types ||= FIELD_TYPES.inject({}) do |hash, n|
    arr = [n[1]].flatten
    hash[n[0]] = {:type => arr[0], :options => arr[1]}
    hash
  end
end

Instance Method Details

#collection_stringObject



100
101
102
# File 'app/models/fields/field.rb', line 100

def collection_string
  collection.try(:join, "|")
end

#collection_string=(value) ⇒ Object



97
98
99
# File 'app/models/fields/field.rb', line 97

def collection_string=(value)
  self.collection = value.split("|").map(&:strip).reject(&:blank?)
end

#column_type(field_type = self.as) ⇒ Object



85
86
87
# File 'app/models/fields/field.rb', line 85

def column_type(field_type = self.as)
  (opts = Field.field_types[field_type]) ? opts[:type] : raise("Unknown field_type: #{field_type}")
end

#input_optionsObject



89
90
91
92
93
94
95
# File 'app/models/fields/field.rb', line 89

def input_options
  input_html = {:maxlength => attributes[:maxlength]}

  attributes.reject { |k,v|
    !%w(as collection disabled label placeholder required).include?(k) or v.blank?
  }.symbolize_keys.merge(:input_html => input_html)
end

#render(value) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/fields/field.rb', line 108

def render(value)
  case as
  when 'checkbox'
    value.to_s == '0' ? "no" : "yes"
  when 'date'
    value && value.strftime(I18n.t("date.formats.default"))
  when 'datetime'
    value && value.strftime(I18n.t("time.formats.long"))
  when 'check_boxes'
    value = YAML.load(value) if value.is_a?(String)
    value.select(&:present?).in_groups_of(2, false).map {|g| g.join(', ')}.join("<br />".html_safe) if Array === value
  else
    value.to_s
  end
end

#render_value(object) ⇒ Object



104
105
106
# File 'app/models/fields/field.rb', line 104

def render_value(object)
  render object.send(name)
end