Module: MongoMapper::Plugins::ActsAsList::ClassMethods

Defined in:
lib/mongo_mapper/plugins/acts_as_list.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



20
21
22
# File 'lib/mongo_mapper/plugins/acts_as_list.rb', line 20

def self.included(base)
  include Comparable
end

Instance Method Details

#acts_as_list(options = {}) ⇒ Object



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mongo_mapper/plugins/acts_as_list.rb', line 16

def acts_as_list(options = {})
  
  
  
  def self.included(base)
    include Comparable
  end
  
  
  
  configuration = { :column => "position", :scope => {} }
  configuration.update(options) if options.is_a?(Hash)
  configuration[:scope] = "#{configuration[:scope]}_id".intern if 
    configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/



  if configuration[:scope].is_a?(Symbol)
    define_method "scope_condition" do
      { configuration[:scope] => send(configuration[:scope].to_s) }.symbolize_keys!
    end
  elsif configuration[:scope].is_a?(Array)
    define_method "scope_condition" do
      configuration[:scope].inject({}) { |memo, column| 
        memo[column.intern] = send(column.intern)
        memo
      }.symbolize_keys!
    end
  else
    define_method "scope_condition" do
      configuration[:scope]
    end
  end
  
  
  
  define_method "position_column" do
    "#{configuration[:column]}"
  end
  
  
  
  key configuration[:column], Integer
  
  
  
  before_destroy :decrement_positions_on_lower_items
  before_create  :add_to_list_bottom
  
  
  
  class_eval <<-EOV          
    def acts_as_list_class
      ::#{self.name}
    end
  EOV
  
  
  
end