1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module SimpleAutocomplete
      def self.included(base)
        base.class_eval do |klass|
          extend ClassMethods
        end
      end
    
      module ClassMethods
        def autocomplete_for(method_name)
          meta_def("autocomplete_for_#{self.name.underscore}_#{method_name}") do
            self.all
          end
        end
      end
    end
    
    class Object
      def meta_def(method, &blok)
          (class << self; self; end).instance_eval { define_method method, &blok }
        end
    end
end

class Subject
      include SimpleAutocomplete
    
      field :title, :type => String
    
      autocomplete_for :title
    end