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

@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

association[RW]

The association object for this field if it is a foreign_key

data_type[RW]

The data_type of this field. Retrieved from the column definition.

display_name[W]

The display name for this field. By default it will be a humanized form of the method name.

display_proc[RW]

a proc used to display the value of the field. For dynamic virtual fields defined with a block

editable[W]

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.

editor[W]

DSL option - The editor to use when editing this field.

enum[W]
foreign_key[RW]

A boolean value indicating if this field is the foreign_key of an association

formatter[W]

DSL option for setting the formatting used for a field. @param val [Symbol]

name[RW]

A symbol which relates to the method on the model this field gets it’s value from.

show_in_details[W]
show_in_filters[W]
show_in_table[W]
sort_field[RW]

A custom field to sort by.

Public Class Methods

new(model, name, options={}, &block) click to toggle source

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

associated_record() click to toggle source

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
display_name(val = nil) click to toggle source
# File lib/app_kit/field.rb, line 93
def display_name(val = nil)
  if val != nil
    @display_name = val
  else
    @display_name
  end
end
editable(val=nil) click to toggle source
# File lib/app_kit/field.rb, line 49
def editable(val=nil)
  return @editable if val.nil?
  @editable = val
end
editor(val=nil) click to toggle source
# File lib/app_kit/field.rb, line 103
def editor(val=nil)
  return @editor if val.nil?
  @editor = val
end
editor_options() click to toggle source

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
enum(value = nil) click to toggle source
# File lib/app_kit/field.rb, line 145
def enum(value = nil)
  if value != nil
    editor :enum
    @enum = value
  else
    return @enum
  end
end
formatter(val=nil) click to toggle source
# File lib/app_kit/field.rb, line 57
def formatter(val=nil)
  return @formatter if val.nil?
  @formatter = val
end
hide(val = true) click to toggle source

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
is_foreign_key?() click to toggle source

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
show_in_details(val=nil) click to toggle source

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
show_in_filters(val=nil) click to toggle source

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
show_in_table(val = nil) click to toggle source

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
value_for_record(record) click to toggle source
# 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