Sha256: d9b3f4a26ce3ec09e740ae7ac8890742711b865bb8784a99e1ff8c611c034aad

Contents?: true

Size: 1.42 KB

Versions: 8

Compression:

Stored size: 1.42 KB

Contents

require 'spec_helper'

describe Protoable::Scope do
  describe ".search_scope" do
    let(:request) { UserSearchMessage.new(:guid => ["foo"], :email => ["foo@test.co"]) }

    it "builds scopes for searchable fields" do
      User.stub(:searchable_fields).and_return({ :email =>  :by_email })
      User.search_scope(request).should eq User.by_email("foo@test.co")
    end

    it "is chainable" do
      User.limit(1).search_scope(request).order(:email).should eq User.limit(1).order(:email)
    end

    context "when a searchable field does not have a value" do
      let(:request) { UserSearchMessage.new(:email => ["foo@test.co"]) }

      it "doesn't build a scope from that field" do
        User.stub(:searchable_fields).and_return({ :email =>  :by_email })
        User.search_scope(request).should eq User.by_email("foo@test.co")
      end
    end
  end

  describe ".field_scope" do
    before { @fields = User.instance_variable_get("@_searchable_fields") }
    after { User.instance_variable_set("@_searchable_fields", @fields) }

    context "when given a search scope that is not defined" do
      it "raises an exception" do
        expect { User.field_scope :name, :foo }.to raise_exception(/Undefined scope :foo/)
      end
    end

    it "stores the search scope in the searchable fields hash using the field as the key" do
      User.field_scope :guid, :by_guid
      User.searchable_fields[:guid].should eq :by_guid
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
protobuf-activerecord-1.2.4 spec/protoable/scope_spec.rb
protobuf-activerecord-1.2.3 spec/protoable/scope_spec.rb
protobuf-activerecord-1.2.2 spec/protoable/scope_spec.rb
protobuf-activerecord-1.2.1 spec/protoable/scope_spec.rb
protobuf-activerecord-1.2.0 spec/protoable/scope_spec.rb
protobuf-activerecord-1.2.0.rc1 spec/protoable/scope_spec.rb
protobuf-activerecord-1.1.1 spec/protoable/scope_spec.rb
protobuf-activerecord-1.1.0 spec/protoable/scope_spec.rb