Sha256: 627d5adc8f27cea0f073035f93b6e20593865e7d00b0e08972c4d1d73cb70bea

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

require 'spec_helper'

describe "ActiveRecord behaviors" do
  describe "a temporary ActiveRecord model created with with_model that has a named_scope" do
    before do
      class RegularModel < ActiveRecord::Base
        scope_method =
          if respond_to?(:scope) && !protected_methods.include?('scope')
            :scope # ActiveRecord 3.x
          else
            :named_scope # ActiveRecord 2.x
          end

        send scope_method, :title_is_foo, :conditions => {:title => 'foo'}
      end
      RegularModel.connection.drop_table(RegularModel.table_name) rescue nil
      RegularModel.connection.create_table(RegularModel.table_name) do |t|
        t.string 'title'
        t.text 'content'
        t.timestamps
      end
    end

    after do
      RegularModel.connection.drop_table(@model.table_name) rescue nil
    end

    with_model :blog_post do
      table do |t|
        t.string 'title'
        t.text 'content'
        t.timestamps
      end

      model do
        scope_method =
          if respond_to?(:scope) && !protected_methods.include?('scope')
            :scope # ActiveRecord 3.x
          else
            :named_scope # ActiveRecord 2.x
          end

        send scope_method, :title_is_foo, :conditions => {:title => 'foo'}
      end
    end

    describe "the named scope" do
      it "should work like a regular named scope" do
        included = RegularModel.create!(:title => 'foo', :content => "Include me!")
        excluded = RegularModel.create!(:title => 'bar', :content => "Include me!")

        RegularModel.title_is_foo.should == [included]

        included = BlogPost.create!(:title => 'foo', :content => "Include me!")
        excluded = BlogPost.create!(:title => 'bar', :content => "Include me!")

        BlogPost.title_is_foo.should == [included]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
with_model-0.1.4 spec/active_record_behaviors_spec.rb
with_model-0.1.2 spec/active_record_behaviors_spec.rb