Module: Constantizable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/constantizable.rb,
lib/constantizable/version.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
"0.3.0"
Instance Method Summary collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# 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 |