Sha256: ba14a3170819b4fc54d90ba3869d8615855d359797c9d4b990b8008b4f4b93d6

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module BetterRecord
  class PolymorphicOverride
    @@debugging_override = nil

    def self.debug=(val)
      @@debugging_override = !!val
    end

    def self.debug
      !!@@debugging_override
    end

    def self.polymorphic_value(klass, options = nil)
      type_val = nil
      type_method = polymorphic_method(options.presence || {})
      begin
        type_val = klass.__send__(type_method)
      rescue
        puts "Error in Polymorphic Value:",
          type_method, klass, type_val,
          $!.message, $!.backtrace if debug

        if type_val == :table_name_without_schema
          type_val = klass.table_name.to_s.split('.').first
        else
          type_val = klass.polymorphic_name
        end
      end
      type_val
    end

    def self.polymorphic_method(options = {})
      (options[:primary_type].presence) || BetterRecord.default_polymorphic_method.presence || :polymorphic_name
    end

    def self.all_types(klass)
      keys = [ :polymorphic_name, :full_table_name, :table_name_only, :table_name ]
      keys |= [BetterRecord.default_polymorphic_method] if BetterRecord.default_polymorphic_method.present?
      p "Polymorphic methods:", keys if debug
      values = []
      keys.each do |k|
        val = nil
        begin
          val = klass.__send__(k)
          values << val if val.present?
        rescue
          p "Error in Polymorphic Method, #{k}:", $!.message, $!.backtrace if debug
        end
      end
      p "Present Polymorphic Values:", values if debug
      values
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
better_record-0.23.6 lib/better_record/polymorphic_override.rb
better_record-0.23.5 lib/better_record/polymorphic_override.rb
better_record-0.23.4 lib/better_record/polymorphic_override.rb
better_record-0.23.3 lib/better_record/polymorphic_override.rb