Sha256: dd04248119518fdbb95a24390964050019bd7702c218fb53ed220e3f4c5e6003

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 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
    include Naming

    self.abstract_class = true

    before_create :setear_creado_y_actualizado_por
    before_update :setear_actualizado_por

    scope :unkept, -> { discarded }
    # ransacker :search do |parent|
    #   parent.table[:nombre]
    # end

    # Para el dom_id (index.html)
    def to_key
      if respond_to? :hashid
        [hashid]
      else
        super
      end
    end

    def to_s
      %i[nombre name].each do |campo|
        # Using `_in_database` for consistent breadcrumbs when editing the name
        campo = :"#{campo}_in_database"

        return "#{send(campo)} ##{to_param}" if try(campo).present?
      end
      if to_param.present?
        "#{self.class.nombre_singular} ##{to_param}"
      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

3 entries across 3 versions & 1 rubygems

Version Path
pg_rails-7.6.20 pg_engine/app/models/pg_engine/base_record.rb
pg_rails-7.6.19 pg_engine/app/models/pg_engine/base_record.rb
pg_rails-7.6.18 pg_engine/app/models/pg_engine/base_record.rb