class AppKit::Field
The field class manages individual attributes on a model.
Fields DSL¶ ↑
Fields are defined inside resource DSL. A field must have a name given by a symbol which relates to a specific method on the model class and can take a variety of options to configure the way it is handled.
Field
Options¶ ↑
-
editable
- If set to false a fieild will not be visible in the create/edit forms and it will not be whitelisted -
formatter
- A symbol for the fomatter to use when displaying this field.-
:currency
- Formats using ActiveSupports number_to_currency method. -
:integer
- Formats to humanized numbers. -
:date
- Formats tom/d/yy
format -
:datetime
- Formats tom/d/yy
hh:mm:ss -
:string
- Returns the value unmodified.
The default formatter for a given field is determened by its data type.
-
-
show_in_table
- If set to false the field will not appear in tabular lists of the records. -
show_in_detail
- If set to false the field will not appear in the key/value details view in the show action. -
hide
- A convienence method. If set to false thedisplay_in_table
,display_in_detail
, andeditable
options will all be set to false. -
editor
- The editor to use when this field is displayed on in the edit/create forms. The default editor is selected based on the field’s data type.
@example DSL inline options
field :total_amount, :formatter => :currency, editable: false, display_in_table: false
@example DSL block options
field :total_amount do formatter :currency editable false display_in_table false end
Attributes
The association object for this field if it is a foreign_key
The data_type
of this field. Retrieved from the column definition.
The display name for this field. By default it will be a humanized form of the method name.
a proc used to display the value of the field. For dynamic virtual fields defined with a block
DSL option for flagging the field as editable or not. @param val [Boolean] if set to false the field will not be displayed in the edit/create form.
DSL option - The editor to use when editing this field.
A boolean value indicating if this field is the foreign_key
of an association
DSL option for setting the formatting used for a field. @param val [Symbol]
A symbol which relates to the method on the model this field gets it’s value from.
A custom field to sort by.
Public Class Methods
This class is generally created by the Resource
DSL and it should not need to be created directly. @param model [ActiveRecord::Base] The model this field belongs to. @param name [Symbol] The method this field gets it’s value from. @param options [Hash] The options which should be set for this field.
# File lib/app_kit/field.rb, line 117 def initialize(model, name, options={}, &block) @name = name @data_type = model.columns_hash[name.to_s].try(:type) || :string @show_in_details = true @show_in_table = true @show_in_filters = true @editable = true @formatter = data_type @display_name = name.to_s.humanize options.each {|k,v| send(k,v) } if block_given? @editable = false @show_in_filters = false @display_proc = block end @association = model.reflect_on_all_associations.find{|i| i.foreign_key.to_sym == name} end
Public Instance Methods
Retrieves the assoicated record for this field. If it is a foreign_key. @return An active record object on the otherside of the association.
# File lib/app_kit/field.rb, line 172 def associated_record assoication.active_record end
# File lib/app_kit/field.rb, line 93 def display_name(val = nil) if val != nil @display_name = val else @display_name end end
# File lib/app_kit/field.rb, line 49 def editable(val=nil) return @editable if val.nil? @editable = val end
# File lib/app_kit/field.rb, line 103 def editor(val=nil) return @editor if val.nil? @editor = val end
genereated options for editor fields
# File lib/app_kit/field.rb, line 136 def editor_options options = { :label => display_name, :as => editor } if enum options[:enum] = enum end options end
# File lib/app_kit/field.rb, line 145 def enum(value = nil) if value != nil editor :enum @enum = value else return @enum end end
# File lib/app_kit/field.rb, line 57 def formatter(val=nil) return @formatter if val.nil? @formatter = val end
A conveinence method for completely hiding a field from the UI. @param val [Boolean] If true the item will not be displayed in tables, details, and will not be editable.
# File lib/app_kit/field.rb, line 178 def hide(val = true) show_in_details !val show_in_table !val editable !val end
Determins if the field is the foreign_key
of an association. @returns Boolean
# File lib/app_kit/field.rb, line 166 def is_foreign_key? association.present? end
DSL option @param val [Boolean] - If set to false the field will not be displayed in the details panel of the show action.
# File lib/app_kit/field.rb, line 76 def show_in_details(val=nil) return @show_in_details if val.nil? @show_in_details = val end
DSL Option @param val [Boolean] - If set to false the field will not be displayed in the filter panel
# File lib/app_kit/field.rb, line 85 def show_in_filters(val=nil) return @show_in_filters if val.nil? @show_in_filters = val end
DSL option @param val [Boolean] - If set to false the item will not appear in tables.
# File lib/app_kit/field.rb, line 66 def show_in_table(val = nil) return @show_in_table if val.nil? @show_in_table = val end
# File lib/app_kit/field.rb, line 154 def value_for_record(record) if @display_proc @display_proc.call(record) elsif @enum @enum[record.send(name).to_sym] else record.send(name) end end