Skip to content

Instantly share code, notes, and snippets.

@ernie
Created November 16, 2010 14:11
Show Gist options
  • Save ernie/701856 to your computer and use it in GitHub Desktop.
Save ernie/701856 to your computer and use it in GitHub Desktop.
A really evil trick to get at the wrapped AR model in a class method
def initialize(base_or_relation, opts = {})
@relation = base_or_relation.scoped
@base = @relation.klass
@search_key = opts[:search_key] ? opts[:search_key].to_s : 'search'
@join_dependency = build_join_dependency
@search_attributes = {}
@errors = ActiveModel::Errors.new(self)
singleton_class.instance_eval "def base; #{@base} end"
end
# We need the base at the singleton_class level to play nice with i18n.
# If anyone's got a less evil solution than this, I'm all ears.
def class
singleton_class
end
@ernie
Copy link
Author

ernie commented Nov 16, 2010

I was expecting to be condemned to coder hell, where I would only be allowed to code in Visual Basic. Sounds like you're letting me off easy. That being said, I already changed it, and I'm still not happy with things. activerecord-hackery/meta_search@0997c3d

I'm beginning to think I'm in for a much larger refactor. I really don't want to pollute the AR::Base namespace the way that Searchlogic does, though.

@evanphx
Copy link

evanphx commented Nov 16, 2010

90 days is enough to think about what you've done and how you can do it better. No one deserves a permanent condemnation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment