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.



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

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.



72
73
74
# File 'app/models/workarea/release/changeset.rb', line 72

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

.latest(limit = Workarea.config.per_page) ⇒ Object



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

def self.latest(limit = Workarea.config.per_page)
  order(updated_at: :desc).limit(limit)
end

.summary(release_id) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/workarea/release/changeset.rb', line 31

def self.summary(release_id)
  collection.aggregate([
    { '$match' => { 'release_id' => release_id } },
    {
      '$addFields' => {
        'root' => { '$arrayElemAt' => ['$document_path', 0] }
      }
    },
    {
      '$group' => {
        '_id' => '$root.type',
        'count' => { '$sum' => 1 }
      }
    },
    { '$sort' => { '_id' => 1 } }
  ]).to_a
end

Instance Method Details

#apply_to(model) ⇒ Object

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



89
90
91
# File 'app/models/workarea/release/changeset.rb', line 89

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

#build_undo(attributes = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/workarea/release/changeset.rb', line 110

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



76
77
78
# File 'app/models/workarea/release/changeset.rb', line 76

def changed_fields
  changeset.keys
end

#includes_change?(key, new_value) ⇒ Boolean



80
81
82
# File 'app/models/workarea/release/changeset.rb', line 80

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.



98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/workarea/release/changeset.rb', line 98

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



136
137
138
139
140
141
142
143
144
145
# File 'app/models/workarea/release/changeset.rb', line 136

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