Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

if method_missing accepts some method name patterns, then respod_to? …

…should return true for the same method names
  • Loading branch information...
commit baff20edcd231b7258cdc94c0351a4d57948dbd9 1 parent 3720ce0
@justinfrench authored
Showing with 16 additions and 1 deletion.
  1. +9 −1 lib/active_time.rb
  2. +7 −0 test/active_time_test.rb
View
10 lib/active_time.rb
@@ -1,5 +1,7 @@
class ActiveTime
+ COLLECTION_METHOD_NAMES_PATTERN = /[a-z_]+s$/
+
attr_accessor :starting, :ending
alias_method :time, :starting
@@ -60,7 +62,7 @@ def range
#
# TODO: memoize the results, to avoid multiple queries for the same method call.
def method_missing(method_name, *args)
- if method_name.to_s =~ /[a-z_]+s$/
+ if method_name.to_s =~ COLLECTION_METHOD_NAMES_PATTERN
args[0] ||= :created_at
begin
klass_name = method_name.to_s.singularize.classify
@@ -76,6 +78,12 @@ def method_missing(method_name, *args)
end
end
+ # If method_missing deals with these, respond_to should too.
+ def respond_to?(method_name)
+ return true if method_name.to_s =~ COLLECTION_METHOD_NAMES_PATTERN
+ super
+ end
+
# Provides a human friendly string description of the date or time range being used. Examples:
#
# ActiveTime.new(2008) # => "in 2008"
View
7 test/active_time_test.rb
@@ -208,6 +208,13 @@ def ending
assert_equal @object.starting, @object.time
end
+ should "respond to methods named after collections of ActiveRecord objects" do
+ assert @object.respond_to?(:posts)
+ assert @object.respond_to?(:activity_events)
+ assert @object.respond_to?(:users)
+ assert @object.respond_to?(:summaries)
+ end
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.