Class: Artefact

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

Constant Summary collapse

FORMATS_BY_DEFAULT_OWNING_APP =
{
  "publisher"               => ["answer",
                                "business_support",
                                "campaign",
                                "completed_transaction",
                                "guide",
                                "help_page",
                                "licence",
                                "local_transaction",
                                "place",
                                "programme",
                                "simple_smart_answer",
                                "transaction",
                                "video"],
  "smartanswers"            => ["smart-answer"],
  "custom-application"      => ["custom-application"], # In this case the owning_app is overriden. eg calendars, licencefinder
  "travel-advice-publisher" => ["travel-advice"],
  "specialist-publisher"    => ["manual"],
  "finder-api"              => ["finder",
                                "finder_email_signup"],
  "whitehall"               => ["announcement",
                                "authored_article",
                                "case_study",
                                "consultation",
                                "corporate_information_page",
                                "corporate_report",
                                "correspondence",
                                "decision",
                                "detailed_guide",
                                "document_collection",
                                "draft_text",
                                "fatality_notice",
                                "foi_release",
                                "form",
                                "government_response",
                                "guidance",
                                "impact_assessment",
                                "independent_report",
                                "international_treaty",
                                "map",
                                "national_statistics",
                                "news_story",
                                "notice",
                                "official_statistics",
                                "oral_statement",
                                "policy",
                                "policy_paper",
                                "press_release",
                                "promotional",
                                "publication",
                                "regulation",
                                "research",
                                "speaking_notes",
                                "statistical_data_set",
                                "statistics",
                                "statutory_guidance",
                                "supporting_page",
                                "transcript",
                                "transparency",
                                "world_location_news_article",
                                "worldwide_priority",
                                "written_statement"],
}.freeze
RETIRED_FORMATS =
%w[ business_support campaign programme video]
FORMATS =
FORMATS_BY_DEFAULT_OWNING_APP.values.flatten
KIND_TRANSLATIONS =
{
  "standard transaction link"        => "transaction",
  "local authority transaction link" => "local_transaction",
  "completed/done transaction" => "completed_transaction",
  "benefit / scheme"                 => "programme",
  "find my nearest"                  => "place",
}.tap { |h| h.default_proc = -> _, k { k } }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_app_for_format(format) ⇒ Object



125
126
127
# File 'app/models/artefact.rb', line 125

def self.default_app_for_format(format)
  FORMATS_BY_DEFAULT_OWNING_APP.detect { |app, formats| formats.include?(format) }.first
end

.find_by_slug(s) ⇒ Object



163
164
165
# File 'app/models/artefact.rb', line 163

def self.find_by_slug(s)
  where(slug: s).first
end

.from_param(slug_or_id) ⇒ Object



221
222
223
# File 'app/models/artefact.rb', line 221

def self.from_param(slug_or_id)
  find_by_slug(slug_or_id) || find(slug_or_id)
end

.in_alphabetical_orderObject



159
160
161
# File 'app/models/artefact.rb', line 159

def self.in_alphabetical_order
  order_by(name: :asc)
end

Instance Method Details

#admin_url(options = {}) ⇒ Object



177
178
179
180
181
# File 'app/models/artefact.rb', line 177

def admin_url(options = {})
  [ "#{Plek.current.find(owning_app)}/admin/publications/#{id}",
    options.to_query
  ].reject(&:blank?).join("?")
end

#any_editions_ever_published?Boolean

Returns:

  • (Boolean)


193
194
195
196
# File 'app/models/artefact.rb', line 193

def any_editions_ever_published?
  Edition.where(panopticon_id: self.id,
                :state.in => ['published', 'archived']).any?
end

#any_editions_published?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'app/models/artefact.rb', line 189

def any_editions_published?
  Edition.where(panopticon_id: self.id, state: 'published').any?
end

#archive_editionsObject



212
213
214
215
216
217
218
219
# File 'app/models/artefact.rb', line 212

def archive_editions
  if state == 'archived'
    Edition.where(panopticon_id: self.id, :state.nin => ["archived"]).each do |edition|
      edition.new_action(self, "note", comment: "Artefact has been archived. Archiving this edition.")
      edition.perform_event_without_validations(:archive!)
    end
  end
end

#archived?Boolean

Returns:

  • (Boolean)


281
282
283
# File 'app/models/artefact.rb', line 281

def archived?
  self.state == "archived"
end

#as_json(options = {}) ⇒ Object



183
184
185
186
187
# File 'app/models/artefact.rb', line 183

def as_json(options={})
  super.tap { |hash|
    hash["id"] = hash.delete("_id")
  }
end

#languageObject

Fallback to english if no language is present



168
169
170
# File 'app/models/artefact.rb', line 168

def language
  attributes['language'] || "en"
end

#live?Boolean

Returns:

  • (Boolean)


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

def live?
  self.state == "live"
end

#need_id=(new_need_id) ⇒ Object



294
295
296
297
298
299
# File 'app/models/artefact.rb', line 294

def need_id=(new_need_id)
  super

  need_ids << new_need_id if new_need_id.present? && ! need_ids.include?(new_need_id)
  new_need_id
end

#normaliseObject



172
173
174
175
# File 'app/models/artefact.rb', line 172

def normalise
  return unless kind.present?
  self.kind = KIND_TRANSLATIONS[kind.to_s.downcase.strip]
end

#record_action(action_type, options = {}) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'app/models/artefact.rb', line 256

def record_action(action_type, options={})
  user = options[:user]
  task_name = options[:task_name]
  current_snapshot = snapshot
  last_snapshot = actions.last.snapshot if actions.last

  unless current_snapshot == last_snapshot

    attributes = {
      action_type: action_type,
      snapshot: current_snapshot,
    }

    attributes.merge!(user: user) if user
    attributes.merge!(task_performed_by: task_name) if task_name

    new_action = actions.build(attributes)
    # Mongoid will not fire creation callbacks on embedded documents, so we
    # need to trigger this manually. There is a `cascade_callbacks` option on
    # `embeds_many`, but it doesn't appear to trigger creation events on
    # children when an update event fires on the parent
    new_action.set_created_at
  end
end

#record_create_actionObject



248
249
250
# File 'app/models/artefact.rb', line 248

def record_create_action
  record_action "create"
end

#record_update_actionObject



252
253
254
# File 'app/models/artefact.rb', line 252

def record_update_action
  record_action "update"
end

#save_as(user, options = {}) ⇒ Object



230
231
232
233
234
235
# File 'app/models/artefact.rb', line 230

def save_as(user, options={})
  default_action = new_record? ? "create" : "update"
  action_type = options.delete(:action_type) || default_action
  record_action(action_type, user: user)
  save(options)
end

#save_as_task!(task_name, options = {}) ⇒ Object

We should use this method when performing save actions from rake tasks, message queue consumer or any other performed tasks that have no user associated as we are still interested to know what triggered the action.



240
241
242
243
244
245
246
# File 'app/models/artefact.rb', line 240

def save_as_task!(task_name, options = {})
  default_action = new_record? ? "create" : "update"
  action_type = options.delete(:action_type) || default_action

  record_action(action_type, task_name: task_name)
  save!(options)
end

#snapshotObject



289
290
291
292
# File 'app/models/artefact.rb', line 289

def snapshot
  attributes
    .except("_id", "created_at", "updated_at", "actions")
end

#update_attributes_as(user, *args) ⇒ Object



225
226
227
228
# File 'app/models/artefact.rb', line 225

def update_attributes_as(user, *args)
  assign_attributes(*args)
  save_as user
end

#update_editionsObject



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/models/artefact.rb', line 198

def update_editions
  case state
  when 'draft'
    if self.slug_changed?
      Edition.where(:state.nin => ["archived"],
                    panopticon_id: self.id).each do |edition|
        edition.update_slug_from_artefact(self)
      end
    end
  when 'archived'
    archive_editions
  end
end