Class: Tolaria::ManagedClass

Inherits:
Object
  • Object
show all
Defined in:
lib/tolaria/managed_class.rb

Overview

Wraps an ActiveRecord::Base descendant and stores information that Tolaria needs to track about it

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#allowed_actionsObject

An array of the route actions allowed on this resource. Tolaria will pass this array as the :only option to the router



32
33
34
# File 'lib/tolaria/managed_class.rb', line 32

def allowed_actions
  @allowed_actions
end

#categoryObject

The navigation category and order to use for this resource



10
11
12
# File 'lib/tolaria/managed_class.rb', line 10

def category
  @category
end

#controller_nameObject

An auto-generated controller name for this resource in the Admin namespace



28
29
30
# File 'lib/tolaria/managed_class.rb', line 28

def controller_name
  @controller_name
end

#default_orderObject

The default sort order for this resource



22
23
24
# File 'lib/tolaria/managed_class.rb', line 22

def default_order
  @default_order
end

#iconObject

The Font Awesome icon to use for this resource



16
17
18
# File 'lib/tolaria/managed_class.rb', line 16

def icon
  @icon
end

#klassObject

The ActiveRecord::Base model we’re managing



7
8
9
# File 'lib/tolaria/managed_class.rb', line 7

def klass
  @klass
end

A String to override the model's label in the primary admin navigation



38
39
40
# File 'lib/tolaria/managed_class.rb', line 38

def navigation_label
  @navigation_label
end

#paginatedObject

Enable or disable automatic pagination for this model.



25
26
27
# File 'lib/tolaria/managed_class.rb', line 25

def paginated
  @paginated
end

#param_keyObject

A stored symbol for the params.permit key for this resource



35
36
37
# File 'lib/tolaria/managed_class.rb', line 35

def param_key
  @param_key
end

#permitted_paramsObject

An array of options to pass to params.permit for this model



19
20
21
# File 'lib/tolaria/managed_class.rb', line 19

def permitted_params
  @permitted_params
end

#priorityObject

Items are sorted with lowest priority first in the menu



13
14
15
# File 'lib/tolaria/managed_class.rb', line 13

def priority
  @priority
end

Class Method Details

.create(klass, icon: "file-o", permit_params: [], priority: 10, category: "Settings", default_order: "id DESC", paginated: true, allowed_actions: [:index, :show, :new, :create, :edit, :update, :destroy], navigation_label: klass.model_name.human.pluralize.titleize) ⇒ Object

A factory method that registers a new model in Tolaria and configures its menu and param settings. Developers should use ActiveRecord::Base.manage_with_tolaria



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tolaria/managed_class.rb', line 42

def self.create(klass, icon:"file-o", permit_params:[], priority:10, category:"Settings", default_order:"id DESC", paginated:true, allowed_actions:[:index, :show, :new, :create, :edit, :update, :destroy], navigation_label: klass.model_name.human.pluralize.titleize)

  managed_class = self.new
  managed_class.klass = klass

  # Assign the passed in setting
  managed_class.icon = icon.to_s.freeze
  managed_class.priority = priority.to_i
  managed_class.category = category.to_s.freeze
  managed_class.default_order = default_order.to_s.freeze
  managed_class.paginated = paginated.present?
  managed_class.permitted_params = permit_params.freeze
  managed_class.allowed_actions = allowed_actions.freeze
  managed_class.navigation_label = navigation_label.freeze

  # Set auto-generated attributes
  managed_class.controller_name = "#{managed_class.model_name.collection.camelize}Controller".freeze
  managed_class.param_key = managed_class.model_name.singular.to_sym

  return managed_class

end

Instance Method Details

#allows?(action) ⇒ Boolean

True if the given symbol is in the managed class's allowed_actions array.

Returns:

  • (Boolean)


66
67
68
# File 'lib/tolaria/managed_class.rb', line 66

def allows?(action)
  self.allowed_actions.include?(action)
end

#model_nameObject

Defer to the ActiveRecord::Base model_name system for producing pleasant user-interface names for this resource



82
83
84
# File 'lib/tolaria/managed_class.rb', line 82

def model_name
  self.klass.model_name
end

#no_resources?Boolean

True if there are no resources in the database for this class

Returns:

  • (Boolean)


76
77
78
# File 'lib/tolaria/managed_class.rb', line 76

def no_resources?
  self.klass.count.eql?(0)
end

#paginated?Boolean

True if this managed class should be paginated

Returns:

  • (Boolean)


71
72
73
# File 'lib/tolaria/managed_class.rb', line 71

def paginated?
  self.paginated
end