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

Methods included from Mongoid::Document

#embedded_children

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:



30
31
32
33
34
35
# File 'app/models/workarea/release/changeset.rb', line 30

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:



45
46
47
# File 'app/models/workarea/release/changeset.rb', line 45

def self.by_document_path_type(klass)
  where('document_path.type' => klass.name)
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:



62
63
64
# File 'app/models/workarea/release/changeset.rb', line 62

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

#build_undo(attributes = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/workarea/release/changeset.rb', line 83

def build_undo(attributes = {})
  # Ensure the appropriate Release.current for building the undo
  # This can be nil, which is essential if there is some other arbitrary
  # release as Release.current.
  Release.with_current(release.previous) do
    releasable_from_document_path.reload

    Changeset.new(
      attributes.reverse_merge(
        releasable: releasable,
        document_path: document_path,
        changeset: changeset.keys.inject({}) do |memo, key|
          old_value = releasable_from_document_path.attributes[key]
          new_value = changeset[key]

          memo[key] = old_value if Changes.tracked_change?(key, old_value, new_value)
          memo
        end
      )
    )
  end

ensure
  releasable_from_document_path.reload
end

#changed_fieldsObject



49
50
51
# File 'app/models/workarea/release/changeset.rb', line 49

def changed_fields
  changeset.keys
end

#includes_change?(key, new_value) ⇒ Boolean

Returns:

  • (Boolean)


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

def includes_change?(key, new_value)
  changed_fields.include?(key) && changeset[key] == new_value
end

#publish!Boolean

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

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/workarea/release/changeset.rb', line 71

def publish!
  return false if releasable_from_document_path.blank?

  apply_to(releasable_from_document_path)

  releasable_from_document_path.skip_changeset do
    releasable_from_document_path.save!
  end

  save!
end

#releasable_from_document_pathObject



109
110
111
112
113
114
115
116
117
118
# File 'app/models/workarea/release/changeset.rb', line 109

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