Class: Workarea::Release::Changeset
- Inherits:
-
Object
- Object
- Workarea::Release::Changeset
- Includes:
- ApplicationDocument
- Defined in:
- app/models/workarea/release/changeset.rb
Class Method Summary collapse
-
.by_document_path(document) ⇒ Mongoid::Criteria
Finds changeset by whether the passed document is in the document path of the changeset.
-
.by_document_path_type(klass) ⇒ Mongoid::Criteria
Find changesets by whether the passed class is in the document path of the changeset.
Instance Method Summary collapse
-
#apply_to(model) ⇒ Object
Apply (but do not save) the changes represented by this changeset to the model passed in.
- #build_undo(attributes = {}) ⇒ Object
- #changed_fields ⇒ Object
- #includes_change?(key, new_value) ⇒ Boolean
-
#publish! ⇒ Boolean
Make the changes represented by this changeset live.
- #releasable_from_document_path ⇒ Object
Methods included from ApplicationDocument
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
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.
31 32 33 34 35 36 |
# File 'app/models/workarea/release/changeset.rb', line 31 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.
46 47 48 |
# File 'app/models/workarea/release/changeset.rb', line 46 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.
63 64 65 |
# File 'app/models/workarea/release/changeset.rb', line 63 def apply_to(model) apply_changeset(model, changeset) end |
#build_undo(attributes = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'app/models/workarea/release/changeset.rb', line 84 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_fields ⇒ Object
50 51 52 |
# File 'app/models/workarea/release/changeset.rb', line 50 def changed_fields changeset.keys end |
#includes_change?(key, new_value) ⇒ Boolean
54 55 56 |
# File 'app/models/workarea/release/changeset.rb', line 54 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.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/models/workarea/release/changeset.rb', line 72 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_path ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/workarea/release/changeset.rb', line 110 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 |