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"],
}.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



83
84
85
# File 'app/models/artefact.rb', line 83

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



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

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

.from_param(slug_or_id) ⇒ Object



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

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

.in_alphabetical_orderObject



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

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

Instance Method Details

#admin_url(options = {}) ⇒ Object



135
136
137
138
139
# File 'app/models/artefact.rb', line 135

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)


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

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

#any_editions_published?Boolean

Returns:

  • (Boolean)


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

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

#archive_editionsObject



170
171
172
173
174
175
176
177
# File 'app/models/artefact.rb', line 170

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)


239
240
241
# File 'app/models/artefact.rb', line 239

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

#as_json(options = {}) ⇒ Object



141
142
143
144
145
# File 'app/models/artefact.rb', line 141

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

#languageObject

Fallback to english if no language is present



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

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

#live?Boolean

Returns:

  • (Boolean)


243
244
245
# File 'app/models/artefact.rb', line 243

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

#need_id=(new_need_id) ⇒ Object



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

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



130
131
132
133
# File 'app/models/artefact.rb', line 130

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

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



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'app/models/artefact.rb', line 214

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



206
207
208
# File 'app/models/artefact.rb', line 206

def record_create_action
  record_action "create"
end

#record_update_actionObject



210
211
212
# File 'app/models/artefact.rb', line 210

def record_update_action
  record_action "update"
end

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



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

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.



198
199
200
201
202
203
204
# File 'app/models/artefact.rb', line 198

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



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

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

#update_attributes_as(user, *args) ⇒ Object



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

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

#update_editionsObject



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/models/artefact.rb', line 156

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