Module: Mongoid::Listable::Callbacks::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#added(name, meta) ⇒ Object

Defines a mongoid has_many relation after_add callback. Sets the position attribute to current relations length + 1

Parameters:

  • name (Symbol)

    The name of the has_many relation

  • meta (MetaData)

    The MetaData class

Returns:

  • (Object)

    self

Since:

  • 0.0.6



82
83
84
85
86
87
88
89
90
91
# File 'lib/mongoid/listable/callbacks.rb', line 82

def added name, meta
  callback = "#{name.to_s.singularize}_added"
  define_method callback do |object|
    if object[field_name(meta)].nil?
      object.set field_name(meta), has_many_count(name) + 1
    end
  end
  meta[:before_add] = callback
  self
end

#created(name) ⇒ Object

Defines a mongoid before_create callback. Sets the position field to current object count + 1

Parameters:

  • The (Hash)

    configuration hash

Returns:

  • (Object)

    self

Since:

  • 0.1.0



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongoid/listable/callbacks.rb', line 17

def created name
  callback = "#{name}_#{__method__}"
  define_method callback do
    position = send name
    if position.present?
      siblings = siblings(name).gte name => position
      reposition siblings, name, position + 1
    else
      set name, siblings(name).count + 1
    end
  end

  before_create callback
  self
end

#destroyed(name) ⇒ Object

Defines a mongoid before_destroy callback.

Resets all sibling object’s higher in the list

Parameters:

  • The (Hash)

    configuration hash

Returns:

  • (Object)

    self

Since:

  • 0.1.0



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mongoid/listable/callbacks.rb', line 61

def destroyed name
  callback = "#{name}_#{__method__}"
  define_method callback do
    position = send name
    siblings = siblings(name).gt(name => position)
    reposition siblings, name, position
  end

  before_destroy callback
  self
end

#removed(name, meta) ⇒ Object

Defines a mongoid has_many relation before_remove callback. Resets the position index on all objects that came after

Parameters:

  • name (Symbol)

    The name of the has_many relation

  • meta (MetaData)

    The MetaData class

Returns:

  • (Object)

    self

Since:

  • 0.0.6



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/mongoid/listable/callbacks.rb', line 102

def removed name, meta       
  field_name = field_name meta
  callback   = "#{name.to_s.singularize}_removed"
  define_method callback do |object|
    position = object.send field_name
    reposition object.siblings(field_name).gt(field_name => position), 
    field_name, position
    object.unset field_name
  end

  meta[:before_remove] = callback
  self
end

#updated(name) ⇒ Object

Defines a mongoid before_update callback.

If the position column has changed, apply the change. Hoe the change is applied varies depending on the redrection of the update.

Parameters:

  • The (Hash)

    configuration hash

Returns:

  • (Object)

    self

Since:

  • 0.1.0



43
44
45
46
47
48
49
50
51
# File 'lib/mongoid/listable/callbacks.rb', line 43

def updated name
  callback = "#{name}_#{__method__}"
  define_method callback do       
    apply_change_on name if send("#{name}_changed?")
  end

  before_update callback
  self
end