Created
April 24, 2009 04:34
-
-
Save jaredatron/100942 to your computer and use it in GitHub Desktop.
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
# These methods are inteded to extend any relationship owned | |
# by a another User | |
# Example | |
# class User | |
# has_manny :comments, :extend => OwnedByOtherUserRelationshipExtension | |
# end | |
module OwnedByOtherUserRelationshipExtension | |
# creates magic methods like these | |
# user.comments.by_blocked_users | |
# # => user.comments.select{ |c| user.blocking?(c.user) } | |
# | |
# user.comments.not_by_idolized_users | |
# # => user.comments.reject{ |c| user.idolizing?(c.user) } | |
# | |
# user.subscribers.not_with_idolized_users | |
# # => user.subscribers.reject{ |c| user.idolizing?(c.user) } | |
def method_missing(method, *args) | |
if matches = method.to_s.match(/^(not_)?(?:[a-z]+)_([a-z]+)_([a-z]+)/) # and proxy_owner.responds_to?("#{state}?".to_sym) # and !self.empty? and self.first.responds | |
puts matches.to_a.inspect | |
method, with_or_without, state, thing = matches.to_a | |
matcher = lambda{ |associated_model| | |
proxy_owner.send("#{state.sub(/ed$/,'ing')}?".to_sym, associated_model.send(thing.singularize)) | |
} | |
return with_or_without.nil? ? self.select(&matcher) : self.reject(&matcher) | |
end | |
super | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment