Class: AppKit::Field
- Inherits:
-
Object
- Object
- AppKit::Field
- Defined in:
- lib/app_kit/field.rb
Overview
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/yyformat -
:datetime- Formats tom/d/yyhh: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, andeditableoptions 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.
Instance Attribute Summary collapse
-
#association ⇒ Object
The association object for this field if it is a foreign_key.
-
#data_type ⇒ Object
The data_type of this field.
- #display_name(val = nil) ⇒ Object
-
#display_proc ⇒ Object
a proc used to display the value of the field.
- #editable(val = nil) ⇒ Object
- #editor(val = nil) ⇒ Object
- #enum(value = nil) ⇒ Object
-
#foreign_key ⇒ Object
A boolean value indicating if this field is the foreign_key of an association.
- #formatter(val = nil) ⇒ Object
-
#name ⇒ Object
A symbol which relates to the method on the model this field gets it’s value from.
-
#show_in_details(val = nil) ⇒ Object
DSL option displayed in the details panel of the #show action.
-
#show_in_filters(val = nil) ⇒ Object
DSL Option displayed in the filter panel.
-
#show_in_table(val = nil) ⇒ Object
DSL option in tables.
-
#sort_field(val = nil) ⇒ Object
DSL method for setting the sort name for this field.
Instance Method Summary collapse
-
#associated_record ⇒ Object
Retrieves the assoicated record for this field.
-
#editor_options ⇒ Object
genereated options for editor fields.
-
#hide(val = true) ⇒ Object
A conveinence method for completely hiding a field from the UI.
-
#initialize(model, name, options = {}, &block) ⇒ Field
constructor
This class is generally created by the Resource DSL and it should not need to be created directly.
-
#is_foreign_key? ⇒ Boolean
Determins if the field is the foreign_key of an association.
- #value_for_record(record) ⇒ Object
Constructor Details
#initialize(model, name, options = {}, &block) ⇒ Field
This class is generally created by the Resource DSL and it should not need to be created directly.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/app_kit/field.rb', line 117 def initialize(model, name, ={}, &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 .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 |
Instance Attribute Details
#association ⇒ Object
The association object for this field if it is a foreign_key
40 41 42 |
# File 'lib/app_kit/field.rb', line 40 def association @association end |
#data_type ⇒ Object
The data_type of this field. Retrieved from the column definition.
38 39 40 |
# File 'lib/app_kit/field.rb', line 38 def data_type @data_type end |
#display_name(val = nil) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/app_kit/field.rb', line 93 def display_name(val = nil) if val != nil @display_name = val else @display_name end end |
#display_proc ⇒ Object
a proc used to display the value of the field. For dynamic virtual fields defined with a block
110 111 112 |
# File 'lib/app_kit/field.rb', line 110 def display_proc @display_proc end |
#editable(val = nil) ⇒ Object
49 50 51 52 |
# File 'lib/app_kit/field.rb', line 49 def editable(val=nil) return @editable if val.nil? @editable = val end |
#editor(val = nil) ⇒ Object
103 104 105 106 |
# File 'lib/app_kit/field.rb', line 103 def editor(val=nil) return @editor if val.nil? @editor = val end |
#enum(value = nil) ⇒ Object
145 146 147 148 149 150 151 152 |
# File 'lib/app_kit/field.rb', line 145 def enum(value = nil) if value != nil editor :enum @enum = value else return @enum end end |
#foreign_key ⇒ Object
A boolean value indicating if this field is the foreign_key of an association
36 37 38 |
# File 'lib/app_kit/field.rb', line 36 def foreign_key @foreign_key end |
#formatter(val = nil) ⇒ Object
57 58 59 60 |
# File 'lib/app_kit/field.rb', line 57 def formatter(val=nil) return @formatter if val.nil? @formatter = val end |
#name ⇒ Object
A symbol which relates to the method on the model this field gets it’s value from.
34 35 36 |
# File 'lib/app_kit/field.rb', line 34 def name @name end |
#show_in_details(val = nil) ⇒ Object
DSL option displayed in the details panel of the #show action.
76 77 78 79 |
# 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 |
#show_in_filters(val = nil) ⇒ Object
DSL Option displayed in the filter panel
85 86 87 88 |
# 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 |
#show_in_table(val = nil) ⇒ Object
DSL option in tables.
66 67 68 69 |
# 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 |
#sort_field(val = nil) ⇒ Object
DSL method for setting the sort name for this field.
42 43 44 |
# File 'lib/app_kit/field.rb', line 42 def sort_field @sort_field end |
Instance Method Details
#associated_record ⇒ Object
Retrieves the assoicated record for this field. If it is a foreign_key.
172 173 174 |
# File 'lib/app_kit/field.rb', line 172 def associated_record assoication.active_record end |
#editor_options ⇒ Object
genereated options for editor fields
136 137 138 139 140 141 142 |
# File 'lib/app_kit/field.rb', line 136 def = { :label => display_name, :as => editor } if enum [:enum] = enum end end |
#hide(val = true) ⇒ Object
A conveinence method for completely hiding a field from the UI.
178 179 180 181 182 |
# File 'lib/app_kit/field.rb', line 178 def hide(val = true) show_in_details !val show_in_table !val editable !val end |
#is_foreign_key? ⇒ Boolean
Determins if the field is the foreign_key of an association.
166 167 168 |
# File 'lib/app_kit/field.rb', line 166 def is_foreign_key? association.present? end |
#value_for_record(record) ⇒ Object
154 155 156 157 158 159 160 161 162 |
# 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 |