Module: Mongoid::Suicide::ClassMethods

Defined in:
lib/mongoid/suicide.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#remove_accessors(name, meth) ⇒ Object

Remove the field accessors.

Examples:

Remove the accessors.

Person.remove_accessors(:name, "name")
person.name #=> undefined
person.name = "" #=> undefined
person.name? #=> undefined
person.name_before_type_cast #=> undefined
person.name_translations #=> undefined
person.name_translations = "" #=> undefined
person.name_t #=> undefined
person.name_t = "" #=> undefined

Parameters:

  • name (Symbol)

    The name of the field.

  • meth (Symbol)

    The name of the accessor.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mongoid/suicide.rb', line 59

def remove_accessors(name, meth)
  field = fields[name]

  remove_field_getter(meth)
  remove_field_getter_before_typecast(meth)
  remove_field_setter(meth)
  remove_field_check(meth)

  return unless field.options[:localize]

  remove_translations_getter(meth)
  remove_translations_setter(meth)
  localized_fields.delete(name)
end

#remove_dirty_change_accessor(meth) ⇒ Object

Removes the dirty change accessor.

Examples:

Remove the accessor.

Model.remove_dirty_change_accessor("name")

Parameters:

  • meth (String)

    The name of the accessor.



218
219
220
221
222
# File 'lib/mongoid/suicide.rb', line 218

def remove_dirty_change_accessor(meth)
  generated_methods.module_eval do
    undef_method("#{meth}_change") if method_defined?("#{meth}_change")
  end
end

#remove_dirty_change_check(meth) ⇒ Object

Removes the dirty change check.

Examples:

Remove the check.

Model.remove_dirty_change_check("name")

Parameters:

  • meth (String)

    The name of the accessor.



230
231
232
233
234
# File 'lib/mongoid/suicide.rb', line 230

def remove_dirty_change_check(meth)
  generated_methods.module_eval do
    undef_method("#{meth}_changed?") if method_defined?("#{meth}_changed?")
  end
end

#remove_dirty_change_flag(meth) ⇒ Object

Removes the dirty change flag.

Examples:

Remove the flag.

Model.remove_dirty_change_flag("name")

Parameters:

  • meth (String)

    The name of the accessor.



242
243
244
245
246
# File 'lib/mongoid/suicide.rb', line 242

def remove_dirty_change_flag(meth)
  generated_methods.module_eval do
    undef_method("#{meth}_will_change!") if method_defined?("#{meth}_will_change!")
  end
end

#remove_dirty_default_change_check(meth) ⇒ Object

Removes the dirty default change check.

Examples:

Remove the check.

Model.remove_dirty_default_change_check("name")

Parameters:

  • meth (String)

    The name of the accessor.



254
255
256
257
258
# File 'lib/mongoid/suicide.rb', line 254

def remove_dirty_default_change_check(meth)
  generated_methods.module_eval do
    undef_method("#{meth}_changed_from_default?") if method_defined?("#{meth}_changed_from_default?")
  end
end

#remove_dirty_methods(meth) ⇒ Object

Removes the dirty change methods.

Examples:

Remove the accessors.

Person.remove_dirty_change_accessor(:name, "name")
person.name_change #=> undefined
person.name_changed? #=> undefined
person.name_will_change! #=> undefined
person.name_changed_from_default? #=> undefined
person.name_was #=> undefined
person.reset_name! #=> undefined
person.reset_name_to_default! #=> undefined

Parameters:

  • name (Symbol)

    The attribute name.

  • meth (String)

    The name of the accessor.



88
89
90
91
92
93
94
95
96
# File 'lib/mongoid/suicide.rb', line 88

def remove_dirty_methods(meth)
  remove_dirty_change_accessor(meth)
  remove_dirty_change_check(meth)
  remove_dirty_change_flag(meth)
  remove_dirty_default_change_check(meth)
  remove_dirty_previous_value_accessor(meth)
  remove_dirty_reset(meth)
  remove_dirty_reset_to_default(meth)
end

#remove_dirty_previous_value_accessor(meth) ⇒ Object

Removes the dirty change previous value accessor.

Examples:

Remove the accessor.

Model.remove_dirty_previous_value_accessor("name")

Parameters:

  • meth (String)

    The name of the accessor.



266
267
268
269
270
# File 'lib/mongoid/suicide.rb', line 266

def remove_dirty_previous_value_accessor(meth)
  generated_methods.module_eval do
    undef_method("#{meth}_was") if method_defined?("#{meth}_was")
  end
end

#remove_dirty_reset(meth) ⇒ Object

Removes the dirty change reset.

Examples:

Remove the reset.

Model.remove_dirty_reset("name")

Parameters:

  • meth (String)

    The name of the accessor.



278
279
280
281
282
# File 'lib/mongoid/suicide.rb', line 278

def remove_dirty_reset(meth)
  generated_methods.module_eval do
    undef_method("reset_#{meth}!") if method_defined?("reset_#{meth}!")
  end
end

#remove_dirty_reset_to_default(meth) ⇒ Object

Removes the dirty change reset to default.

Examples:

Remove the reset.

Model.remove_dirty_reset_to_default("name")

Parameters:

  • meth (String)

    The name of the accessor.



290
291
292
293
294
# File 'lib/mongoid/suicide.rb', line 290

def remove_dirty_reset_to_default(meth)
  generated_methods.module_eval do
    undef_method("reset_#{meth}_to_default!") if method_defined?("reset_#{meth}_to_default!")
  end
end

#remove_field(name) ⇒ Field

Removes the field from the Document. A getter and setter will be removed.

Examples:

Remove the field

Model.remove_field :score

Parameters:

  • name (Symbol)

    The name of the field.

Returns:

  • (Field)

    The removed field



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mongoid/suicide.rb', line 23

def remove_field(name)
  name = name.to_s

  return unless fields[name]

  aliased = fields[name].options[:as]

  remove_accessors(name, name)
  remove_accessors(name, aliased) if aliased
  remove_dirty_methods(name)
  remove_dirty_methods(aliased) if aliased

  remove_defaults(name)

  remove_field_in_descendants(name)
  remove_validations_for(name)

  aliased_fields.delete(aliased.to_s) if aliased
  fields.delete(name)
end

#remove_field_check(meth) ⇒ Object

Remove the check method for the provided field.

Examples:

Remove the check.

Model.remove_field_check("name")

Parameters:

  • meth (String)

    The name of the method.



180
181
182
183
184
# File 'lib/mongoid/suicide.rb', line 180

def remove_field_check(meth)
  generated_methods.module_eval do
    undef_method("#{meth}?") if method_defined?("#{meth}?")
  end
end

#remove_field_getter(meth) ⇒ Object

Remove the getter method for the provided field.

Examples:

Remove the getter.

Model.remove_field_getter("name")

Parameters:

  • meth (String)

    The name of the method.



144
145
146
147
148
# File 'lib/mongoid/suicide.rb', line 144

def remove_field_getter(meth)
  generated_methods.module_eval do
    undef_method(meth) if method_defined?(meth)
  end
end

#remove_field_getter_before_typecast(meth) ⇒ Object

Remove the getter_before_type_cast method for the provided field.

Examples:

Remove the getter_before_type_cast.

Model.remove_field_getter_before_type_cast("name")

Parameters:

  • meth (String)

    The name of the method.



156
157
158
159
160
# File 'lib/mongoid/suicide.rb', line 156

def remove_field_getter_before_typecast(meth)
  generated_methods.module_eval do
    undef_method("#{meth}_before_type_cast") if method_defined?("#{meth}_before_type_cast")
  end
end

#remove_field_in_descendants(name) ⇒ Object

Removes the field from descendants.

Parameters:

  • name (String)

    The name of the field



101
102
103
# File 'lib/mongoid/suicide.rb', line 101

def remove_field_in_descendants(name)
  descendants.each { |descendant| descendant.remove_field(name) }
end

#remove_field_setter(meth) ⇒ Object

Remove the setter method for the provided field.

Examples:

Remove the setter.

Model.remove_field_setter("name")

Parameters:

  • meth (String)

    The name of the method.



168
169
170
171
172
# File 'lib/mongoid/suicide.rb', line 168

def remove_field_setter(meth)
  generated_methods.module_eval do
    undef_method("#{meth}=") if method_defined?("#{meth}=")
  end
end

#remove_translations_getter(meth) ⇒ Object

Remove the translation getter method for the provided field.

Examples:

Remove the translation getter.

Model.remove_translations_getter("name")

Parameters:

  • meth (String)

    The name of the method.



192
193
194
195
196
197
# File 'lib/mongoid/suicide.rb', line 192

def remove_translations_getter(meth)
  generated_methods.module_eval do
    undef_method("#{meth}_translations") if method_defined?("#{meth}_translations")
    undef_method("#{meth}_t") if method_defined?("#{meth}_t")
  end
end

#remove_translations_setter(meth) ⇒ Object

Remove the translation setter method for the provided field.

Examples:

Remove the translation setter.

Model.remove_translations_setter("name")

Parameters:

  • meth (String)

    The name of the method.



205
206
207
208
209
210
# File 'lib/mongoid/suicide.rb', line 205

def remove_translations_setter(meth)
  generated_methods.module_eval do
    undef_method("#{meth}_translations=") if method_defined?("#{meth}_translations=")
    undef_method("#{meth}_t=") if method_defined?("#{meth}_t=")
  end
end

#remove_validate_callbacks(a_name) ⇒ Object

Removes validate callbacks for the field.

Examples:

Remove validate callbacks.

Model.remove_validate_callbacks([:name])

Parameters:

  • a_name (Array<Symbol>)

    The attribute name.



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/mongoid/suicide.rb', line 125

def remove_validate_callbacks(a_name)
  chain = _validate_callbacks.dup.reject do |callback|
    f = callback.filter
    f.respond_to?(:attributes) && f.attributes == a_name
  end

  reset_callbacks(:validate)

  chain.each do |callback|
    set_callback 'validate', callback.filter
  end
end

#remove_validations_for(name) ⇒ Object

Removes validations for the field.

Examples:

Remove validations.

Model.remove_validations_for("name")

Parameters:

  • name (String)

    The attribute name.



111
112
113
114
115
116
117
# File 'lib/mongoid/suicide.rb', line 111

def remove_validations_for(name)
  name = name.to_sym
  a_name = [name]

  _validators.reject! { |key, _| key == name }
  remove_validate_callbacks a_name
end