class AppKit::Resource

The resource class manages a model and contains most of the functionality for dynamically interacting with the model. It also contains all the settings given in the DSL. The resource DSL is evaluated within this class.

Attributes

before_actions[W]

A list of before_actions defined in the DSL. These actions are executed in the controller before views are rendered. They should be blocks which are given an instance of the model.

This list should be managed from within the DSL and there should be no need to add things to it directly. See AppKit::Action.

controller_name[RW]

The name of the controller this resource manages.

editable_fields[W]
editors[W]
fields[W]

A list of fields defined by the DSL

formatters[W]
hidden_fields[W]
member_actions[W]
model[RW]

The model class that this resource manages.

navigation_icon[RW]

A string tha represents a icon used in the navigation menu. Taken from FontAwesome and is set using the icon DSL method.

Public Class Methods

find(model) click to toggle source

Looks up a resoruce by its model class.

Examples

AppKit::Resource.find(User)
# File lib/app_kit/resource.rb, line 30
def self.find(model)
  if model.is_a? Symbol
    return AppKit.application.resources.find{|r|
      r.model.model_name.name.underscore.to_sym == model
    }
  end
  AppKit.application.resources.find{|r| r.model == model}
end
new(model) click to toggle source

Initilization method

Attributes

  • model - The model class this resource will manage.

# File lib/app_kit/resource.rb, line 57
def initialize(model)
  @model = model
  AppKit.application.resources << self
  @navigation_position = :left
end

Public Instance Methods

actions_for_record?(record) click to toggle source
# File lib/app_kit/resource.rb, line 113
def actions_for_record?(record)
  member_actions.select{|name, a| a.enabled_for_record?(record)}.count > 0
end
before_actions() click to toggle source
# File lib/app_kit/resource.rb, line 50
def before_actions; @before_actions ||= {}; end
detail_fields() click to toggle source

A list of fields that should be displayed in the SHOW action or detail view All fields are included in this list by default, unless the display_in_detail option is set to false in the field options.

# File lib/app_kit/resource.rb, line 92
def detail_fields
  fields.each.select(&:show_in_details)
end
display_name() click to toggle source

Helper method for displaying the resource name. @returns A human readable string for the given resource @example A resource called against a UserOption model.

resource.display_name #=> "User option"
# File lib/app_kit/resource.rb, line 68
def display_name
  model.name.demodulize.underscore.humanize
end
editable_fields() click to toggle source

A list of all fields that can be displayed in the edit/create form. All fields are included in this list unless the editable option in the field configuration is set to false.

# File lib/app_kit/resource.rb, line 99
def editable_fields
  fields.each.select(&:editable)
end
fields() click to toggle source
# File lib/app_kit/resource.rb, line 41
def fields; @fields ||= []; end
filter_fields() click to toggle source

A list of all fields that can be displayed in the filter panel.

# File lib/app_kit/resource.rb, line 104
def filter_fields
  fields.each.select(&:show_in_filters)
end
has_many_associations() click to toggle source

A list of all has_many associations the model has.

# File lib/app_kit/resource.rb, line 109
def has_many_associations
  model.reflect_on_all_associations.select{|a| a.macro == :has_many unless a.name == :versions }
end
member_actions() click to toggle source
# File lib/app_kit/resource.rb, line 23
def member_actions; @member_actions ||= {}; end
plural_display_name() click to toggle source

Helper method for displaying the resource name. @returns A human readable plural string for the given resource @example A resource called against a UserOption model.

resource.display_name #=> "User options"
# File lib/app_kit/resource.rb, line 77
def plural_display_name
  display_name.pluralize
end
table_fields() click to toggle source

A list of the fields that should be displayed in tables. All fields by default are included in this list, unless the display_in_table option is set to false. @return [array] An of Field objects with display_in_table set to true

# File lib/app_kit/resource.rb, line 85
def table_fields
  fields.each.select(&:show_in_table)
end