Class: Volt::ControllerHandler
- Defined in:
- lib/volt/page/bindings/view_binding/controller_handler.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
Class Method Summary collapse
-
.get_controller_and_action(controller_path) ⇒ Object
Fetch the controller class.
Instance Method Summary collapse
- #call_action(stage_prefix = nil, stage_suffix = nil) ⇒ Object
-
#initialize(controller, action) ⇒ ControllerHandler
constructor
A new instance of ControllerHandler.
Constructor Details
#initialize(controller, action) ⇒ ControllerHandler
Returns a new instance of ControllerHandler.
5 6 7 8 |
# File 'lib/volt/page/bindings/view_binding/controller_handler.rb', line 5 def initialize(controller, action) @controller = controller @action = action.to_sym if action end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
3 4 5 |
# File 'lib/volt/page/bindings/view_binding/controller_handler.rb', line 3 def action @action end |
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
3 4 5 |
# File 'lib/volt/page/bindings/view_binding/controller_handler.rb', line 3 def controller @controller end |
Class Method Details
.get_controller_and_action(controller_path) ⇒ Object
Fetch the controller class
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/volt/page/bindings/view_binding/controller_handler.rb', line 43 def self.get_controller_and_action(controller_path) fail "Invalid controller path: #{controller_path.inspect}" unless controller_path && controller_path.size > 0 action = controller_path[-1] # Get the constant parts parts = controller_path[0..-2].map { |v| v.tr('-', '_').camelize } # Do const lookups starting at object and working our way down. # So Volt::ProgressBar would lookup Volt, then ProgressBar on Volt. obj = Object parts.each do |part| if obj.const_defined?(part) obj = obj.const_get(part) else # return a blank ModelController return [ModelController, nil] end end [obj, action] end |
Instance Method Details
#call_action(stage_prefix = nil, stage_suffix = nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/volt/page/bindings/view_binding/controller_handler.rb', line 10 def call_action(stage_prefix = nil, stage_suffix = nil) return unless @action has_stage = stage_prefix || stage_suffix if has_stage method_name = @action method_name = "#{stage_prefix}_#{method_name}" if stage_prefix method_name = "#{method_name}_#{stage_suffix}" if stage_suffix method_name = method_name.to_sym else method_name = @action end # If no stage, then we are calling the main action method, # so we should call the before/after actions unless has_stage if @controller.run_actions(:before, @action) # stop_chain was called return true end end @controller.send(method_name) if @controller.respond_to?(method_name) @controller.run_actions(:after, @action) unless has_stage # before_action chain was not stopped false end |