Sha256: ab13e1994af8904ff9a46e516a9c40e0c19a28c5abe0c07fdd930ec0714a7d3b

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

require File.dirname(__FILE__) + '/../spec_helper'

describe Netzke::ActiveRecord::RelationExtensions do
  it "should accept different options in extend_with" do
    # Preparations
    10.times do |i|
      Factory(:user, :first_name => "First Name #{i}", :role_id => i)
    end
    
    User.scope(:role_id_gt_7, User.where(["role_id > ?", 7]))
    User.scope(:role_id_gt, lambda{ |param| User.where(["role_id > ?", param]) })
    
    # Tests
    User.where({}).extend_with(["role_id >= ?", 5]).count.should == 5
    
    User.where({}).extend_with(:role_id_gt_7).count.should == 2
    
    User.where({}).extend_with(:role_id_gt, 2).count.should == 7
    
    User.where({}).extend_with([:role_id_gt, 3]).count.should == 6
    
    User.where({}).extend_with(:role_id => 5).first.first_name.should == "First Name 5"
    
    User.where(["role_id < ?", 7]).extend_with(lambda{ |relation| relation.where(["role_id > ?", 4]) }).count.should == 2
    
    # User.where({}).extend_with("select * from users where role_id > 6").all.size.should == 3
    
  end
  
  it "should be extendable with extend_with_netzke_conditions" do
    # Preparations
    roles = [Factory(:role, :name => "admin"), Factory(:role, :name => "user"), Factory(:role, :name => "superadmin")]
    
    # 3 users of each role
    9.times do |i|
      Factory(:user, :role_id => roles[i%3].id)
    end

    # Tests
    User.where({}).extend_with_netzke_conditions(:role_id__eq => roles.last.id).count.should == 3
    User.where({}).extend_with_netzke_conditions(:role__name__eq => "admin").count.should == 3
    User.where({}).extend_with_netzke_conditions(:role__name__like => "%admin%").count.should == 6
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
netzke-basepack-0.6.0 spec/active_record/relation_extensions_spec.rb