Sha256: 4afca9c6ac9db2fbad8582631091b878ae52b42564ee9c273530b4546c671435

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

module Ambition
  class Context

    def find *args
      with_context_scope(to_hash) { owner.find *args }
    end
    def find_with_destroyed *args
      with_context_scope(to_hash) { owner.find_with_destroyed *args }
    end
    def find_destroyed_only *args
      with_context_scope(to_hash) { owner.find_destroyed_only *args }
    end

    def count *args
      with_context_scope(to_hash) { owner.count *args }
    end
    alias_method :size, :count

    def count_with_destroyed *args
      with_context_scope(to_hash) { owner.count_with_destroyed *args }
    end
    def count_destroyed_only *args
      with_context_scope(to_hash) { owner.count_destroyed_only *args }
    end

    def paginate *args
      with_context_scope(to_hash) { owner.paginate *args }
    end

    def within outer_scope, join_association = nil
      context = ambition_context

      unless outer_scope.nil?
        join_association ||= outer_scope.owner.symbolize
        raise "#{owner} doesn't have a #{join_association.inspect} association. Make sure it's spelt and pluralized correctly and speficied as a symbol." unless context.owner.reflections.has_key?(join_association)

        (context.stash[:joins] ||= []).push(join_association).concat(outer_scope.stash[:joins] || []).uniq!
        context.clauses[:select].concat(outer_scope.clauses[:select]).uniq!
      end

      context
    end


    protected

    def with_context_scope find_conditions, &block
      owner.send :with_scope, :find => find_conditions do
        yield
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
benhoskings-ambitious-activerecord-0.1.3.7 lib/ambition/adapters/active_record/context.rb
benhoskings-ambitious-activerecord-0.1.3.8 lib/ambition/adapters/active_record/context.rb