Module: Volt::Buffer

Included in:
Model
Defined in:
lib/volt/models/buffer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



106
107
108
109
# File 'lib/volt/models/buffer.rb', line 106

def self.included(base)
  base.include ReactiveAccessors
  base.reactive_accessor :__save_to
end

Instance Method Details

#bufferObject

Returns a buffered version of the model



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/volt/models/buffer.rb', line 89

def buffer
  model_path = options[:path]

  model_klass = self.class

  new_options = options.merge(path: model_path, save_to: self, buffer: true).reject { |k, _| k.to_sym == :persistor }

  model = nil
  Volt::Model.no_validate do
    model = model_klass.new(attributes, new_options, :loaded)

    model.instance_variable_set('@new', @new)
  end

  model
end

#buffer?Boolean

Returns:



84
85
86
# File 'lib/volt/models/buffer.rb', line 84

def buffer?
  options[:buffer] || false
end

#promise_for_errors(errors) ⇒ Object

When errors come in, we mark all fields and return a rejected promise.



75
76
77
78
79
80
81
82
# File 'lib/volt/models/buffer.rb', line 75

def promise_for_errors(errors)
  mark_all_fields!

  # Wrap in an Errors class unless it already is one
  errors = errors.is_a?(Errors) ? errors : Errors.new(errors)

  Promise.new.reject(errors)
end

#save!(&block) ⇒ Object

Save saves the contents of a buffer to the save_to location. If the buffer is new, it will create a new model to save to. Otherwise it will be an existing model. Save returns a promise that may fail with validation errors from the server (or the client). You can also pass a block as a shortcut to calling “‘.save!.then do“`



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
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/volt/models/buffer.rb', line 9

def save!(&block)
  # TODO: this shouldn't need to be run, but if no attributes are assigned, then
  # if needs to be run.  Maybe there's a better way to handle it.
  validate!.then do
    # Get errors from validate
    errors = self.errors.to_h

    result = nil

    if errors.size == 0
      # cache save_to in a local
      save_to = self.save_to
      if save_to
        if save_to.is_a?(ArrayModel)
          # Add to the collection
          promise = save_to.create(attributes)
        else
          # We have a saved model
          promise = save_to.assign_attributes(attributes)
        end

        result = promise.then do |new_model|
          # The main model saved, so mark the buffer as not new
          @new = false

          if new_model
            # Mark the model as loaded
            new_model.change_state_to(:loaded_state, :loaded)

            # Set the buffer's id to track the main model's id
            self.save_to = new_model
          end

          # Copy attributes back from save_to model
          @attributes = new_model.attributes

          # Remove tracked changes
          clear_tracked_changes!

          new_model
        end.fail do |errors|
          if errors.is_a?(Hash)
            server_errors.replace(errors)

            # Merge the server errors into the main errors
            self.errors.merge!(server_errors.to_h)
          end

          promise_for_errors(errors)
        end
      else
        fail 'Model is not a buffer, can not be saved, modifications should be persisted as they are made.'
      end
    else
      # Some errors, mark all fields
      result = promise_for_errors(errors)
    end

    # If passed a block, call then on it with the block.
    result = result.then(&block) if block

    result
  end
end

#save_toObject



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/volt/models/buffer.rb', line 111

def save_to
  val = __save_to

  unless val
    # Lazy load the save_to
    self.save_to = options[:save_to]
    return __save_to
  end

  val
end

#save_to=(val) ⇒ Object



123
124
125
# File 'lib/volt/models/buffer.rb', line 123

def save_to=(val)
  self.__save_to = val
end

#saved?Boolean

Returns:



138
139
140
# File 'lib/volt/models/buffer.rb', line 138

def saved?
  saved_state == :saved
end

#saved_stateObject



127
128
129
130
131
132
133
134
135
136
# File 'lib/volt/models/buffer.rb', line 127

def saved_state
  if buffer?
    # cache save_to in local
    lsave_to = self.save_to

    lsave_to.try(:saved_state) || :not_saved
  else
    super
  end
end