Class: Field
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Field
- Defined in:
- app/models/fields/field.rb
Overview
Copyright © 2008-2013 Michael Dvorkin and contributors.
Fat Free CRM is freely distributable under the terms of MIT license. See MIT-LICENSE file or www.opensource.org/licenses/mit-license.php
Schema Information
Table name: fields
id :integer not null, primary key
type :string(255)
field_group_id :integer
position :integer
pair_id :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
Constant Summary collapse
- BASE_FIELD_TYPES =
{ 'string' => {:klass => 'CustomField', :type => 'string'}, 'text' => {:klass => 'CustomField', :type => 'text'}, 'email' => {:klass => 'CustomField', :type => 'string'}, 'url' => {:klass => 'CustomField', :type => 'string'}, 'tel' => {:klass => 'CustomField', :type => 'string'}, 'select' => {:klass => 'CustomField', :type => 'string'}, 'radio' => {:klass => 'CustomField', :type => 'string'}, 'check_boxes' => {:klass => 'CustomField', :type => 'text'}, 'boolean' => {:klass => 'CustomField', :type => 'boolean'}, 'date' => {:klass => 'CustomField', :type => 'date'}, 'datetime' => {:klass => 'CustomField', :type => 'timestamp'}, 'decimal' => {:klass => 'CustomField', :type => 'decimal', :column_options => {:precision => 15, :scale => 2} }, 'integer' => {:klass => 'CustomField', :type => 'integer'}, 'float' => {:klass => 'CustomField', :type => 'float'} }.with_indifferent_access
Class Method Summary collapse
-
.field_types ⇒ Object
Provides access to registered field_types ——————————————————————————.
-
.lookup_class(as) ⇒ Object
Returns class name given a key ——————————————————————————.
-
.register(options) ⇒ Object
Register custom fields so they are available to the rest of the app Example options: :as => ‘datepair’, :type => ‘date’, :klass => ‘CustomFieldDatePair’ ——————————————————————————.
Instance Method Summary collapse
- #collection_string ⇒ Object
- #collection_string=(value) ⇒ Object
- #column_type(field_type = self.as) ⇒ Object
- #input_options ⇒ Object
- #render(value) ⇒ Object
- #render_value(object) ⇒ Object
Class Method Details
.field_types ⇒ Object
Provides access to registered field_types
108 109 110 |
# File 'app/models/fields/field.rb', line 108 def field_types @@field_types ||= BASE_FIELD_TYPES end |
.lookup_class(as) ⇒ Object
Returns class name given a key
123 124 125 |
# File 'app/models/fields/field.rb', line 123 def lookup_class(as) (@@field_types ||= BASE_FIELD_TYPES)[as][:klass] end |
.register(options) ⇒ Object
Register custom fields so they are available to the rest of the app Example options: :as => ‘datepair’, :type => ‘date’, :klass => ‘CustomFieldDatePair’
115 116 117 118 119 |
# File 'app/models/fields/field.rb', line 115 def register() opts = .dup as = .delete(:as) (@@field_types ||= BASE_FIELD_TYPES).merge!(as => ) end |
Instance Method Details
#collection_string ⇒ Object
79 80 81 |
# File 'app/models/fields/field.rb', line 79 def collection_string collection.try(:join, "|") end |
#collection_string=(value) ⇒ Object
76 77 78 |
# File 'app/models/fields/field.rb', line 76 def collection_string=(value) self.collection = value.split("|").map(&:strip).reject(&:blank?) end |
#column_type(field_type = self.as) ⇒ Object
65 66 67 |
# File 'app/models/fields/field.rb', line 65 def column_type(field_type = self.as) (opts = Field.field_types[field_type]) ? opts[:type] : raise("Unknown field_type: #{field_type}") end |
#input_options ⇒ Object
69 70 71 72 73 74 |
# File 'app/models/fields/field.rb', line 69 def input_html = {} attributes.reject { |k,v| !%w(as collection disabled label placeholder required maxlength).include?(k) or v.blank? }.symbolize_keys.merge(input_html) end |
#render(value) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/models/fields/field.rb', line 87 def render(value) case as when 'checkbox' value.to_s == '0' ? "no" : "yes" when 'date' value && value.strftime(I18n.t("date.formats.mmddyy")) when 'datetime' value && value.in_time_zone.strftime(I18n.t("time.formats.mmddyyyy_hhmm")) when 'check_boxes' 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
83 84 85 |
# File 'app/models/fields/field.rb', line 83 def render_value(object) render object.send(name) end |