lib/supernova/criteria.rb in supernova-0.3.12 vs lib/supernova/criteria.rb in supernova-0.3.13

- old
+ new

@@ -1,10 +1,10 @@ class Supernova::Criteria DEFAULT_PER_PAGE = 25 FIRST_PAGE = 1 - attr_accessor :filters, :search_options, :clazz + attr_accessor :filters, :search_options, :clazz, :results class << self def method_missing(*args) scope = self.new if scope.respond_to?(args.first) @@ -136,12 +136,26 @@ end def to_parameters implement_in_subclass end - + + def populate + @results = execute if !populated? + self + end + def to_a + populate + results + end + + def populated? + instance_variables.include?("@results") + end + + def execute implement_in_subclass end def current_page pagination_attribute_when_greater_zero(:page) || 1 @@ -168,11 +182,12 @@ end self end def method_missing(*args, &block) - if args.length == 1 && Array.new.respond_to?(args.first) - to_a.send(args.first, &block) + if Supernova::Collection.instance_methods.include?(args.first.to_s) + populate + @results.send(*args, &block) elsif self.named_scope_defined?(args.first) self.merge(self.search_options[:named_scope_class].send(*args)) # merge named scope and current criteria else super end \ No newline at end of file