Module: MmPartialUpdate::Plugins::PartialUpdate::InstanceMethods

Defined in:
lib/mm_partial_update/plugins/partial_update.rb

Instance Method Summary collapse

Instance Method Details

#add_updates_to_command(command) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mm_partial_update/plugins/partial_update.rb', line 58

def add_updates_to_command(command)

  selector = respond_to?(:database_selector) ? database_selector : nil

  add_create_self_to_command(selector, command) and return if new?

  field_changes = persistable_changes

  associations.values.each do |association|
    proxy = get_proxy(association)
    association_changes = field_changes.delete(association.name)
    proxy.add_updates_to_command(association_changes, command) if
      proxy.respond_to?(:add_updates_to_command)
  end

  field_changes = field_changes.inject({}) do |changes,change|
    changes[change[0]] = change[-1][-1]
    changes
  end
  command.tap {|c|c.set(selector,field_changes)}
end

#create_or_update_changes(options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mm_partial_update/plugins/partial_update.rb', line 41

def create_or_update_changes(options={})
  #assert_root_saved
  update_command  = prepare_update_command
  execute_command(update_command, options)
  #callbacks and changes_only are not valid
  # options for regular MongoMapper saves.
  # clear_changes_to_subtree will pass this options
  # hash to downstream saves, which can errors
  # so they need to be removed
  options.reject!{ |k,v|[:callbacks, :changes_only].include?(k)}
  clear_changes_to_subtree(options)
end

#persistable_changesObject

Since we are going to persist the values directly from the changes hash, we need to make sure they are propertly readied for storage



83
84
85
86
87
88
89
90
91
# File 'lib/mm_partial_update/plugins/partial_update.rb', line 83

def persistable_changes
  changes.tap do |persistable_changes|
    persistable_changes.each_pair do |key_name, value|
      if (key = keys[key_name])
        value[-1] = key.set(value[-1])
      end
    end
  end
end

#prepare_update_commandObject



54
55
56
# File 'lib/mm_partial_update/plugins/partial_update.rb', line 54

def prepare_update_command
  UpdateCommand.new(self).tap { |command| add_updates_to_command(command) }
end

#save_changes(options = {}) ⇒ Object



30
31
32
33
34
# File 'lib/mm_partial_update/plugins/partial_update.rb', line 30

def save_changes(options={})
  options.assert_valid_keys(:validate, :callbacks, :safe, :changes_only)
  options.reverse_merge!(:validate=>true, :callbacks=>true)
  !options[:validate] || valid? ? create_or_update_changes(options) : false
end

#save_changes!(options = {}) ⇒ Object



36
37
38
39
# File 'lib/mm_partial_update/plugins/partial_update.rb', line 36

def save_changes!(options={})
  options.assert_valid_keys(:callbacks, :safe, :changes_only)
  save_changes(options) || raise(MongoMapper::DocumentNotValid.new(self))
end