Skip to content

Commit

Permalink
check for method existance in a ruby 1.8- and 1.9-compatible way
Browse files Browse the repository at this point in the history
this is a cherry-pick of ed6785b
[mislav#269 state:resolved]
  • Loading branch information
mislav committed May 20, 2009
1 parent 11ca129 commit b63e438
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/will_paginate.rb
Expand Up @@ -17,7 +17,7 @@ def enable

# hooks WillPaginate::ViewHelpers into ActionView::Base
def enable_actionpack
return if ActionView::Base.instance_methods.include? 'will_paginate'
return if ActionView::Base.instance_methods.include_method? :will_paginate
require 'will_paginate/view_helpers'
ActionView::Base.send :include, ViewHelpers

Expand Down
15 changes: 13 additions & 2 deletions lib/will_paginate/core_ext.rb
@@ -1,7 +1,18 @@
require 'set'
require 'will_paginate/array'

unless Hash.instance_methods.include? 'except'
# helper to check for method existance in ruby 1.8- and 1.9-compatible way
# because `methods`, `instance_methods` and others return strings in 1.8 and symbols in 1.9
#
# ['foo', 'bar'].include_method?(:foo) # => true
class Array
def include_method?(name)
name = name.to_sym
!!(find { |item| item.to_sym == name })
end
end

unless Hash.instance_methods.include_method? :except
Hash.class_eval do
# Returns a new hash without the given keys.
def except(*keys)
Expand All @@ -16,7 +27,7 @@ def except!(*keys)
end
end

unless Hash.instance_methods.include? 'slice'
unless Hash.instance_methods.include_method? :slice
Hash.class_eval do
# Returns a new hash with only the given keys.
def slice(*keys)
Expand Down
2 changes: 1 addition & 1 deletion lib/will_paginate/finder.rb
Expand Up @@ -214,7 +214,7 @@ def wp_count(options, args, finder)

# forget about includes if they are irrelevant (Rails 2.1)
if count_options[:include] and
klass.private_methods.include?('references_eager_loaded_tables?') and
klass.private_methods.include_method?(:references_eager_loaded_tables?) and
!klass.send(:references_eager_loaded_tables?, count_options)
count_options.delete :include
end
Expand Down
6 changes: 3 additions & 3 deletions test/finder_test.rb
Expand Up @@ -284,11 +284,11 @@ def test_readonly
# this functionality is temporarily removed
def xtest_pagination_defines_method
pager = "paginate_by_created_at"
assert !User.methods.include?(pager), "User methods should not include `#{pager}` method"
assert !User.methods.include_method?(pager), "User methods should not include `#{pager}` method"
# paginate!
assert 0, User.send(pager, nil, :page => 1).total_entries
# the paging finder should now be defined
assert User.methods.include?(pager), "`#{pager}` method should be defined on User"
assert User.methods.include_method?(pager), "`#{pager}` method should be defined on User"
end

# Is this Rails 2.0? Find out by testing find_all which was removed in [6998]
Expand Down Expand Up @@ -440,7 +440,7 @@ def test_paginated_each_with_named_scope
end

# detect ActiveRecord 2.1
if ActiveRecord::Base.private_methods.include?('references_eager_loaded_tables?')
if ActiveRecord::Base.private_methods.include_method?(:references_eager_loaded_tables?)
def test_removes_irrelevant_includes_in_count
Developer.expects(:find).returns([1])
Developer.expects(:count).with({}).returns(0)
Expand Down

0 comments on commit b63e438

Please sign in to comment.