Sha256: 326e79e0f5118a354efa836baceddf22d543d51ed4bf7d568b49a82f420fd352

Contents?: true

Size: 1.37 KB

Versions: 74

Compression:

Stored size: 1.37 KB

Contents

require "action"

module RecordableActions
  extend ActiveSupport::Concern

  included do
    embeds_many :actions

    def latest_status_action(type = nil)
      if type
        self.actions.where(request_type: type).last
      else
        most_recent_action(&:status_action?)
      end
    end

    def last_fact_checked_at
      last_fact_check = actions.reverse.find(&:is_fact_check_request?)
      last_fact_check ? last_fact_check.created_at : NullTimestamp.new
    end

    def new_action(user, type, options={})
      actions.create!(options.merge(requester_id: user.id, request_type: type))
    end

    def new_action_without_validation(user, type, options={})
      action = actions.build(options.merge(requester_id: user.id, request_type: type))
      save(validate: false)
      action
    end

    def most_recent_action(&blk)
      self.actions.sort_by(&:created_at).reverse.find(&blk)
    end

    def created_by
      creation = actions.detect do |a|
        a.request_type == Action::CREATE || a.request_type == Action::NEW_VERSION
      end
      creation.requester if creation
    end

    def published_by
      publication = actions.where(request_type: Action::PUBLISH).first
      publication.requester if publication
    end

    def archived_by
      publication = actions.where(request_type: Action::ARCHIVE).first
      publication.requester if publication
    end
  end
end

Version data entries

74 entries across 74 versions & 1 rubygems

Version Path
govuk_content_models-47.0.0 app/traits/recordable_actions.rb
govuk_content_models-46.0.1 app/traits/recordable_actions.rb
govuk_content_models-46.0.0 app/traits/recordable_actions.rb
govuk_content_models-45.0.0 app/traits/recordable_actions.rb
govuk_content_models-44.4.0 app/traits/recordable_actions.rb
govuk_content_models-44.3.0 app/traits/recordable_actions.rb
govuk_content_models-44.2.1 app/traits/recordable_actions.rb
govuk_content_models-44.2.0 app/traits/recordable_actions.rb
govuk_content_models-44.1.0 app/traits/recordable_actions.rb
govuk_content_models-44.0.1 app/traits/recordable_actions.rb
govuk_content_models-44.0.0 app/traits/recordable_actions.rb
govuk_content_models-43.2.0 app/traits/recordable_actions.rb
govuk_content_models-43.1.0 app/traits/recordable_actions.rb
govuk_content_models-43.0.1 app/traits/recordable_actions.rb
govuk_content_models-43.0.0 app/traits/recordable_actions.rb
govuk_content_models-42.1.0 app/traits/recordable_actions.rb
govuk_content_models-42.0.1 app/traits/recordable_actions.rb
govuk_content_models-42.0.0 app/traits/recordable_actions.rb
govuk_content_models-41.1.1 app/traits/recordable_actions.rb
govuk_content_models-41.1.0 app/traits/recordable_actions.rb