Module: StatefulLink::ActionAnyOf::InstanceMethods
- Defined in:
- lib/stateful_link/action_any_of.rb
Instance Method Summary collapse
-
#action_any_of?(*actions) ⇒ Boolean
Returns true if current controller and action names equals to any of passed.
-
#action_state(active, chosen = nil) ⇒ Object
Returns link state related to current controller and action: :inactive, :active or :chosen.
-
#extract_controller_and_action(ca) ⇒ Object
Extracts controller and action names from a string.
Instance Method Details
#action_any_of?(*actions) ⇒ Boolean
Returns true if current controller and action names equals to any of passed. Asterik as action name matches all controller’s action.
Examples:
<%= "we are in PostsController::index" if action_any_of?("posts#index") %>
<%= "we are not in PostsController::index" unless action_any_of?("posts#index") %>
<% if action_any_of?("posts#index", "messages#index") %>
we are in PostsController or in MessagesController
<% end %>
22 23 24 25 26 27 28 29 |
# File 'lib/stateful_link/action_any_of.rb', line 22 def action_any_of?(*actions) actions.any? do |sub_ca| if sub_ca.present? sub_controller, sub_action = extract_controller_and_action(sub_ca) ((self.controller_path == sub_controller) || (sub_controller.blank?)) && (self.action_name == sub_action || (sub_action == '' || sub_action == '*')) end end end |
#action_state(active, chosen = nil) ⇒ Object
Returns link state related to current controller and action: :inactive, :active or :chosen. A list of active actions is the first argument, chosen actions are the second argument.
Examples:
# :active for PostsController::index, :chosen for PostsController::* (except :index),
# :inactive otherwise.
action_state("posts#index", "posts#")
# :active for PostsController::new and PostsController::create, :inactive otherwise.
action_state(["posts#new", "posts#create"])
# :active for PostsController::index, :chosen for MessagesController::* and
# PostsController::* (except :index), :inactive otherwise.
action_state("posts#index", ["posts#", "messages#"])
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/stateful_link/action_any_of.rb', line 65 def action_state(active, chosen = nil) active = active.is_a?(String) ? [active] : active chosen = chosen.is_a?(String) ? [chosen] : chosen if action_any_of?(*active) :active elsif !chosen.nil? && action_any_of?(*chosen) :chosen else :inactive end end |
#extract_controller_and_action(ca) ⇒ Object
Extracts controller and action names from a string.
Examples:
extract_controller_and_action("posts#index") # ["posts", "index"]
extract_controller_and_action("admin/posts#index") # ["admin/posts", "index"]
extract_controller_and_action("admin/posts#index") # raises ArgumentError
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/stateful_link/action_any_of.rb', line 39 def extract_controller_and_action(ca) raise ArgumentError, "Pass the string" if ca.nil? slash_pos = ca.rindex('#') raise ArgumentError, "Invalid action: #{ca}" if slash_pos.nil? controller = ca[0, slash_pos] action = ca[slash_pos+1..-1] || "" raise ArgumentError, "Invalid action or controller" if action.nil? [controller, action] end |