Module: ActiveRecord::Acts::Finder::ClassMethods

Defined in:
lib/acts_as_finder/active_record/acts/finder.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_finder(*fields) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/acts_as_finder/active_record/acts/finder.rb', line 9

def acts_as_finder(*fields)
  instance_eval <<-EOV
    @acts_as_finder_fields ||= []
    @acts_as_finder_fields += #{fields.inspect}
    def method_missing(sym, *args, &block)
      begin 
        super
      rescue => e
        @acts_as_finder_fields.each do |field|
          el = where(field.to_sym => sym).first
          return el if el
        end
        raise e
      end
    end
    
    def methods
      @acts_as_finder_fields.inject(super) do |acc, field|
        acc += all.map { |r| r.send(field).to_sym }
      end
    end
  EOV
end