Sha256: b30cc8ad3cd1f07fabb5d2d9c7602df282029230093883340c4c23be30e7e23f

Contents?: true

Size: 1.09 KB

Versions: 11

Compression:

Stored size: 1.09 KB

Contents

require 'active_support/concern'

module ApplicationRecordConcern
  extend ActiveSupport::Concern
  included do
    after_validation :validation_ko

    after_commit :message_ok

    after_rollback :message_ko

    def validation_ko
      ActionCable.server.broadcast("messages", build_message(false, false, self.errors.full_messages.uniq)) if self.errors.any? && !is_model_forbidden
    end

    def message_ok
      ActionCable.server.broadcast("messages", build_message(true, true, [])) unless is_model_forbidden
    end

    def message_ko
      ActionCable.server.broadcast("messages", build_message(false, true, [])) unless is_model_forbidden
    end

    def is_model_forbidden
      [ 'User', 'Role' ].include?(self.class.name)
    end

    def build_message success, valid, errors
      { topic: :record, action: detect_action, class: self.class.name, success: success, valid: valid, errors: errors, record: self}
    end

    def detect_action
      return :create if transaction_include_any_action?([:create])
      return :update if transaction_include_any_action?([:update])
      :destroy
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
thecore_backend_commons-3.2.4 config/initializers/concern_application_record.rb
thecore_backend_commons-3.2.3 config/initializers/concern_application_record.rb
thecore_backend_commons-3.2.2 config/initializers/concern_application_record.rb
thecore_backend_commons-3.2.1 config/initializers/concern_application_record.rb
thecore_backend_commons-3.2.0 config/initializers/concern_application_record.rb
thecore_backend_commons-3.1.9 config/initializers/concern_application_record.rb
thecore_backend_commons-3.1.8 config/initializers/concern_application_record.rb
thecore_backend_commons-3.1.7 config/initializers/concern_application_record.rb
thecore_backend_commons-3.1.6 config/initializers/concern_application_record.rb
thecore_backend_commons-3.1.5 config/initializers/concern_application_record.rb
thecore_backend_commons-3.1.4 config/initializers/concern_application_record.rb