Created
September 11, 2014 14:07
-
-
Save wojtha/2972034afdcf6796ce1a to your computer and use it in GitHub Desktop.
Smart params matcher helper for Ruby on Rails navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: | |
# params_match?(controller: 'comments', article_id: 23, id: 1) | |
def params_match?(pattern) | |
return true if pattern.nil? || pattern.empty? | |
pattern.collect do |(param, value)| | |
if value.is_a? Array | |
value.map(&:to_s).include?(params[param].to_s) | |
else | |
value.to_s == params[param].to_s | |
end | |
end.reduce(&:&) | |
end | |
# Usage: | |
# smart_params_match?([:comments], [:edit, :update], {article_id: 23, id: 1}, {:comment_id: 1}) | |
def smart_params_match?(controllers, actions = nil, *params_or) | |
return false unless controllers.nil? || controllers.empty? || [*controllers].include?(params[:controller].to_sym) | |
return false unless actions.nil? || actions.empty? || [*actions].include?(params[:action].to_sym) | |
params_or.nil? || params_or.empty? || [*params_or].map { |pars| params_match?(pars) }.reduce(&:|) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment