Module: Workarea::Releasable

Extended by:
ActiveSupport::Concern
Includes:
Mongoid::DocumentPath
Included in:
Catalog::Category, Catalog::Product, Catalog::Variant, Content, Content::Block, Content::Page, Navigation::Menu, Pricing::Discount, Pricing::Price, Pricing::Sku, Search::Customization
Defined in:
app/models/workarea/releasable.rb

Instance Method Summary collapse

Instance Method Details

#activates_with_current_release?Boolean

Whether this model becomes active with the current release. Used for some funny business when displaying content blocks in the admin. :(

Returns:

  • (Boolean)


67
68
69
70
# File 'app/models/workarea/releasable.rb', line 67

def activates_with_current_release?
  return false if Release.current.blank?
  active? && active_changed? && !was_active?
end

#releasable?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'app/models/workarea/releasable.rb', line 114

def releasable?
  true
end

#release_changesHash

A hash of changes for being set on the changeset. It’s just a filtered version of #changes from Mongoid.

Returns:

  • (Hash)


37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/workarea/releasable.rb', line 37

def release_changes
  changes.keys.inject({}) do |memo, key|
    old_value, new_value = *changes[key]

    if Release::Changeset.track_change?(key, old_value, new_value)
      memo[key] = new_value
    end

    memo
  end
end

#save_changeset(release_id) ⇒ Object

Persist a to be recalled for publishing later. This is where changesets make it to the database.

Will raise an error if the persistence goes wrong (it shouldn’t)

Parameters:



79
80
81
82
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
108
109
110
111
112
# File 'app/models/workarea/releasable.rb', line 79

def save_changeset(release_id)
  return unless release_id.present?

  changeset = Release::Changeset.find_or_initialize_by(
    releasable_id: id,
    releasable_type: self.class.name,
    release_id: release_id
  )

  if changeset.persisted? && release_changes.present?
    # This is to avoid triggering callbacks - calling #save on a
    # persisted Changeset is causing Mongoid to run callbacks on the
    # parent document, which resets the changeset changes to previous
    # values, not allowing updates to a changeset. #set merges a hash
    # field's new value into the old, so a call to #unset is necessary
    # to ensure any removed changes are properly deleted.
    #
    # TODO check in with this before v3 to see if Mongoid has fixed or
    # open a Mongoid PR
    #
    changeset.unset(:changeset)
    changeset.set(changeset: release_changes)
  elsif release_changes.present?
    changeset.document_path = document_path
    changeset.changeset = release_changes
    changeset.save!
  elsif changeset.persisted?
    changeset.destroy
  end

  changes.each do |field, change|
    attributes[field] = change.first
  end
end

#skip_changesetObject

Skip the release changeset for the duration of the block. Used when publishing a changeset, i.e. don’t apply/save the release changes since we actually want to publish.

Returns:

  • whatever the block returns



55
56
57
58
59
60
# File 'app/models/workarea/releasable.rb', line 55

def skip_changeset
  @_skip_changeset = true
  result = yield
  @_skip_changeset = false
  result
end