Sha256: c920998ce65497a7a1b21e6cc64fc8209a701df440384a9d02a78fada182f069

Contents?: true

Size: 975 Bytes

Versions: 27

Compression:

Stored size: 975 Bytes

Contents

class String

  def constantize_with_custom_fields
    begin
      constantize_without_custom_fields
    rescue NameError => exception
      # DEBUG: puts "constantizing #{self.inspect}"
      # alright, does it look like a custom_fields dynamic klass ?
      if self =~ /(.*)([0-9a-fA-F]{24})$/
        base = $1.constantize
        # we can know it for sure
        if base.with_custom_fields?
          relation = base.relations.values.detect { |metadata| metadata[:custom_fields_parent_klass] == true }

          # load the class which holds the recipe to build the dynamic klass
          if relation && parent_instance = relation.klass.find($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 exception
    end
  end

  alias_method_chain :constantize, :custom_fields
end

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
custom_fields-2.8.0 lib/custom_fields/extensions/active_support.rb
custom_fields-2.7.0 lib/custom_fields/extensions/active_support.rb
custom_fields-2.7.0.rc1 lib/custom_fields/extensions/active_support.rb
custom_fields-2.6.0 lib/custom_fields/extensions/active_support.rb
custom_fields-2.5.0 lib/custom_fields/extensions/active_support.rb
custom_fields-2.5.0.rc1 lib/custom_fields/extensions/active_support.rb
custom_fields-2.4.0.rc8 lib/custom_fields/extensions/active_support.rb
custom_fields-2.4.0.rc7 lib/custom_fields/extensions/active_support.rb
custom_fields-2.4.0.rc6 lib/custom_fields/extensions/active_support.rb
custom_fields-2.3.4 lib/custom_fields/extensions/active_support.rb
custom_fields-2.4.0.rc5 lib/custom_fields/extensions/active_support.rb
custom_fields-2.3.3 lib/custom_fields/extensions/active_support.rb
custom_fields-2.4.0.rc4 lib/custom_fields/extensions/active_support.rb
custom_fields-2.4.0.rc3 lib/custom_fields/extensions/active_support.rb
custom_fields-2.4.0.rc2 lib/custom_fields/extensions/active_support.rb
custom_fields-2.3.2 lib/custom_fields/extensions/active_support.rb
custom_fields-2.4.0.rc1 lib/custom_fields/extensions/active_support.rb
gnuside-custom_fields-2.3.2.2 lib/custom_fields/extensions/active_support.rb
gnuside-custom_fields-2.3.2 lib/custom_fields/extensions/active_support.rb
gnuside-custom_fields-2.3.1.1 lib/custom_fields/extensions/active_support.rb