Class: AppKit::Resource

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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
  @navigation_position = :left
end

Instance Attribute Details

#before_actionsObject



50
# File 'lib/app_kit/resource.rb', line 50

def before_actions; @before_actions ||= {}; end

#controller_nameObject

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_fieldsObject

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

Parameters:

  • value

    the value to set the attribute editors to.



8
9
10
# File 'lib/app_kit/resource.rb', line 8

def editors=(value)
  @editors = value
end

#fieldsObject



41
# File 'lib/app_kit/resource.rb', line 41

def fields; @fields ||= []; end

#formatters=(value) ⇒ Object (writeonly)

Sets the attribute formatters

Parameters:

  • value

    the value to set the attribute formatters to.



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

Parameters:

  • value

    the value to set the attribute hidden_fields to.



8
9
10
# File 'lib/app_kit/resource.rb', line 8

def hidden_fields=(value)
  @hidden_fields = value
end

#member_actionsObject



23
# File 'lib/app_kit/resource.rb', line 23

def member_actions; @member_actions ||= {}; end

#modelObject

The model class that this resource manages.



12
13
14
# File 'lib/app_kit/resource.rb', line 12

def model
  @model
end

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 navigation_icon
  @navigation_icon
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

Returns:

  • (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_fieldsObject

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_nameObject

Helper method for displaying the resource name.

Examples:

A resource called against a UserOption model.

resource.display_name #=> "User option"


68
69
70
# File 'lib/app_kit/resource.rb', line 68

def display_name
  model.name.demodulize.underscore.humanize
end

#filter_fieldsObject

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_associationsObject

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_nameObject

Helper method for displaying the resource name.

Examples:

A resource called against a UserOption model.

resource.display_name #=> "User options"


77
78
79
# File 'lib/app_kit/resource.rb', line 77

def plural_display_name
  display_name.pluralize
end

#table_fieldsarray

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.

Returns:

  • (array)

    An of Field objects with display_in_table set to true



85
86
87
# File 'lib/app_kit/resource.rb', line 85

def table_fields
  fields.each.select(&:show_in_table)
end