Sha256: 7dc115ccb72ba344eb2563533e6203b2f8910e73a10d2ac409ef32d9035096ec

Contents?: true

Size: 982 Bytes

Versions: 3

Compression:

Stored size: 982 Bytes

Contents

# frozen_string_literal: true

module CustomFieldsStringExtension
  def constantize
    super
  rescue NameError => e
    # DEBUG: puts "constantizing #{self.inspect}"
    # alright, does it look like a custom_fields dynamic klass ?
    if self =~ CustomFields::KLASS_REGEXP
      base = ::Regexp.last_match(1).constantize
      # we can know it for sure
      if base.with_custom_fields?
        relation = base.relations.values.detect do |association|
          association.options[:custom_fields_parent_klass] == true
        end

        # load the class which holds the recipe to build the dynamic klass
        if relation && parent_instance = relation.klass.find(::Regexp.last_match(2))
          # DEBUG: puts "re-building #{self}"
          return parent_instance.klass_with_custom_fields(relation.inverse_of)
        end
      end
    end
    # not a custom_fields dynamic klass or unable to re-build it
    raise e
  end
end

::String.prepend CustomFieldsStringExtension

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
custom_fields-2.14.0.alpha1 lib/custom_fields/extensions/active_support.rb
custom_fields-2.13.1 lib/custom_fields/extensions/active_support.rb
custom_fields-2.13.0 lib/custom_fields/extensions/active_support.rb