Module: Workarea::Releasable

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

Instance Method Summary collapse

Methods included from Segmentable

#active?, #active_segment_ids_with_children, #segmented?, #segments

Methods included from Workarea::Release::Activation

#activate_with?, #create_activation_changeset, #save_activate_with, #was_active?

Instance Method Details

#changesets_with_childrenObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/workarea/releasable.rb', line 30

def changesets_with_children
  criteria = Release::Changeset.any_of(
    { releasable_type: self.class.name, releasable_id: id }
  )

  embedded_children.each do |child|
    if child.respond_to?(:changesets_with_children)
      criteria.merge!(child.changesets_with_children)
    end
  end

  criteria
end

#destroyObject



132
133
134
135
136
137
138
# File 'app/models/workarea/releasable.rb', line 132

def destroy(*)
  if embedded? && Release.current.present?
    update!(active: false)
  else
    super
  end
end

#in_release(release) ⇒ Releasable

Get a new instance of this model loaded with changes for the release passed in.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/workarea/releasable.rb', line 62

def in_release(release)
  if release.blank? && !changed? # No extra work necessary, return a copy
    result = dup
    result.id = id
    result.release_id = nil
    result
  elsif release.present? && !changed? # We don't have to reload from DB, just apply release changes to a copy
    result = dup
    result.id = id
    result.release_id = release.id
    release.preview.changesets_for(self).each { |cs| cs.apply_to(result) }
    result
  else
    Release.with_current(release) { self.class.find(id) }
  end
end

#release_changesHash

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



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

def release_changes
  ::Workarea::Release::Changes.new(self).to_h
end

#release_originalsObject



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

def release_originals
  ::Workarea::Release::Changes.new(self).to_originals_h
end

#save_changeset(release) ⇒ 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)



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

def save_changeset(release)
  return unless release.present?

  changeset = release.changesets.find_or_initialize_by(releasable: self)

  run_callbacks :save_release_changes do
    if changeset.persisted? && release_changes.present?
      changeset.update!(changeset: release_changes, original: release_originals)
    elsif release_changes.present?
      changeset.document_path = document_path
      changeset.changeset = release_changes
      changeset.original = release_originals
      changeset.save!
    elsif changeset.persisted?
      changeset.destroy
    end
  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.



94
95
96
97
98
99
100
# File 'app/models/workarea/releasable.rb', line 94

def skip_changeset
  @_skip_changeset = true
  yield

ensure
  @_skip_changeset = false
end

#without_releaseReleasable

Get a new instance of this model without any release changes. This a new instance without any release changes applied.



84
85
86
# File 'app/models/workarea/releasable.rb', line 84

def without_release
  in_release(nil)
end