Class: VersionedRecord::AttributeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/versioned_record/attribute_builder.rb

Overview

Builds a set of attributes for a new version of a record

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ AttributeBuilder

New Builder

Parameters:



7
8
9
# File 'lib/versioned_record/attribute_builder.rb', line 7

def initialize(record)
  @record = record
end

Instance Method Details

#attributes(new_attrs) ⇒ Object

Get new attributes hash for the new version Attributes missing in new_attrs will be filled in from the previous version (specified in the constructor)

Parameters:

  • new_attrs (Hash)

    are the attributes we are changing in this version



16
17
18
19
20
21
22
23
# File 'lib/versioned_record/attribute_builder.rb', line 16

def attributes(new_attrs)
  @new_attrs = new_attrs.symbolize_keys
  attrs = original_attributes.merge(@new_attrs.merge({
    is_current_version: true,
    id:                 @record.id,
    version:            @record.version + 1
  }))
end

#original_attributesObject

The relevant attributes of the previous version i.e: the record we initialized this builder with

Returns:

  • Hash excluding created_at and updated_at keys



30
31
32
33
34
# File 'lib/versioned_record/attribute_builder.rb', line 30

def original_attributes
  @record.attributes.symbolize_keys.select do |(attr, _)|
    !%i(created_at updated_at).include?(attr)
  end
end