Permalink
Browse files

adding category model for testing associations

  • Loading branch information...
1 parent dfb7774 commit 7205357d5f92fb3b94200f0733012524a1013aa3 @ryanb committed Aug 26, 2008
Showing with 33 additions and 2 deletions.
  1. +15 −0 spec/models/category.rb
  2. +2 −0 spec/models/product.rb
  3. +12 −0 spec/scope_builder/builder_spec.rb
  4. +4 −2 spec/spec_helper.rb
View
@@ -0,0 +1,15 @@
+class Category < ActiveRecord::Base
+ has_many :products
+end
+
+class CreateCategories < ActiveRecord::Migration
+ def self.up
+ create_table :categories do |t|
+ t.string :name
+ end
+ end
+
+ def self.down
+ drop_table :categories
+ end
+end
View
@@ -1,12 +1,14 @@
class Product < ActiveRecord::Base
named_scope :released, :conditions => ['released=?', true]
+ belongs_to :category
end
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.string :name
t.boolean :released
+ t.integer :category_id
end
end
@@ -52,4 +52,16 @@
builder = Product.released.scope_builder
builder.all.should == Product.released.all
end
+
+ describe "products in category" do
+ before(:each) do
+ Category.delete_all
+ @category = Category.create!(:products => Product.find_all_by_name(['a', 'b']))
+ end
+
+ it "should work on named scope through association" do
+ builder = @category.products.released.scope_builder
+ builder.all.should == @category.products.released.all
+ end
+ end
end
View
@@ -6,13 +6,15 @@
# setup database adapter
ActiveRecord::Base.establish_connection({
- :adapter => "sqlite3",
- :dbfile => File.dirname(__FILE__) + "/test.sqlite3"
+ :adapter => "sqlite3",
+ :dbfile => File.dirname(__FILE__) + "/test.sqlite3"
})
# load models
# there's probably a better way to handle this
+require File.dirname(__FILE__) + '/models/category.rb'
require File.dirname(__FILE__) + '/models/product.rb'
+CreateCategories.migrate(:up) unless Category.table_exists?
CreateProducts.migrate(:up) unless Product.table_exists?
Spec::Runner.configure do |config|

0 comments on commit 7205357

Please sign in to comment.