Sha256: 631a6bbcca650c1c7c5a673faaf6b76767e269c3a60dbd18b8e0b9671f02b287

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

require 'spec_helper'

describe EdgeRider::Scoped do
  describe 'scoped' do
    
    it 'returns a scope when called without parameters' do
      unless EdgeRider::Util.activerecord2? # Rails 2 cannot do this
        EdgeRider::Util.scope?( Forum.scoped ).should == true
      end
    end
    
    it 'returns a scope when called with an empty hash' do
      EdgeRider::Util.scope?( Forum.scoped({}) ).should == true
    end
    
    it 'respects conditions' do
      trashed = Forum.create! :trashed => true
      active = Forum.create! :trashed => false
      
      Forum.scoped(:conditions => { :trashed => true }).to_a.should == [ trashed ]
    end
    
    it 'respects order' do
      c = Forum.create! :name => 'Chemikalien'
      a = Forum.create! :name => 'Allegorie'
      b = Forum.create! :name => 'Botanisch'
      
      Forum.scoped(:order => 'name ASC').to_a.should == [ a, b, c ]
    end
    
    it 'can be used to add conditions to an existing scope chain' do
      bulldog = Forum.create! :name => 'bulldog', :trashed => false
      trashed = Forum.create! :name => 'maneuver', :trashed => true
      trashed_bulldog = Forum.create! :name => 'bulldog', :trashed => true
      
      Forum.
        scoped(:conditions => { :trashed => true }).
        scoped(:conditions => { :name => 'bulldog' }).to_a.should == [ trashed_bulldog ]
    end
    
    it 'can be used to add conditions to a has_many association' do
      forum = Forum.create!
      thema = Topic.create! :subject => 'Thema', :trashed => false, :forum => forum
      trashed_thema = Topic.create! :subject => 'Thema', :trashed => true, :forum => forum
      
      has_many_scope = forum.active_topics
      # In Rails 3, #scoped on associations does not take parameters but turns
      # an association into a real scope.
      has_many_scope = has_many_scope.scoped if ActiveRecord::VERSION::MAJOR == 3
      has_many_scope.scoped(:conditions => { :subject => 'Thema' }).should =~ [ thema ]
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
edge_rider-0.3.1 spec/shared/spec/edge_rider/scoped_spec.rb