lib/trackoid/aggregates.rb in trackoid-0.1.4 vs lib/trackoid/aggregates.rb in trackoid-0.1.5

- old
+ new

@@ -107,13 +107,13 @@ # Returns true if there is a class defined with the same name as our # aggregate class. def foreign_class_defined? # The following construct doesn't work with namespaced constants. # Object.const_defined?(internal_aggregates_name.to_sym) - - cn = internal_aggregates_name.constantize rescue nil - !cn.nil? + internal_aggregates_name.constantize && true + rescue NameError + false end # Adds the aggregate field to the array of aggregated fields. def add_aggregate_field(name, block) aggregate_fields[name] = block @@ -121,10 +121,13 @@ # Defines the aggregation external class. This class is named after # the original class model but with "Aggregates" appended. # Example: TestModel ==> TestModelAggregates def define_klass(&block) - klass = Object.const_set(internal_aggregates_name, Class.new) + scope = internal_aggregates_name.split('::') + klass = scope.pop + scope = scope.inject(Kernel) {|scope, const_name| scope.const_get(const_name)} + klass = scope.const_set(klass, Class.new) klass.class_eval(&block) end def create_aggregation_accessors(name) # Aggregation accessors in the model acts like a named scope