Class: Edition

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps, RecordableActions, Workflow
Defined in:
app/models/edition.rb

Constant Summary collapse

GOVSPEAK_FIELDS =
[]

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Workflow

#can_destroy?, #check_can_delete_and_notify, #denormalise_users!, #error_description, #fact_checked?, #important_note, #in_progress?, #locked_for_edits?, #mark_as_rejected, #notify_siblings_of_new_edition, #perform_event_without_validations, #previous_edition, #status_text

Class Attribute Details

.fields_to_cloneObject

Returns the value of attribute fields_to_clone.



72
73
74
# File 'app/models/edition.rb', line 72

def fields_to_clone
  @fields_to_clone
end

Class Method Details

.find_and_identify(slug, edition) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
# File 'app/models/edition.rb', line 242

def self.find_and_identify(slug, edition)
  scope = where(slug: slug)

  if edition.present? and edition == "latest"
    scope.order_by(:version_number).last
  elsif edition.present?
    scope.where(version_number: edition).first
  else
    scope.where(state: "published").order(version_number: "desc").first
  end
end

.find_or_create_from_panopticon_data(panopticon_id, importing_user, api_credentials) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'app/models/edition.rb', line 226

def self.find_or_create_from_panopticon_data(panopticon_id,
                                             importing_user, api_credentials)
  existing_publication = Edition.where(panopticon_id: panopticon_id)
                                .order_by([:version_number, :desc]).first
  return existing_publication if existing_publication

  raise "Artefact not found" unless  = Artefact.find(panopticon_id)

  importing_user.create_edition(.kind.to_sym,
    panopticon_id: .id,
    slug: .slug,
    title: .name,
    department: .department,
    business_proposition: .business_proposition)
end

Instance Method Details

#artefactObject



309
310
311
# File 'app/models/edition.rb', line 309

def artefact
  Artefact.find(panopticon_id)
end

#broadcast_action(callback_action) ⇒ Object

Stop broadcasting a delete message unless there are no siblings.



279
280
281
282
283
# File 'app/models/edition.rb', line 279

def broadcast_action(callback_action)
  unless callback_action == "destroyed" and self.siblings.any?
    super(callback_action)
  end
end

#build_clone(edition_class = nil) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'app/models/edition.rb', line 183

def build_clone(edition_class=nil)
  unless state == "published"
    raise "Cloning of non published edition not allowed"
  end
  unless can_create_new_edition?
    raise "Cloning of a published edition when an in-progress edition exists
           is not allowed"
  end

  edition_class = self.class unless edition_class
  new_edition = edition_class.new(title: self.title,
                                  version_number: get_next_version_number)

  real_fields_to_merge = fields_to_copy(edition_class) + [
    :panopticon_id,
    :overview,
    :slug,
    :department,
    :browse_pages,
    :primary_topic,
    :additional_topics
  ]

  real_fields_to_merge.each do |attr|
    new_edition[attr] = read_attribute(attr)
  end

  if edition_class == AnswerEdition and %w(GuideEdition ProgrammeEdition TransactionEdition).include?(self.class.name)
    new_edition.body = whole_body
  end

  if edition_class == TransactionEdition and %w(AnswerEdition GuideEdition ProgrammeEdition).include?(self.class.name)
    new_edition.more_information = whole_body
  end

  if edition_class == GuideEdition and self.is_a?(AnswerEdition)
    new_edition.parts.build(title: "Part One", body: whole_body,
                            slug: "part-one")
  end

  new_edition
end

#can_create_new_edition?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'app/models/edition.rb', line 113

def can_create_new_edition?
  !scheduled_for_publishing? && subsequent_siblings.in_progress.empty?
end

#check_for_archived_artefactObject



295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'app/models/edition.rb', line 295

def check_for_archived_artefact
  if panopticon_id
    a = Artefact.find(panopticon_id)
    if a.state == "archived" and changes.any?
      # If we're only changing the state to archived, that's ok
      # Any other changes are not allowed
      allowed_keys = ["state", "updated_at"]
      unless ((changes.keys - allowed_keys).empty?) and state == "archived"
        raise "Editing of an edition with an Archived artefact is not allowed"
      end
    end
  end
end

#destroy_artefactObject

When we delete an edition is the only one in its series we delete the associated artefact to remove all trace of the item from the system.

We don’t do this by notifying panopticon as this will only ever happen for artefacts representing editions that haven’t been published (and therefore aren’t registered in the rest of the) system.



321
322
323
324
325
# File 'app/models/edition.rb', line 321

def destroy_artefact
  if can_destroy? && siblings.empty?
    Artefact.find(self.panopticon_id).destroy
  end
end

#fields_to_copy(edition_class) ⇒ Object

If the new clone is of the same type, we can copy all its fields over; if we are changing the type of the edition, any fields other than the base fields will likely be meaningless.



179
180
181
# File 'app/models/edition.rb', line 179

def fields_to_copy(edition_class)
  edition_class == self.class ? self.class.fields_to_clone : []
end

#first_edition_of_publishedObject



143
144
145
# File 'app/models/edition.rb', line 143

def first_edition_of_published
  series.archived_or_published.order(version_number: "asc").first
end

#formatObject



258
259
260
# File 'app/models/edition.rb', line 258

def format
  self.class.to_s.gsub("Edition", "")
end

#format_nameObject



262
263
264
# File 'app/models/edition.rb', line 262

def format_name
  format
end

#get_next_version_numberObject



151
152
153
154
# File 'app/models/edition.rb', line 151

def get_next_version_number
  latest_version = series.order(version_number: "desc").first.version_number
  latest_version + 1
end

#has_ever_been_published?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'app/models/edition.rb', line 139

def has_ever_been_published?
  series.map(&:state).include?('published')
end

#has_sibling_in_progress?Boolean

Returns:

  • (Boolean)


274
275
276
# File 'app/models/edition.rb', line 274

def has_sibling_in_progress?
  ! sibling_in_progress.nil?
end

#has_video?Boolean

Returns:

  • (Boolean)


266
267
268
# File 'app/models/edition.rb', line 266

def has_video?
  false
end

#historyObject



81
82
83
# File 'app/models/edition.rb', line 81

def history
  series.order([:version_number, :desc])
end

#in_progress_siblingObject



109
110
111
# File 'app/models/edition.rb', line 109

def in_progress_sibling
  subsequent_siblings.in_progress.order(version_number: "desc").first
end

#indexable_contentObject



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

def indexable_content
  respond_to?(:parts) ? indexable_content_with_parts : indexable_content_without_parts
end

#indexable_content_with_partsObject



168
169
170
171
172
173
174
# File 'app/models/edition.rb', line 168

def indexable_content_with_parts
  content = indexable_content_without_parts
  return content unless published_edition
  parts.inject([content]) { |acc, part|
    acc.concat([part.title, Govspeak::Document.new(part.body).to_text])
  }.compact.join(" ").strip
end

#indexable_content_without_partsObject



160
161
162
163
164
165
166
# File 'app/models/edition.rb', line 160

def indexable_content_without_parts
  if respond_to?(:body)
    "#{Govspeak::Document.new(body).to_text}".strip
  else
    ""
  end
end

#latest_change_noteObject



125
126
127
128
129
# File 'app/models/edition.rb', line 125

def latest_change_note
  if latest_major_update.present?
    latest_major_update.change_note
  end
end

#latest_edition?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'app/models/edition.rb', line 97

def latest_edition?
  subsequent_siblings.empty?
end

#latest_major_updateObject



121
122
123
# File 'app/models/edition.rb', line 121

def latest_major_update
  major_updates_in_series.first
end

#major_updates_in_seriesObject



117
118
119
# File 'app/models/edition.rb', line 117

def major_updates_in_series
  history.published.major_updates
end

#meta_dataObject



147
148
149
# File 'app/models/edition.rb', line 147

def 
  PublicationMetadata.new self
end

#panopticon_uriObject



254
255
256
# File 'app/models/edition.rb', line 254

def panopticon_uri
  Plek.current.find("panopticon") + "/artefacts/" + (panopticon_id || slug).to_s
end

#previous_published_editionObject



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

def previous_published_edition
  series.where(state: "published").order(version_number: "desc").second
end

#previous_siblingsObject



89
90
91
# File 'app/models/edition.rb', line 89

def previous_siblings
  siblings.where(:version_number.lt => version_number)
end

#public_updated_atObject



131
132
133
134
135
136
137
# File 'app/models/edition.rb', line 131

def public_updated_at
  if latest_major_update.present?
    latest_major_update.updated_at
  elsif has_ever_been_published?
    first_edition_of_published.updated_at
  end
end

#published_editionObject



101
102
103
# File 'app/models/edition.rb', line 101

def published_edition
  series.where(state: "published").order(version_number: "desc").first
end

#safe_to_preview?Boolean

Returns:

  • (Boolean)


270
271
272
# File 'app/models/edition.rb', line 270

def safe_to_preview?
  true
end

#seriesObject



77
78
79
# File 'app/models/edition.rb', line 77

def series
  Edition.where(panopticon_id: panopticon_id)
end

#siblingsObject



85
86
87
# File 'app/models/edition.rb', line 85

def siblings
  series.excludes(id: id)
end

#subsequent_siblingsObject



93
94
95
# File 'app/models/edition.rb', line 93

def subsequent_siblings
  siblings.where(:version_number.gt => version_number)
end

#update_slug_from_artefact(artefact) ⇒ Object



290
291
292
293
# File 'app/models/edition.rb', line 290

def update_slug_from_artefact(artefact)
  self.slug = artefact.slug
  self.save!
end

#was_publishedObject



285
286
287
288
# File 'app/models/edition.rb', line 285

def was_published
  previous_siblings.each { |s| s.perform_event_without_validations(:archive) }
  notify_siblings_of_published_edition
end