app/models/fields/field.rb in fat_free_crm-0.11.3 vs app/models/fields/field.rb in fat_free_crm-0.11.4
- old
+ new
@@ -21,10 +21,11 @@
#
# 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)
@@ -38,62 +39,52 @@
class Field < ActiveRecord::Base
acts_as_list
serialize :collection, Array
+ serialize :settings, HashWithIndifferentAccess
belongs_to :field_group
+
+ scope :core_fields, where(:type => 'CoreField')
+ scope :custom_fields, where("type != 'CoreField'")
+ scope :without_pairs, where(:pair_id => nil)
delegate :klass, :klass_name, :klass_name=, :to => :field_group
- KLASSES = [Campaign, Lead, Contact, Account, Opportunity]
+ 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
- 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
- }
-
- validates_presence_of :label, :message => "^Please enter a Field label."
- validates_length_of :label, :maximum => 64, :message => "^The Field name must be less than 64 characters in length."
-
+ validates_presence_of :label, :message => "^Please enter a field label."
+ validates_length_of :label, :maximum => 64, :message => "^The field name must be less than 64 characters in length."
validates_numericality_of :maxlength, :only_integer => true, :allow_blank => true, :message => "^Max size can only be whole number."
+ validates_presence_of :as, :message => "^Please specify a field type."
+ validates_inclusion_of :as, :in => Proc.new{self.field_types.keys}, :message => "^Invalid field type.", :allow_blank => true
- validates_presence_of :as, :message => "^Please specify a Field type."
- validates_inclusion_of :as, :in => FIELD_TYPES.keys, :message => "Invalid Field Type."
-
-
- 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
-
def column_type(field_type = self.as)
(opts = Field.field_types[field_type]) ? opts[:type] : raise("Unknown field_type: #{field_type}")
end
def input_options
- input_html = {:maxlength => attributes[:maxlength]}
-
+ input_html = {}
attributes.reject { |k,v|
- !%w(as collection disabled label placeholder required).include?(k) or v.blank?
- }.symbolize_keys.merge(:input_html => input_html)
+ !%w(as collection disabled label placeholder required maxlength).include?(k) or v.blank?
+ }.symbolize_keys.merge(input_html)
end
def collection_string=(value)
self.collection = value.split("|").map(&:strip).reject(&:blank?)
end
@@ -108,17 +99,43 @@
def render(value)
case as
when 'checkbox'
value.to_s == '0' ? "no" : "yes"
when 'date'
- value && value.strftime(I18n.t("date.formats.default"))
+ value && value.strftime(I18n.t("date.formats.mmddyy"))
when 'datetime'
- value && value.strftime(I18n.t("time.formats.long"))
+ value && value.strftime(I18n.t("time.formats.mmddyyyy_hhmm"))
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
-end
+ protected
+
+ class << self
+
+ # Provides access to registered field_types
+ #------------------------------------------------------------------------------
+ def field_types
+ @@field_types ||= BASE_FIELD_TYPES
+ end
+
+ # Register custom fields so they are available to the rest of the app
+ # Example options: :as => 'datepair', :type => 'date', :klass => 'CustomFieldDatePair'
+ #------------------------------------------------------------------------------
+ def register(options)
+ opts = options.dup
+ as = options.delete(:as)
+ (@@field_types ||= BASE_FIELD_TYPES).merge!(as => options)
+ end
+
+ # Returns class name given a key
+ #------------------------------------------------------------------------------
+ def lookup_class(as)
+ (@@field_types ||= BASE_FIELD_TYPES)[as][:klass]
+ end
+
+ end
+
+end