Sha256: 42100cca15ede89d65f52a642e9673fda7e13a799aa52c82433597f44793aa57

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

require 'oj'
require 'active_support/concern'
require 'active_support/core_ext/module/delegation'
require 'pg_serializable/errors'
require 'pg_serializable/visitable'
require 'pg_serializable/nodes'
require 'pg_serializable/trait_manager'
require 'pg_serializable/trait'
require 'pg_serializable/visitors'

module ActiveRecord
  class Relation
    include PgSerializable::Visitable

    def json(trait: :default)
      to_pg_json accept(PgSerializable::Visitors::Json.new, trait: trait)
    end
  end
end

module PgSerializable
  extend ActiveSupport::Concern

  def self.validate_traits!
    ActiveRecord::Base.descendants.each do |klass|
      begin
        klass.validate_traits!
      rescue NoMethodError, ActiveRecord::NoDatabaseError
      end
    end
  end

  included do
    include Visitable

    def json(trait: :default)
      self.class.to_pg_json accept(PgSerializable::Visitors::Json.new, trait: trait)
    end
  end

  class_methods do
    def json(trait: :default)
      to_pg_json accept(PgSerializable::Visitors::Json.new, trait: trait)
    end

    def serializable(&blk)
      trait_manager.instance_eval &blk
    end

    def trait_manager
      @trait_manager ||= TraitManager.new(self)
    end

    def accept visitor, **kwargs
      visitor.visit self, **kwargs
    end

    def to_pg_json(scope)
      res = scope.as_json.first
      ::Oj.dump(res['coalesce'] || res['json_build_object'])
    end

    private

    delegate :validate_traits!, to: :trait_manager
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pg_serializable-2.1.1 lib/pg_serializable.rb
pg_serializable-2.1.0 lib/pg_serializable.rb
pg_serializable-2.0.0 lib/pg_serializable.rb