Class: ActiveElement::ControllerInterface
- Inherits:
-
Object
- Object
- ActiveElement::ControllerInterface
- 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
-
.state ⇒ Object
readonly
Returns the value of attribute state.
Instance Attribute Summary collapse
-
#assigned_editable_fields ⇒ Object
readonly
Returns the value of attribute assigned_editable_fields.
-
#current_user ⇒ Object
readonly
Returns the value of attribute current_user.
-
#missing_template_store ⇒ Object
readonly
Returns the value of attribute missing_template_store.
Instance Method Summary collapse
- #application_name ⇒ Object
- #authenticate ⇒ Object
- #authenticate_with(&block) ⇒ Object
- #authorize? ⇒ Boolean
- #authorize_with(&block) ⇒ Object
- #component ⇒ Object
- #deletable ⇒ Object
- #editable_fields(*args) ⇒ Object
- #field_options(field, &block) ⇒ Object (also: #field)
-
#initialize(controller_class, controller_instance = nil) ⇒ ControllerInterface
constructor
A new instance of ControllerInterface.
- #listable_fields(*args, order: nil, scope: nil) ⇒ Object
- #model=(model) ⇒ Object
- #permit_action(action, with: nil, always: false) ⇒ Object
- #searchable_fields(*args, required: false) ⇒ Object
- #sign_in_path ⇒ Object
- #sign_in_with(method: :get, &block) ⇒ Object
- #sign_out_path ⇒ Object
- #sign_out_with(method: :get, &block) ⇒ Object
- #state ⇒ Object
- #t(identifier, **kwargs) ⇒ Object
- #viewable_fields(*args) ⇒ Object
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
.state ⇒ Object (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_fields ⇒ Object (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_user ⇒ Object (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_store ⇒ Object (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_name ⇒ Object
60 61 62 |
# File 'lib/active_element/controller_interface.rb', line 60 def application_name RailsComponent.new(::Rails).application_name end |
#authenticate ⇒ Object
99 100 101 102 103 104 |
# File 'lib/active_element/controller_interface.rb', line 99 def authenticate authenticator&.call @current_user = state.&.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
24 25 26 |
# File 'lib/active_element/controller_interface.rb', line 24 def @authorize end |
#authorize_with(&block) ⇒ Object
72 73 74 75 |
# File 'lib/active_element/controller_interface.rb', line 72 def (&block) @authorize = true state. = block end |
#component ⇒ Object
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 |
#deletable ⇒ Object
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, &block) state.[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
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. << { 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_path ⇒ Object
93 94 95 |
# File 'lib/active_element/controller_interface.rb', line 93 def sign_in_path state.sign_in_path&.call end |
#sign_in_with(method: :get, &block) ⇒ Object
88 89 90 91 |
# File 'lib/active_element/controller_interface.rb', line 88 def sign_in_with(method: :get, &block) state.sign_in_method = method state.sign_in_path = block end |
#sign_out_path ⇒ Object
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 |
#state ⇒ Object
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 |