Sha256: 1de2caf77d8e525b3ec4bce2179120e4474ff3c037038b9b762d3f9a2d105984

Contents?: true

Size: 1.73 KB

Versions: 11

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

require 'carrierwave/orm/activerecord' if defined?(CarrierWave::Mount)

class Tramway::Core::ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
  audited
  extend ::Enumerize
  include ::AASM

  scope :created_by_user, lambda { |user_id|
    joins(:audits).where('audits.action = \'create\' AND audits.user_id = ?', user_id)
  }
  scope :admin_scope, -> (_arg) { all }

  # FIXME remove this after testing soft-deletion
  aasm column: :state do
    state :active, initial: true
    state :removed

    event :remove do
      transitions from: :active, to: :removed
    end
  end


  include ::PgSearch::Model

  def creator
    creation_event = audits.where(action: :create).first
    creation_event.user if creation_event.user_id.present?
  end

  # FIXME: detect inhertited locales
  class << self
    def human_attribute_name(attribute_name, *_args)
      excepted_attributes = %w[created_at updated_at]
      if attribute_name.to_s.in? excepted_attributes
        I18n.t "activerecord.attributes.tramway/core/application_record.#{attribute_name}"
      else
        super attribute_name
      end
    end

    def search_by(*attributes, **associations)
      pg_search_scope :full_text_search, against: attributes, associated_against: associations
    end

    def uploader(attribute_name, uploader_name, **options)
      mount_uploader attribute_name, "#{uploader_name.to_s.camelize}Uploader".constantize
      @versions = options[:versions] if uploader_name == :photo
      @extensions = options[:extensions]
    end

    def photo_versions
      @versions
    end

    def file_extensions
      @extensions
    end

    def state_machines_names
      AASM::StateMachineStore.fetch(self).machine_names
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
tramway-core-4.1 app/models/tramway/core/application_record.rb
tramway-core-4.0.2.1 app/models/tramway/core/application_record.rb
tramway-core-4.0.2 app/models/tramway/core/application_record.rb
tramway-core-4.0.1.2 app/models/tramway/core/application_record.rb
tramway-core-4.0.1.1 app/models/tramway/core/application_record.rb
tramway-core-4.0.1 app/models/tramway/core/application_record.rb
tramway-core-4.0.0.1 app/models/tramway/core/application_record.rb
tramway-core-4.0 app/models/tramway/core/application_record.rb
tramway-core-3.0.1.1 app/models/tramway/core/application_record.rb
tramway-core-3.0.1 app/models/tramway/core/application_record.rb
tramway-core-3.0.0.2 app/models/tramway/core/application_record.rb