module Constantizable

Constants

VERSION

Public Instance Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/constantizable.rb, line 57
def method_missing(method, *args, &block)
  # Refer https://github.com/rails/rails/blob/master/activesupport/lib/active_support/string_inquirer.rb
  # Inquiry happens only if method is an inquiry method (i.e) method name ends with a '?'

  if method[-1] == '?'
    # If Column name isn't present it should fallback to the default `method_missing`
    # implementation.
    m = method.to_s.gsub("?","")
    column_names = self.class.instance_variable_get(:@constantized_columns)
    super if column_names.blank?

    # The value of the constantized column needs to be titleized or underscored.
    record = nil
    column_names.each do | column_name |
      break if record.present?
      record = self.class.find_by("lower(#{column_name}) = ? or lower(#{column_name}) = ?", m.to_s.downcase, m.to_s.titleize.downcase)    
    end

    if record.present?
      self.id == record.id
    else
      super
    end
  else
    super
  end
end