Class: AppKit::Resource
- Inherits:
-
Object
- Object
- AppKit::Resource
- Includes:
- Dsl::ResourceDsl
- Defined in:
- lib/app_kit/resource.rb
Overview
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.
Instance Attribute Summary collapse
- #before_actions ⇒ Object
-
#controller_name ⇒ Object
The name of the controller this resource manages.
-
#editable_fields ⇒ Object
A list of all fields that can be displayed in the edit/create form.
-
#editors ⇒ Object
writeonly
Sets the attribute editors.
- #fields ⇒ Object
-
#formatters ⇒ Object
writeonly
Sets the attribute formatters.
-
#hidden_fields ⇒ Object
writeonly
Sets the attribute hidden_fields.
- #member_actions ⇒ Object
-
#model ⇒ Object
The model class that this resource manages.
-
#navigation_icon ⇒ Object
A string tha represents a icon used in the navigation menu.
Class Method Summary collapse
-
.find(model) ⇒ Object
Looks up a resoruce by its model class.
Instance Method Summary collapse
- #actions_for_record?(record) ⇒ Boolean
-
#detail_fields ⇒ Object
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.
-
#display_name ⇒ Object
Helper method for displaying the resource name.
-
#filter_fields ⇒ Object
A list of all fields that can be displayed in the filter panel.
-
#has_many_associations ⇒ Object
A list of all has_many associations the model has.
-
#initialize(model) ⇒ Resource
constructor
Initilization method.
-
#plural_display_name ⇒ Object
Helper method for displaying the resource name.
-
#table_fields ⇒ array
A list of the fields that should be displayed in tables.
Methods included from Dsl::ResourceDsl
#action, #before, #field, #icon, #show_in_navigation
Constructor Details
#initialize(model) ⇒ Resource
Initilization method
Attributes
-
model- The model class this resource will manage.
57 58 59 60 61 |
# File 'lib/app_kit/resource.rb', line 57 def initialize(model) @model = model AppKit.application.resources << self = :left end |
Instance Attribute Details
#before_actions ⇒ Object
50 |
# File 'lib/app_kit/resource.rb', line 50 def before_actions; @before_actions ||= {}; end |
#controller_name ⇒ Object
The name of the controller this resource manages.
19 20 21 |
# File 'lib/app_kit/resource.rb', line 19 def controller_name @controller_name end |
#editable_fields ⇒ Object
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.
99 100 101 |
# File 'lib/app_kit/resource.rb', line 99 def editable_fields fields.each.select(&:editable) end |
#editors=(value) ⇒ Object (writeonly)
Sets the attribute editors
8 9 10 |
# File 'lib/app_kit/resource.rb', line 8 def editors=(value) @editors = value end |
#fields ⇒ Object
41 |
# File 'lib/app_kit/resource.rb', line 41 def fields; @fields ||= []; end |
#formatters=(value) ⇒ Object (writeonly)
Sets the attribute formatters
8 9 10 |
# File 'lib/app_kit/resource.rb', line 8 def formatters=(value) @formatters = value end |
#hidden_fields=(value) ⇒ Object (writeonly)
Sets the attribute hidden_fields
8 9 10 |
# File 'lib/app_kit/resource.rb', line 8 def hidden_fields=(value) @hidden_fields = value end |
#member_actions ⇒ Object
23 |
# File 'lib/app_kit/resource.rb', line 23 def member_actions; @member_actions ||= {}; end |
#model ⇒ Object
The model class that this resource manages.
12 13 14 |
# File 'lib/app_kit/resource.rb', line 12 def model @model end |
#navigation_icon ⇒ Object
A string tha represents a icon used in the navigation menu. Taken from FontAwesome and is set using the #icon DSL method.
16 17 18 |
# File 'lib/app_kit/resource.rb', line 16 def end |
Class Method Details
.find(model) ⇒ Object
Looks up a resoruce by its model class.
Examples
AppKit::Resource.find(User)
30 31 32 33 34 35 36 37 |
# 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 |
Instance Method Details
#actions_for_record?(record) ⇒ Boolean
113 114 115 |
# 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 |
#detail_fields ⇒ Object
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.
92 93 94 |
# File 'lib/app_kit/resource.rb', line 92 def detail_fields fields.each.select(&:show_in_details) end |
#display_name ⇒ Object
Helper method for displaying the resource name.
68 69 70 |
# File 'lib/app_kit/resource.rb', line 68 def display_name model.name.demodulize.underscore.humanize end |
#filter_fields ⇒ Object
A list of all fields that can be displayed in the filter panel.
104 105 106 |
# File 'lib/app_kit/resource.rb', line 104 def filter_fields fields.each.select(&:show_in_filters) end |
#has_many_associations ⇒ Object
A list of all has_many associations the model has.
109 110 111 |
# 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 |
#plural_display_name ⇒ Object
Helper method for displaying the resource name.
77 78 79 |
# File 'lib/app_kit/resource.rb', line 77 def plural_display_name display_name.pluralize end |
#table_fields ⇒ array
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.
85 86 87 |
# File 'lib/app_kit/resource.rb', line 85 def table_fields fields.each.select(&:show_in_table) end |