Module: Mongoid::Listable::Macros::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#listedObject

Macro to set basic position field on an object

Parameters:

  • options (Hash)

    The options hash

Returns:

  • self

Since:

  • 0.1.0



18
19
20
21
22
23
24
25
26
27
# File 'lib/mongoid/listable/macros.rb', line 18

def listed
  field :position, type: Integer

  created(:position)
    .updated(:position)
    .destroyed(:position)

  scope :list, order_by(:position => :asc)
  self
end

#lists(name, options = {}) ⇒ Mongoid:Relations:Metadata

Macro to set relation on which to make a list

Parameters:

  • name (Symbol)

    The name of the has_many relation

  • options (Hash) (defaults to: {})

    The options hash

Returns:

  • (Mongoid:Relations:Metadata)

    Instance of metadata

Since:

  • 0.0.1



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mongoid/listable/macros.rb', line 37

def lists name, options={}
  meta = reflect_on_association name

  field_name = field_name(meta)
  meta.klass.send :field, field_name, type: Integer

  ids_set(name, meta).set(name, meta)
    .added(name, meta).removed(name, meta)

  meta.klass.send :include, Mongoid::Listable
  meta.klass.updated(field_name).destroyed(field_name)

  meta[:order] ||= "#{field_name(meta)} asc"
end