Skip to content

Instantly share code, notes, and snippets.

@jaredatron
Created April 24, 2009 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jaredatron/100942 to your computer and use it in GitHub Desktop.
Save jaredatron/100942 to your computer and use it in GitHub Desktop.
# 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