Instantly share code, notes, and snippets.
Created
November 10, 2011 16:37
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save hilios/1355326 to your computer and use it in GitHub Desktop.
Navigation helpers
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
module NavigationHelper | |
# Create a link with confirmation message and method set to delete. | |
# | |
# Returns a link_to helper with options +:confirm+ and +:method+ setted, | |
# you can override the default values setting the options hash. | |
def link_to_destroy(*args, &block) | |
arg_index = if block_given? then 1 else 2 end | |
args[arg_index] ||= {} | |
args[arg_index][:confirm] = t('actions.delete.are_you_sure', :default => 'Are you sure?') unless args[arg_index].has_key? :confirm | |
args[arg_index][:method] = :delete unless args[arg_index].has_key? :method | |
link_to(*args, &block) | |
end | |
# Returns true if matchs the current path | |
def path_is?(path) | |
request.path == path | |
end | |
# Returns true if matchs the current url | |
def url_is?(url) | |
request.url == url | |
end | |
# Returns true if matchs the current module name | |
# For example: | |
# | |
# Admin::UsersController.index | |
# module_is?(:admin) # => true | |
# module_is?('admin', 'other') # => true | |
# | |
# PostsController.show | |
# module_is?(:admin) # => false | |
# | |
# If a block is given yield with the first argument has the | |
# result of the match. For example: | |
# | |
# module_is?(:admin) do |boolean| | |
# if boolean | |
# # Do something when true | |
# else | |
# # And another when false | |
# end | |
# end | |
def module_is?(*modules, &block) | |
has_value = modules.map(&:to_s).include? params[:controller][/(.+)\//, 1] | |
return block_given? ? yield(has_value) : has_value | |
end | |
# Returns true if matchs the current controller name | |
# For example: | |
# | |
# UsersController.index | |
# controller_is?(:users) # => true | |
# controller_is?('users', 'other') # => true | |
# | |
# PostsController.show | |
# controller_is?(:users) # => false | |
# | |
# If a block is given yield with the first argument has the | |
# result of the match. For example: | |
# | |
# controller_is?(:admin) do |boolean| | |
# if boolean | |
# # Do something when true | |
# else | |
# # And another when false | |
# end | |
# end | |
def controller_is?(*controllers, &block) | |
has_value = controllers.map(&:to_s).include? params[:controller] | |
return block_given? ? yield(has_value) : has_value | |
end | |
# Returns true if matchs the current action name | |
# For example: | |
# | |
# UsersController.index | |
# action_is?(:users) # => true | |
# action_is?('users', 'other') # => true | |
# | |
# PostsController.show | |
# action_is?(:users) # => false | |
# | |
# If a block is given yield with the first argument has the | |
# result of the match. For example: | |
# | |
# action_is?(:admin) do |boolean| | |
# if boolean | |
# # Do something when true | |
# else | |
# # And another when false | |
# end | |
# end | |
def action_is?(*actions, &block) | |
has_value = actions.map(&:to_s).include? params[:action] | |
return block_given? ? yield(has_value) : has_value | |
end | |
end |
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
require 'spec_helper' | |
describe ApplicationHelper do | |
describe "#link_to_destroy" do | |
it "should create a link with data-method=delete and confirmation" do | |
link = helper.link_to_destroy('Delete', '/delete') | |
link.should have_css('a[data-method="delete"]') | |
link.should have_css('a[data-confirm]') | |
link.should have_content('Delete') | |
end | |
it "should work with blocks too" do | |
link = helper.link_to_destroy('/delete') { 'Delete' } | |
link.should have_css('a[data-method="delete"]') | |
link.should have_css('a[data-confirm]') | |
link.should have_content('Delete') | |
end | |
it "should be able to override the settings" do | |
link = helper.link_to_destroy('Delete', '/delete', method: :put) | |
link.should have_css('a[data-method="put"]') | |
link.should have_css('a[data-confirm]') | |
end | |
end | |
describe "#module_is?" do | |
before(:each) do | |
controller.params[:controller] = "test/controller" | |
end | |
it "should return true/false if the module name(s) matchs" do | |
helper.module_is?('test').should be_true | |
helper.module_is?('fake').should be_false | |
helper.module_is?('fake', 'test').should be_true | |
helper.module_is?('fake', 'mock').should be_false | |
# Same test with symbols | |
helper.module_is?(:test).should be_true | |
helper.module_is?(:fake).should be_false | |
helper.module_is?(:fake, :test).should be_true | |
helper.module_is?(:fake, :mock).should be_false | |
end | |
it "should execute block instead of returning true or fale" do | |
helper.module_is?('test') { |b| b.should be_true } | |
helper.module_is?('fake') { |b| b.should be_false } | |
helper.module_is?(:fake, :test) { |b| b.should be_true } | |
helper.module_is?(:fake, :mock) { |b| b.should be_false } | |
end | |
end | |
describe "#controller_is?" do | |
before(:each) do | |
controller.params[:controller] = "test" | |
end | |
it "should return true or false for controller name(s)" do | |
helper.controller_is?('test').should be_true | |
helper.controller_is?('fake').should be_false | |
helper.controller_is?('fake', 'test').should be_true | |
helper.controller_is?('fake', 'mock').should be_false | |
# Same test with symbols | |
helper.controller_is?(:test).should be_true | |
helper.controller_is?(:fake).should be_false | |
helper.controller_is?(:fake, :test).should be_true | |
helper.controller_is?(:fake, :mock).should be_false | |
end | |
it "should execute block instead of returning true or fale" do | |
helper.controller_is?('test') { |b| b.should be_true } | |
helper.controller_is?('fake') { |b| b.should be_false } | |
helper.controller_is?(:fake, :test) { |b| b.should be_true } | |
helper.controller_is?(:fake, :mock) { |b| b.should be_false } | |
end | |
end | |
describe "#action_is?" do | |
before(:each) do | |
controller.params[:action] = "test" | |
end | |
it "should return true or false for action name(s)" do | |
helper.action_is?('test').should be_true | |
helper.action_is?('fake').should be_false | |
helper.action_is?('fake', 'test').should be_true | |
helper.action_is?('fake', 'mock').should be_false | |
# Same test with symbols | |
helper.action_is?(:test).should be_true | |
helper.action_is?(:fake).should be_false | |
helper.action_is?(:fake, :test).should be_true | |
helper.action_is?(:fake, :mock).should be_false | |
end | |
it "should execute block instead of returning true or fale" do | |
helper.action_is?('test') { |b| b.should be_true } | |
helper.action_is?('fake') { |b| b.should be_false } | |
helper.action_is?(:fake, :test) { |b| b.should be_true } | |
helper.action_is?(:fake, :mock) { |b| b.should be_false } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment