Class: ActiveElement::ControllerInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/active_element/controller_interface.rb

Overview

Provides the ‘active_element` object available on all controller instance/class methods. Encapsulates core functionality such as `authenticate_with`, `permit_action`, and `component` without polluting application controller namespace.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller_class, controller_instance = nil) ⇒ ControllerInterface

Returns a new instance of ControllerInterface.



16
17
18
19
20
21
22
# File 'lib/active_element/controller_interface.rb', line 16

def initialize(controller_class, controller_instance = nil)
  @controller_class = controller_class
  @controller_instance = controller_instance
  initialize_state(controller_class)
  @missing_template_store = {}
  @authorize = false
end

Class Attribute Details

.stateObject (readonly)

Returns the value of attribute state.



13
14
15
# File 'lib/active_element/controller_interface.rb', line 13

def state
  @state
end

Instance Attribute Details

#assigned_editable_fieldsObject (readonly)

Returns the value of attribute assigned_editable_fields.



8
9
10
# File 'lib/active_element/controller_interface.rb', line 8

def assigned_editable_fields
  @assigned_editable_fields
end

#current_userObject (readonly)

Returns the value of attribute current_user.



8
9
10
# File 'lib/active_element/controller_interface.rb', line 8

def current_user
  @current_user
end

#missing_template_storeObject (readonly)

Returns the value of attribute missing_template_store.



8
9
10
# File 'lib/active_element/controller_interface.rb', line 8

def missing_template_store
  @missing_template_store
end

Instance Method Details

#application_nameObject



60
61
62
# File 'lib/active_element/controller_interface.rb', line 60

def application_name
  RailsComponent.new(::Rails).application_name
end

#authenticateObject



99
100
101
102
103
104
# File 'lib/active_element/controller_interface.rb', line 99

def authenticate
  authenticator&.call
  @current_user = state.authorizor&.call

  nil
end

#authenticate_with(&block) ⇒ Object



68
69
70
# File 'lib/active_element/controller_interface.rb', line 68

def authenticate_with(&block)
  state.authenticator = block
end

#authorize?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/active_element/controller_interface.rb', line 24

def authorize?
  @authorize
end

#authorize_with(&block) ⇒ Object



72
73
74
75
# File 'lib/active_element/controller_interface.rb', line 72

def authorize_with(&block)
  @authorize = true
  state.authorizor = block
end

#componentObject

Raises:

  • (ArgumentError)


117
118
119
120
121
# File 'lib/active_element/controller_interface.rb', line 117

def component
  return (@component ||= ActiveElement::Component.new(controller_instance)) unless controller_instance.nil?

  raise ArgumentError, 'Attempted to use ActiveElement component from a controller class method.'
end

#deletableObject



52
53
54
# File 'lib/active_element/controller_interface.rb', line 52

def deletable
  state.deletable = true
end

#editable_fields(*args) ⇒ Object



38
39
40
# File 'lib/active_element/controller_interface.rb', line 38

def editable_fields(*args)
  state.editable_fields.concat(args.map(&:to_sym)).uniq!
end

#field_options(field, &block) ⇒ Object Also known as: field



47
48
49
# File 'lib/active_element/controller_interface.rb', line 47

def field_options(field, &block)
  state.field_options[field] = block
end

#listable_fields(*args, order: nil, scope: nil) ⇒ Object



28
29
30
31
32
# File 'lib/active_element/controller_interface.rb', line 28

def listable_fields(*args, order: nil, scope: nil)
  state.list_order = order
  state.list_scope = scope
  state.listable_fields.concat(args.map(&:to_sym)).uniq!
end

#model=(model) ⇒ Object



56
57
58
# File 'lib/active_element/controller_interface.rb', line 56

def model=(model)
  state.model = model.constantize
end

#permit_action(action, with: nil, always: false) ⇒ Object

Raises:

  • (ArgumentError)


106
107
108
109
110
111
# File 'lib/active_element/controller_interface.rb', line 106

def permit_action(action, with: nil, always: false)
  raise ArgumentError, "Must specify `with: '<permission>'` or `always: true`" unless with.present? || always
  raise ArgumentError, 'Cannot specify both `with` and `always: true`' if with.present? && always

  state.permissions << { with: with, always: always, action: action }
end

#searchable_fields(*args, required: false) ⇒ Object



42
43
44
45
# File 'lib/active_element/controller_interface.rb', line 42

def searchable_fields(*args, required: false)
  state.searchable_fields.concat(args.map(&:to_sym)).uniq!
  state.search_required = required
end

#sign_in_pathObject



93
94
95
# File 'lib/active_element/controller_interface.rb', line 93

def 
  state.&.call
end

#sign_in_with(method: :get, &block) ⇒ Object



88
89
90
91
# File 'lib/active_element/controller_interface.rb', line 88

def (method: :get, &block)
  state. = method
  state. = block
end

#sign_out_pathObject



82
83
84
# File 'lib/active_element/controller_interface.rb', line 82

def sign_out_path
  state.sign_out_path&.call
end

#sign_out_with(method: :get, &block) ⇒ Object



77
78
79
80
# File 'lib/active_element/controller_interface.rb', line 77

def sign_out_with(method: :get, &block)
  state.sign_out_method = method
  state.sign_out_path = block
end

#stateObject



123
124
125
# File 'lib/active_element/controller_interface.rb', line 123

def state
  self.class.state[controller_class]
end

#t(identifier, **kwargs) ⇒ Object



64
65
66
# File 'lib/active_element/controller_interface.rb', line 64

def t(identifier, **kwargs)
  ::I18n.t("active_element.#{identifier}", **kwargs)
end

#viewable_fields(*args) ⇒ Object



34
35
36
# File 'lib/active_element/controller_interface.rb', line 34

def viewable_fields(*args)
  state.viewable_fields.concat(args.map(&:to_sym)).uniq!
end