Skip to content
Browse files

refactor code injection to be simpler

  • Loading branch information...
1 parent 523074a commit 42785d82e30d188fa612e120bc220342d650de94 @grosser committed Dec 23, 2009
Showing with 6 additions and 8 deletions.
  1. +1 −3 init.rb
  2. +5 −5 lib/simple_autocomplete.rb
View
4 init.rb
@@ -1,3 +1 @@
-require 'simple_autocomplete'
-ActionController::Base.send :extend, SimpleAutocomplete::ControllerMethods
-ActiveRecord::Base.send :extend, SimpleAutocomplete::RecordMethods
+require 'simple_autocomplete'
View
10 lib/simple_autocomplete.rb
@@ -26,8 +26,8 @@ module SimpleAutocomplete
# autocomplete_for :post, :title do |items|
# items.map{|item| "#{item.title} -- #{item.id}"}.join("\n")
# end
- module ControllerMethods
- def autocomplete_for(object, method, options = {})
+ class ActionController::Base
+ def self.autocomplete_for(object, method, options = {})
define_method("autocomplete_for_#{object}_#{method}") do
model = object.to_s.camelize.constantize
find_options = {
@@ -53,8 +53,8 @@ def autocomplete_for(object, method, options = {})
# -> the auto_user_name field will be resolved to a User, using User.find_by_autocomplete_name(value)
# -> Post has autocomplete_for('user','name')
# -> User has find_by_autocomplete('name')
- module RecordMethods
- def autocomplete_for(object,method,options={})
+ class ActiveRecord::Base
+ def self.autocomplete_for(object,method,options={})
name = options[:name] || object.to_s.underscore
name = name.to_s
object = object.to_s.camelize.constantize
@@ -72,7 +72,7 @@ def autocomplete_for(object,method,options={})
end
end
- def find_by_autocomplete(attr)
+ def self.find_by_autocomplete(attr)
class_eval <<-end_eval
def self.find_by_autocomplete_#{attr}(value)
return nil if value.blank?

0 comments on commit 42785d8

Please sign in to comment.
Something went wrong with that request. Please try again.