Skip to content

Instantly share code, notes, and snippets.

@nz
Created February 6, 2014 21:35
Show Gist options
  • Save nz/8852934 to your computer and use it in GitHub Desktop.
Save nz/8852934 to your computer and use it in GitHub Desktop.
Rails Concern to create a bunch of downcased getter methods
module Downcaseable
extend ActiveSupport::Concern
module ClassMethods
def downcase_field(*names)
Array(names).flatten.each do
class_eval %Q(
def #{name}_eval
send(:#{name}).try(:downcase)
end
)
end
end
end
end
class Person
include Downcaseable
downcaseable :name, :hometown, :college
searchable do
string :name_downcase
string :hometown_downcase
string :college_downcase
end
end
class Article
include Downcaseable
downcaseable :category_name, :author_name
searchable do
string :author_name_downcase
string :category_name_downcase
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment