4
5
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/mongoid/token/finders.rb', line 4
def self.define_custom_token_finder_for(klass, field_name = :token)
klass.define_singleton_method(:"find_by_#{field_name.to_s}") do |token|
self.find_by(field_name.to_sym => token)
end
klass.define_singleton_method :"find_with_#{field_name}" do |*args| args.all?{|arg| Moped::BSON::ObjectId.legal?(arg)} ? send(:"find_without_#{field_name}",*args) : klass.send(:"find_by_#{field_name.to_s}", args.first)
end
klass.class_eval("class << self; alias_method_chain :find, :#{field_name} if self.method_defined?(:find); end")
end
|