Class: AppKit::Field

Inherits:
Object
  • Object
show all
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 to m/d/yy format

    • :datetime - Formats to m/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 the display_in_table, display_in_detail, and editable 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.

Examples:

DSL inline options

field :total_amount, :formatter => :currency, editable: false, display_in_table: false

DSL block options

field :total_amount do
    formatter :currency
    editable false
    display_in_table false
end

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • model (ActiveRecord::Base)

    The model this field belongs to.

  • name (Symbol)

    The method this field gets it’s value from.

  • options (Hash) (defaults to: {})

    The options which should be set for this field.



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, 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

Instance Attribute Details

#associationObject

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_typeObject

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_procObject

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_keyObject

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

#nameObject

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.

Parameters:

  • val (Boolean) (defaults to: nil)
    • If set to false the field will not be



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

Parameters:

  • val (Boolean) (defaults to: nil)
    • If set to false the field will not be



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.

Parameters:

  • val (Boolean) (defaults to: nil)
    • If set to false the item will not appear



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.

Parameters:

  • val (Symbol) (defaults to: nil)

    The name of a column to sort this field by.



42
43
44
# File 'lib/app_kit/field.rb', line 42

def sort_field
  @sort_field
end

Instance Method Details

#associated_recordObject

Retrieves the assoicated record for this field. If it is a foreign_key.

Returns:

  • An active record object on the otherside of the association.



172
173
174
# File 'lib/app_kit/field.rb', line 172

def associated_record
  assoication.active_record
end

#editor_optionsObject

genereated options for editor fields



136
137
138
139
140
141
142
# 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

#hide(val = true) ⇒ Object

A conveinence method for completely hiding a field from the UI.

Parameters:

  • val (Boolean) (defaults to: true)

    If true the item will not be displayed in tables, details, and will not be editable.



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.

Returns:

  • (Boolean)


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