Module: Mongoid::Fields::ClassMethods

Defined in:
lib/locomotive/mongoid/patches.rb

Instance Method Summary collapse

Instance Method Details

#add_field(name, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/locomotive/mongoid/patches.rb', line 26

def add_field(name, options = {})
  # FIXME: The Rails ActionView inputs get the value of a field from the <FIELD>_before_type_cast method if it exists.
  # In Mongoid 7, the Mongoid core team implemented the `_before_type_cast?` / `_before_type_cast` 
  # for ALL the fields including the localized ones.
  # Incidentally, it breaks the form inputs for localized fields. 
  # This patch restores the old behavior for localized fiels by implementing the X_came_from_user? method
  # which makes the form inputs use the translated value of a field.
  generated_methods.module_eval do
    re_define_method("#{name}_came_from_user?") do
      false
    end
  end if options[:localize]

  add_field_without_locomotive_patch(name, options)
end

#add_field_without_locomotive_patchObject



24
# File 'lib/locomotive/mongoid/patches.rb', line 24

alias_method :add_field_without_locomotive_patch, :add_field