Skip to content

Instantly share code, notes, and snippets.

@lazaronixon
Last active August 14, 2024 11:33
Show Gist options
  • Save lazaronixon/c666f027c77bbd0a8ff299d000b1b4a2 to your computer and use it in GitHub Desktop.
Save lazaronixon/c666f027c77bbd0a8ff299d000b1b4a2 to your computer and use it in GitHub Desktop.
Current page helpers
module ApplicationHelper
def current_page_with_controllers?(controllers)
controllers.any? { |controller| current_page? controller: controller }
end
def current_page_with_actions?(actions)
actions.any? { |action| current_page? action: action }
end
def current_page_with_controllers_and_actions?(controllers, actions)
current_page_with_controllers?(controllers) && current_page_with_actions?(actions)
end
def current_page_with_controllers_or_actions?(controllers, actions)
current_page_with_controllers?(controllers) || current_page_with_actions?(actions)
end
def current_page_with_controller_or_action?(value)
current_page?(controller: value) || current_page?(action: value)
end
# Checking controller name
current_page? controller: "people"
current_page_with_controllers? ["people", "contacts"]
# Checking action name
current_page? action: "new"
current_page_with_actions? ["new", "edit"]
# Checking both controller and action name
current_page? controller: "people", action: "new"
current_page_with_controllers_and_actions? ["people", "contacts"], ["new", "edit"]
# Checking controller or action name
current_page_with_controller_or_action? "people"
current_page_with_controllers_or_actions? ["people", "contacts"]
current_page_with_controller_or_action? "new"
current_page_with_controllers_or_actions? ["new", "edit"]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment