Module: Mongoid::Relations::Accessors::ClassMethods
- Defined in:
- lib/mongoid/relations/accessors.rb
Instance Method Summary collapse
-
#existence_check(name) ⇒ Class
Adds the existence check for relations.
-
#getter(name, metadata) ⇒ Class
Defines the getter for the relation.
-
#ids_getter(name, metadata) ⇒ Class
Defines the getter for the ids of documents in the relation.
-
#ids_setter(name, metadata) ⇒ Object
Defines the setter method that allows you to set documents in this relation by their ids.
-
#setter(name, metadata) ⇒ Class
Defines the setter for the relation.
Instance Method Details
#existence_check(name) ⇒ Class
Adds the existence check for relations.
161 162 163 164 165 166 167 168 169 |
# File 'lib/mongoid/relations/accessors.rb', line 161 def existence_check(name) module_eval <<-END, __FILE__, __LINE__ + 1 def #{name}? without_autobuild { !__send__(:#{name}).blank? } end alias :has_#{name}? :#{name}? END self end |
#getter(name, metadata) ⇒ Class
Defines the getter for the relation. Nothing too special here: just return the instance variable for the relation if it exists or build the thing.
184 185 186 187 188 189 190 191 192 193 |
# File 'lib/mongoid/relations/accessors.rb', line 184 def getter(name, ) re_define_method(name) do |reload = false| value = get_relation(name, , nil, reload) if value.nil? && .autobuilding? && !without_autobuild? value = send("build_#{name}") end value end self end |
#ids_getter(name, metadata) ⇒ Class
Defines the getter for the ids of documents in the relation. Should be specify only for referenced many relations.
204 205 206 207 208 209 210 |
# File 'lib/mongoid/relations/accessors.rb', line 204 def ids_getter(name, ) ids_method = "#{name.to_s.singularize}_ids" re_define_method(ids_method) do send(name).only(:id).map(&:id) end self end |
#ids_setter(name, metadata) ⇒ Object
Defines the setter method that allows you to set documents in this relation by their ids. The defined setter, finds documents with given ids and invokes regular relation setter with found documents. Ids setters should be defined only for referenced many relations.
@param [ String, Symbol ] name The name of the relation.
@param [ Metadata ] metadata The metadata for the relation.
@return [ Class ] The class being set up.
257 258 259 260 261 262 263 |
# File 'lib/mongoid/relations/accessors.rb', line 257 def ids_setter(name, ) ids_method = "#{name.to_s.singularize}_ids=" re_define_method(ids_method) do |ids| send(.setter, .klass.find(ids.reject(&:blank?))) end self end |
#setter(name, metadata) ⇒ Class
Defines the setter for the relation. This does a few things based on some conditions. If there is an existing association, a target substitution will take place, otherwise a new relation will be created with the supplied target.
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/mongoid/relations/accessors.rb', line 226 def setter(name, ) re_define_method("#{name}=") do |object| without_autobuild do if value = get_relation(name, , object) if value.respond_to?(:substitute) set_relation(name, value.substitute(object.substitutable)) else value = __build__(name, value, ) set_relation(name, value.substitute(object.substitutable)) end else __build__(name, object.substitutable, ) end end end self end |