Sha256: 0710e59341e15f7e25e7d36cc9969bf7dfdf3eeb1f0c57ec090aefa04564c2d8

Contents?: true

Size: 1.45 KB

Versions: 10

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module PgEngine
  # rubocop:disable Rails/ApplicationRecord
  class BaseRecord < ActiveRecord::Base
    # rubocop:enable Rails/ApplicationRecord
    extend Enumerize
    include PrintHelper
    include PostgresHelper

    self.abstract_class = true

    attr_accessor :current_user

    before_create :setear_creado_y_actualizado_por
    before_update :setear_actualizado_por

    scope :query, ->(param) { param.present? ? where(id: param) : all }

    def self.ransackable_associations(_auth_object = nil)
      authorizable_ransackable_associations
    end

    def self.ransackable_attributes(_auth_object = nil)
      authorizable_ransackable_attributes
    end

    def self.nombre_plural
      model_name.human(count: 2)
    end

    def self.nombre_singular
      model_name.human(count: 1)
    end

    def to_s
      %i[nombre name].each do |campo|
        return "#{send(campo)} ##{id}" if try(campo).present?
      end
      if id.present?
        "#{self.class.nombre_singular} ##{id}"
      else
        super
      end
    end

    private

    def setear_creado_y_actualizado_por
      setear_si_existe :creado_por, current_user
      setear_si_existe :actualizado_por, current_user
    end

    def setear_actualizado_por
      setear_si_existe :actualizado_por, current_user
    end

    def setear_si_existe(campo, valor)
      metodo = "#{campo}="
      send(metodo, valor) if respond_to?(metodo) && valor.present?
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pg_rails-7.0.8.pre.alpha.8 pg_engine/app/models/pg_engine/base_record.rb
pg_rails-7.0.8.pre.alpha.7 pg_engine/app/models/pg_engine/base_record.rb
pg_rails-7.0.8.pre.alpha.6 pg_engine/app/models/pg_engine/base_record.rb
pg_rails-7.0.8.pre.alpha pg_engine/app/models/pg_engine/base_record.rb
pg_rails-7.0.7 pg_engine/app/models/pg_engine/base_record.rb
pg_rails-7.0.6 pg_engine/app/models/pg_engine/base_record.rb
pg_rails-7.0.5 pg_engine/app/models/pg_engine/base_record.rb
pg_rails-7.0.4 pg_engine/app/models/pg_engine/base_record.rb
pg_rails-7.0.3 pg_engine/app/models/pg_engine/base_record.rb
pg_rails-7.0.2 pg_engine/app/models/pg_engine/base_record.rb