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

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

Instance Method Summary collapse

Instance Method Details

#listed(options = {}) ⇒ Object

Macro to set basic position field on an object

Parameters:

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

    The options hash

Returns:

  • self

Since:

  • 0.1.0



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

def listed options={}
  configuration = { field_name: :position }
  configuration.merge! options if options.is_a?(Hash)

  field_name = configuration[:field_name]

  field field_name, type: Integer

  created(field_name).updated(field_name).destroyed(field_name)

  scope :list, order_by(field_name => :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



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

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.updated(field_name).destroyed(field_name)

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