Class: Workarea::Release

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document::Taggable, ApplicationDocument, Commentable
Defined in:
app/models/workarea/release.rb,
app/models/workarea/release/status.rb,
app/models/workarea/release/changes.rb,
app/models/workarea/release/preview.rb,
app/models/workarea/release/changeset.rb,
app/models/workarea/release/activation.rb

Defined Under Namespace

Modules: Activation, Status Classes: Changes, Changeset, Preview

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Commentable

#add_subscription, #remove_subscription

Methods included from Mongoid::Document::Taggable

included

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

assert_valid_config!, async, disable, enable, inline, #run_callbacks

Methods included from Mongoid::Document

#embedded_children

Class Method Details

.currentObject



60
61
62
# File 'app/models/workarea/release.rb', line 60

def self.current
  Thread.current[:current_release]
end

.current=(release) ⇒ Object



64
65
66
# File 'app/models/workarea/release.rb', line 64

def self.current=(release)
  Thread.current[:current_release] = release
end

.published_within(start_date, end_date) ⇒ Object

Get a list of releases published or to be published within a given range

@ return [Array<Release>]



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/workarea/release.rb', line 114

def self.published_within(start_date, end_date)
  results = where(
    :publish_at.gte => start_date.to_time,
    :publish_at.lte => end_date.to_time
  )

  results += where(
    :published_at.gte => start_date.to_time,
    :published_at.lte => end_date.to_time
  )

  results.uniq.sort_by { |r| [r.publish_at || 0, r.published_at || 0] }
end

.sort_by_publishObject



128
129
130
# File 'app/models/workarea/release.rb', line 128

def self.sort_by_publish
  scoped.sort_by { |r| [r.publish_at, r.created_at] }
end

.unscheduledArray<Release>

Gets a list of unscheduled releases

Returns:



96
97
98
# File 'app/models/workarea/release.rb', line 96

def self.unscheduled
  all.and(not_scheduled.selector, not_published.selector).desc(:created_at).to_a
end

.upcomingArray<Release>

Gets a list of unpublished releases sorted by when they will be published.

Returns:



105
106
107
# File 'app/models/workarea/release.rb', line 105

def self.upcoming
  self.unscheduled + scheduled.desc(:publish_at).to_a
end

.with_current(release) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/workarea/release.rb', line 68

def self.with_current(release)
  previous = current

  new_current = if release.is_a?(Release)
    release
  elsif release.present?
    find(release) rescue nil
  end

  self.current = new_current
  current&.reset_preview
  yield

ensure
  self.current = previous

  current&.reset_preview
  previous&.reset_preview
end

.without_current(&block) ⇒ Object



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

def self.without_current(&block)
  with_current(nil, &block)
end

Instance Method Details

#as_currentObject



132
133
134
# File 'app/models/workarea/release.rb', line 132

def as_current
  self.class.with_current(self) { yield }
end

#build_undo(attributes = {}) ⇒ Object



174
175
176
177
178
179
180
181
182
# File 'app/models/workarea/release.rb', line 174

def build_undo(attributes = {})
  result = undo || Release.new(attributes)

  result.name ||= I18n.t('workarea.release.undo', name: name)
  result.tags = %w(undo) if result.tags.blank?
  self.undo = result

  result
end

#has_changes?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'app/models/workarea/release.rb', line 148

def has_changes?
  changesets.present?
end

#ordered_changesetsArray<Workarea::Release::Changeset>

Get changesets ordered based on publish priority set by configuration.

Returns:



215
216
217
218
219
220
221
# File 'app/models/workarea/release.rb', line 215

def ordered_changesets
  ordering = Workarea.config.release_changeset_ordering

  changesets.sort_by do |changeset|
    ordering[changeset.releasable_type].presence || 999
  end
end

#previewObject



152
153
154
# File 'app/models/workarea/release.rb', line 152

def preview
  @preview ||= Preview.new(self)
end

#previousObject



170
171
172
# File 'app/models/workarea/release.rb', line 170

def previous
  scheduled_before.last
end

#publish!Object



184
185
186
187
188
189
190
191
# File 'app/models/workarea/release.rb', line 184

def publish!
  self.published_at = Time.current
  self.publish_at = nil
  save!

  ordered_changesets.each(&:publish!)
  touch_releasables
end

#published?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'app/models/workarea/release.rb', line 140

def published?
  !!published_at
end

#reset_previewObject



156
157
158
# File 'app/models/workarea/release.rb', line 156

def reset_preview
  @preview = nil
end

#scheduled?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'app/models/workarea/release.rb', line 136

def scheduled?
  !!publish_at && (persisted? && !publish_at_changed?)
end

#scheduled_afterObject



165
166
167
168
# File 'app/models/workarea/release.rb', line 165

def scheduled_after
  return [] unless scheduled?
  self.class.scheduled(after: publish_at).ne(id: id).sort_by_publish
end

#scheduled_beforeObject



160
161
162
163
# File 'app/models/workarea/release.rb', line 160

def scheduled_before
  return [] unless scheduled?
  self.class.scheduled(before: publish_at).ne(id: id).sort_by_publish
end

#set_publish_jobObject



193
194
195
196
197
198
199
200
# File 'app/models/workarea/release.rb', line 193

def set_publish_job
  self.publish_job_id = Scheduler.schedule(
    worker: PublishRelease,
    at: publish_at,
    args: [id.to_s],
    job_id: publish_job_id
  )
end

#statusesArray<Symbol>

Get all statuses of this release.

Returns:

  • (Array<Symbol>)


206
207
208
209
# File 'app/models/workarea/release.rb', line 206

def statuses
  calculators = Workarea.config.release_status_calculators.map(&:constantize)
  StatusCalculator.new(calculators, self).results
end

#upcoming?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'app/models/workarea/release.rb', line 144

def upcoming?
  scheduled? || (!scheduled? && !published?)
end