Module: Mongoid::Listable::Accessors::ClassMethods

Defined in:
lib/mongoid/listable/accessors.rb

Instance Method Summary collapse

Instance Method Details

#ids_set(name, meta) ⇒ Object

Redefines the default ids setter, ensuring that the order of objects sent to the relation setter corresponds to the order of the array of ids

Parameters:

  • name (Symbol)

    The name of the has_many relation

  • meta (MetaData)

    The MetaData class

Returns:

  • (Object)

    self

Since:

  • 0.0.6



19
20
21
22
23
24
25
# File 'lib/mongoid/listable/accessors.rb', line 19

def ids_set name, meta
  ids_method = "#{name.to_s.singularize}_ids="
  redefine_method ids_method do |ids|            
    send meta.setter, meta.klass.find(ids).sort_by_attr(:id, ids)
  end
  self
end

#set(name, meta) ⇒ Object

Prepends to the default setter a block that sets the position attribute on each object according to its index in the array

Parameters:

  • name (Symbol)

    The name of the has_many relation

  • meta (MetaData)

    The MetaData class

Returns:

  • (Object)

    self

Since:

  • 0.0.6



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mongoid/listable/accessors.rb', line 36

def set name, meta
  before_method "#{name}=" do |objects|
    objects ||= []
    
    reposition objects, position_field_name(meta), 1

    (send(name).to_a - objects).each do |object|
      object.unset position_field_name(meta)
    end
  end
  self
end