Module: Mongoid::Orderable::ClassMethods

Defined in:
lib/mongoid/orderable.rb

Instance Method Summary collapse

Instance Method Details

#orderable(options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mongoid/orderable.rb', line 5

def orderable options = {}
  configuration = {
    :column => :position,
    :index => true,
    :scope => nil,
    :base => 1
  }

  configuration.merge! options if options.is_a?(Hash)

  if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/
    scope_relation = self.relations[configuration[:scope].to_s]
    if scope_relation
      configuration[:scope] = scope_relation.key.to_sym
    else
      configuration[:scope] = "#{configuration[:scope]}_id".to_sym
    end
  elsif configuration[:scope].is_a?(String)
    configuration[:scope] = configuration[:scope].to_sym
  end

  field configuration[:column], orderable_field_opts(configuration)
  if configuration[:index]
    if MongoidOrderable.mongoid2?
      index configuration[:column]
    else
      index(configuration[:column] => 1)
    end
  end

  case configuration[:scope]
  when Symbol then
    scope :orderable_scope, lambda { |document|
      where(configuration[:scope] => document.send(configuration[:scope])) }
  when Proc then
    scope :orderable_scope, configuration[:scope]
  else
    scope :orderable_scope, lambda { |document| where({}) }
  end

  define_method :orderable_column do
    configuration[:column]
  end

  define_method :orderable_base do
    configuration[:base]
  end

  self_class = self
  define_method :orderable_inherited_class do
    self_class if configuration[:inherited]
  end

  before_save :add_to_list
  after_destroy :remove_from_list
end