Sha256: dc9ad772f209fbabd60c04109f2571d9b892710e2b104cd9633475b178a82588

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module Ambition
  class Context

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

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

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

    def within outer_scope, join_association
      context = ambition_context

      unless outer_scope.nil?
        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

1 entries across 1 versions & 1 rubygems

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