Sha256: 48896086651552b7034b64b70a3d29eeb36bbbe4845abc1eff8410e1b9016666

Contents?: true

Size: 1.31 KB

Versions: 25

Compression:

Stored size: 1.31 KB

Contents

module PgEngine
  module ChildRecord
    extend ActiveSupport::Concern

    included do
      scope :kept, -> { undiscarded.joins(klass.parent_accessor).merge(klass.parent_klass.kept) }
      scope :unkept, -> { discarded.joins(klass.parent_accessor).or(klass.parent_klass.discarded) }
    end

    class_methods do
      attr_accessor :parent_accessor

      def parent_klass
        if parent_accessor.blank?
          # :nocov:
          raise PgEngine::Error, 'parent_accessor must be present'
          # :nocov:
        end

        reflection = reflect_on_all_associations.select { |r| r.name == parent_accessor.to_sym }.first
        if reflection.blank?
          # :nocov:
          raise PgEngine::Error, "#{parent_accessor} not an association on #{self}"
          # :nocov:
        end

        reflection.klass
      end
    end

    def parent?
      if self.class.parent_accessor.blank?
        # :nocov:
        raise PgEngine::Error, 'parent_accessor must be present'
        # :nocov:
      end

      true
    end

    def parent
      if self.class.parent_accessor.blank?
        # :nocov:
        raise PgEngine::Error, 'parent_accessor must be present'
        # :nocov:
      end

      send(self.class.parent_accessor)
    end

    def kept?
      undiscarded? && (parent.blank? || parent.kept?)
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
pg_rails-7.6.28 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.27 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.26 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.25 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.24 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.24.pre.5 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.24.pre.4 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.24.pre.3 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.23 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.22 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.22.pre.3 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.22.pre.2 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.22.pre.1 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.21 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.21.pre.11 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.21.pre.10 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.21.pre.9 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.21.pre.8 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.21.pre.7 pg_engine/app/models/concerns/pg_engine/child_record.rb
pg_rails-7.6.21.pre.6 pg_engine/app/models/concerns/pg_engine/child_record.rb