Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/custom_fields/extensions/active_support.rb

Instance Method Summary collapse

Instance Method Details

#constantize_with_custom_fieldsObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/custom_fields/extensions/active_support.rb', line 3

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 { || [: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