Sha256: f8d80fdba185e8ed99f6fef2ee16d513d62f402b9e32dd9fa8f900698fe41900

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

module Elabs
  module Concerns
    module ActableEntity
      extend ActiveSupport::Concern

      included do
        has_many :acts, as: :content

        before_save :update_acts?
        after_save :update_acts

        before_destroy :update_acts_on_delete
      end

      private

      def update_acts?
        @action = if changed.include?('published')
                    current_publish_action
                  elsif changed.include?('locked') && published?
                    current_lock_action
                  elsif (changed & %w[title name description short_description content excerpt]).any?
                    current_update_action
                  else
                    :nothing
                  end
      end

      def current_publish_action
        published? && unlocked? ? :publish : :unpublish
      end

      def current_lock_action
        published? && locked? ? :lock : :unlock
      end

      def current_update_action
        published? && unlocked? ? :update : :nothing
      end

      def update_acts
        acts.destroy_all if %i[lock unpublish].include? @action
        Act.create content: self, event: @action unless @action == :nothing || hidden_in_history
      end

      # TODO: check if used ? (dependent: destroy may do the trick.)
      def update_acts_on_delete
        acts.destroy_all
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
elabs-2.0.0 app/models/elabs/concerns/actable_entity.rb