Module: Madmin::Resourceable::ClassMethods

Defined in:
lib/madmin/resourceable/class_methods.rb

Overview

This module extends methods into class methods on the resource.

Instance Method Summary collapse

Instance Method Details

#field(*args) ⇒ Object

This becomes a DSL for adding a field into the fields class variable. It is responsible for validating and parsing the arguments before placing them into the fields class variable.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/madmin/resourceable/class_methods.rb', line 21

def field(*args)
  validate_arguments!(args)

  key        = args[0].to_sym
  field_type = args[1]

  # We reassign the entire list of fields to prevent
  # duplication each time the class is evaluated.
  fresh_fields = fields
  fresh_fields[key] = field_type.new(args[2].merge(key: key, model: model, resource: self))

  class_variable_set(:@@fields, fresh_fields)
end

#fieldsObject

This method is a wrapper for the class variable fields. In the event there is no field defined yet, we rescue NameError and return an empty hash to begin the population of the fields class variable.



11
12
13
14
15
# File 'lib/madmin/resourceable/class_methods.rb', line 11

def fields
  class_variable_get(:@@fields)
rescue NameError
  {}
end

#form_all_fields!Object

This method, when used in the resource, sets all fields to be used when the form partial is rendered.



63
64
65
# File 'lib/madmin/resourceable/class_methods.rb', line 63

def form_all_fields!
  class_variable_set(:@@form_all_fields, true)
end

#form_all_fields?Boolean

This method exposes a convenient way to see if all fields for a resource should be available in a form partial.

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/madmin/resourceable/class_methods.rb', line 70

def form_all_fields?
  class_variable_get(:@@form_all_fields)
rescue NameError
  false
end

#modelObject

This method exposes the underlying model.



78
79
80
# File 'lib/madmin/resourceable/class_methods.rb', line 78

def model
  model_name.constantize
end

#model_nameObject

This method extracts the namespace to reveal the underlying models name as a string.



85
86
87
# File 'lib/madmin/resourceable/class_methods.rb', line 85

def model_name
  to_s.split("Madmin::Resources::").last.to_s
end

#scope(*args) ⇒ Object

This becomes a DSL for adding a field into the scopes class variable. It is responsible for validating and parsing the arguments before placing them into the scopes class variable.



50
51
52
53
54
55
56
57
58
# File 'lib/madmin/resourceable/class_methods.rb', line 50

def scope(*args)
  validate_scopes!(args)

  # We reassign the entire list of scopes to prevent
  # duplication each time the class is evaluated.
  fresh_scopes = scopes << args

  class_variable_set(:@@scopes, fresh_scopes.flatten.uniq)
end

#scopesObject

This method is a wrapper for the class variable scopes. In the event there is no scope defined yet, we rescue NameError and return an empty array to begin the population of the scopes class variable.



40
41
42
43
44
# File 'lib/madmin/resourceable/class_methods.rb', line 40

def scopes
  class_variable_get(:@@scopes)
rescue NameError
  []
end

#show_all_fields!Object

This method exposes a convenient way to see if all fields for a resource should be available in a show partial.



92
93
94
# File 'lib/madmin/resourceable/class_methods.rb', line 92

def show_all_fields!
  class_variable_set(:@@show_all_fields, true)
end

#show_all_fields?Boolean

This method exposes a convenient way to see if all fields for a resource should be available in a show partial.

Returns:

  • (Boolean)


99
100
101
102
103
# File 'lib/madmin/resourceable/class_methods.rb', line 99

def show_all_fields?
  class_variable_get(:@@show_all_fields)
rescue NameError
  false
end