Commit a2e707c5bc46332ed85965660ad6731167e44505

Authored by Douglas F Shearer
1 parent dfd8bd6c

Added some method missing magic to acts_as_follower.

lib/acts_as_follower.rb
... ... @@ -68,6 +68,16 @@ module ActiveRecord
68 68 follows_by_type(followable_type).map { |f| f.followable}
69 69 end
70 70  
  71 + # Allows magic names on following_by_type
  72 + # e.g. following_users == following_by_type('User')
  73 + def method_missing(m, *args)
  74 + if m.to_s[/following_(.+)/]
  75 + following_by_type($1.classify)
  76 + else
  77 + super
  78 + end
  79 + end
  80 +
71 81 private
72 82  
73 83 # Returns a follow record for the current instance and followable object.
... ...
test/acts_as_follower_test.rb
... ... @@ -66,4 +66,9 @@ class ActsAsFollowerTest < Test::Unit::TestCase
66 66 assert_equal [users(:jon)], users(:sam).following_by_type('User')
67 67 end
68 68  
  69 + def test_method_missing_should_call_following_by_type
  70 + assert_equal [bands(:oasis)], users(:sam).following_bands
  71 + assert_equal [users(:jon)], users(:sam).following_users
  72 + end
  73 +
69 74 end
... ...