Created
February 6, 2014 21:35
-
-
Save nz/8852934 to your computer and use it in GitHub Desktop.
Rails Concern to create a bunch of downcased getter methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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