Class: Workarea::Release::Changeset

Inherits:
Object
  • Object
show all
Includes:
ApplicationDocument
Defined in:
app/models/workarea/release/changeset.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

add_worker, assert_valid_config!, async, caching_classes?, disable, enable, inline, #run_callbacks, workers, workers_list

Class Method Details

.by_document_path(document) ⇒ Mongoid::Criteria

Finds changeset by whether the passed document is in the document path of the changeset. Useful for showing embedded changes in the admin, e.g. showing content block changes as part of the timeline for the content object.

Parameters:

Returns:



38
39
40
41
42
43
# File 'app/models/workarea/release/changeset.rb', line 38

def self.by_document_path(document)
  where(
    'document_path.type' => document.class.name,
    'document_path.document_id' => document.id.to_s
  )
end

.by_document_path_type(klass) ⇒ Mongoid::Criteria

Find changesets by whether the passed class is in the document path of the changeset. Used in the admin for display embedded changesets by parent type, e.g. filtering activity by content and seeing content block changes.

Parameters:

  • klass (Class)

Returns:



53
54
55
# File 'app/models/workarea/release/changeset.rb', line 53

def self.by_document_path_type(klass)
  where('document_path.type' => klass.name)
end

.track_change?(attribute, old_value, new_value) ⇒ Boolean

Whether these value changes to this field should be included when saving a changeset. Used in building changeset hashes.

Parameters:

  • attribute (String)
  • old_value (Object)
  • new_value (Object)

Returns:

  • (Boolean)


25
26
27
28
# File 'app/models/workarea/release/changeset.rb', line 25

def self.track_change?(attribute, old_value, new_value)
  !attribute.in?(Workarea.config.untracked_release_changes_fields) &&
    (old_value.present? || new_value.present?)
end

Instance Method Details

#apply_to(model) ⇒ Object

Apply (but do not save) the changes represented by this changeset to the model passed in.

Parameters:



66
67
68
# File 'app/models/workarea/release/changeset.rb', line 66

def apply_to(model)
  apply_changeset(model, changeset)
end

#changed_fieldsObject



57
58
59
# File 'app/models/workarea/release/changeset.rb', line 57

def changed_fields
  changeset.keys
end

#publish!Boolean

Make the changes represented by this changeset live. Used when publishing a release.

Builds and saves the undo in case that’s desired later.

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/workarea/release/changeset.rb', line 77

def publish!
  return false if releasable_from_document_path.blank?

  build_undo
  apply_to(releasable_from_document_path)

  releasable_from_document_path.skip_changeset do
    releasable_from_document_path.save!
  end

  save! # saves undo
end

#releasable_from_document_pathObject



103
104
105
106
107
108
109
110
111
112
# File 'app/models/workarea/release/changeset.rb', line 103

def releasable_from_document_path
  return @releasable_from_document_path if defined?(@releasable_from_document_path)

  @releasable_from_document_path =
    begin
      Mongoid::DocumentPath.find(document_path)
    rescue Mongoid::Errors::DocumentNotFound
      nil
    end
end

#undo!Boolean

Apply the changes in the undo hash on this changeset and save to make them live. Used when undoing a release.

Returns:

  • (Boolean)


95
96
97
98
99
100
101
# File 'app/models/workarea/release/changeset.rb', line 95

def undo!
  apply_changeset(releasable_from_document_path, undo)

  releasable_from_document_path.skip_changeset do
    releasable_from_document_path.save!
  end
end