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
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
.
The name of the controller this resource manages.
A list of fields defined by the DSL
The model class that this resource manages.
Public Class Methods
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
Public Instance Methods
# 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
# File lib/app_kit/resource.rb, line 50 def before_actions; @before_actions ||= {}; end
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
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
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
# File lib/app_kit/resource.rb, line 41 def fields; @fields ||= []; end
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
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
# File lib/app_kit/resource.rb, line 23 def member_actions; @member_actions ||= {}; end
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
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