diff --git a/README.markdown b/README.markdown index d33b141..d0cace2 100644 --- a/README.markdown +++ b/README.markdown @@ -91,7 +91,7 @@ Sham lets you generate random but repeatable unique attributes values. For example, you could define a way to generate random names as: - Sham.name { (1..10).map { ('a'..'z').to_a.rand } } + Sham.name { (1..10).map { ('a'..'z').to_a.rand }.join } Then, to generate a name, call: diff --git a/Rakefile b/Rakefile index bd038d0..f20737e 100644 --- a/Rakefile +++ b/Rakefile @@ -1,14 +1,37 @@ -require 'rubygems' require 'rake' -require 'rake/clean' -require 'spec/rake/spectask' -desc 'Default: run specs.' -task :default => :spec +begin + require 'jeweler' + Jeweler::Tasks.new do |gem| + gem.name = "machinist" + gem.summary = "Fixtures aren't fun. Machinist is." + gem.email = "pete@notahat.com" + gem.homepage = "http://github.com/notahat/machinist" + gem.authors = ["Pete Yandell"] + gem.has_rdoc = false + gem.add_development_dependency "rspec", ">= 1.2.8" + end + Jeweler::GemcutterTasks.new +rescue LoadError + puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler" +end -desc 'Run all the specs for the machinist plugin.' -Spec::Rake::SpecTask.new do |t| - t.spec_files = FileList['spec/**/*_spec.rb'] - t.rcov = false + +require 'spec/rake/spectask' +desc 'Run the specs.' +Spec::Rake::SpecTask.new(:spec) do |spec| + spec.libs << 'lib' << 'spec' + spec.spec_files = FileList['spec/**/*_spec.rb'] end +desc 'Run the specs with rcov.' +Spec::Rake::SpecTask.new(:rcov) do |spec| + spec.libs << 'lib' << 'spec' + spec.pattern = 'spec/**/*_spec.rb' + spec.rcov = true +end + +task :spec => :check_dependencies + +desc 'Run the specs.' +task :default => :spec diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..af0b7dd --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +1.0.6 diff --git a/lib/machinist/active_record.rb b/lib/machinist/active_record.rb index 60ea216..63cac6c 100644 --- a/lib/machinist/active_record.rb +++ b/lib/machinist/active_record.rb @@ -32,7 +32,7 @@ def self.assigned_attributes_without_associations(lathe) attributes = {} lathe.assigned_attributes.each_pair do |attribute, value| association = lathe.object.class.reflect_on_association(attribute) - if association && association.macro == :belongs_to + if association && association.macro == :belongs_to && !value.nil? attributes[association.primary_key_name.to_sym] = value.id else attributes[attribute] = value diff --git a/machinist.gemspec b/machinist.gemspec index c962cbd..e86e450 100644 --- a/machinist.gemspec +++ b/machinist.gemspec @@ -1,11 +1,72 @@ +# Generated by jeweler +# DO NOT EDIT THIS FILE DIRECTLY +# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command +# -*- encoding: utf-8 -*- + Gem::Specification.new do |s| - s.name = "machinist" - s.version = "1.0.5" - s.author = "Pete Yandell" - s.email = "pete@nothat.com" - s.homepage = "http://github.com/notahat/machinist" - s.summary = "Fixtures aren't fun. Machinist is." - s.files = ["lib/machinist.rb", "lib/sham.rb", "lib/machinist/blueprints.rb", "lib/machinist/object.rb", "lib/machinist/active_record.rb", "lib/machinist/data_mapper.rb", "lib/machinist/sequel.rb"] - s.require_path = "lib" - s.has_rdoc = false + s.name = %q{machinist} + s.version = "1.0.6" + + s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= + s.authors = ["Pete Yandell"] + s.date = %q{2009-11-29} + s.email = %q{pete@notahat.com} + s.extra_rdoc_files = [ + "README.markdown" + ] + s.files = [ + ".autotest", + ".gitignore", + "FAQ.markdown", + "MIT-LICENSE", + "README.markdown", + "Rakefile", + "VERSION", + "init.rb", + "lib/machinist.rb", + "lib/machinist/active_record.rb", + "lib/machinist/blueprints.rb", + "lib/machinist/data_mapper.rb", + "lib/machinist/object.rb", + "lib/machinist/sequel.rb", + "lib/sham.rb", + "machinist.gemspec", + "spec/active_record_spec.rb", + "spec/data_mapper_spec.rb", + "spec/db/.gitignore", + "spec/db/schema.rb", + "spec/log/.gitignore", + "spec/machinist_spec.rb", + "spec/sequel_spec.rb", + "spec/sham_spec.rb", + "spec/spec_helper.rb" + ] + s.homepage = %q{http://github.com/notahat/machinist} + s.rdoc_options = ["--charset=UTF-8"] + s.require_paths = ["lib"] + s.rubygems_version = %q{1.3.5} + s.summary = %q{Fixtures aren't fun. Machinist is.} + s.test_files = [ + "spec/active_record_spec.rb", + "spec/data_mapper_spec.rb", + "spec/db/schema.rb", + "spec/machinist_spec.rb", + "spec/sequel_spec.rb", + "spec/sham_spec.rb", + "spec/spec_helper.rb" + ] + + if s.respond_to? :specification_version then + current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION + s.specification_version = 3 + + if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then + s.add_development_dependency(%q, [">= 1.2.8"]) + else + s.add_dependency(%q, [">= 1.2.8"]) + end + else + s.add_dependency(%q, [">= 1.2.8"]) + end end + diff --git a/spec/active_record_spec.rb b/spec/active_record_spec.rb index 0852217..baf9323 100644 --- a/spec/active_record_spec.rb +++ b/spec/active_record_spec.rb @@ -1,5 +1,6 @@ require File.dirname(__FILE__) + '/spec_helper' require 'machinist/active_record' +require 'active_support/whiny_nil' module MachinistActiveRecordSpecs @@ -131,6 +132,16 @@ class Comment < ActiveRecord::Base comment[:post].should be_nil comment[:post_id].should_not be_nil end + + describe "on a belongs_to association" do + it "should allow explicitly setting the association to nil" do + Comment.blueprint { post } + Comment.blueprint(:no_post) { post { nil } } + lambda { + @comment = Comment.plan(:no_post) + }.should_not raise_error + end + end describe "on a has_many association" do before do diff --git a/spec/data_mapper_spec.rb b/spec/data_mapper_spec.rb index e162654..bd8cc8c 100644 --- a/spec/data_mapper_spec.rb +++ b/spec/data_mapper_spec.rb @@ -31,7 +31,7 @@ class Comment property :post_id, Integer property :author_id, Integer belongs_to :post - belongs_to :author, :class_name => "Person", :child_key => [:author_id] + belongs_to :author, :model => "Person", :child_key => [:author_id] end describe Machinist, "DataMapper adapter" do @@ -51,14 +51,14 @@ class Comment Admin.blueprint {} admin = Admin.make - admin.should_not be_new_record + admin.should_not be_new admin.type.should_not be_nil end it "should save the constructed object" do Person.blueprint { } person = Person.make - person.should_not be_new_record + person.should_not be_new end it "should create an object through a belongs_to association" do @@ -109,14 +109,14 @@ class Comment it "should not save the constructed object" do Person.blueprint { } person = Person.make_unsaved - person.should be_new_record + person.should be_new end it "should not save associated objects" do Post.blueprint { } Comment.blueprint { post } comment = Comment.make_unsaved - comment.post.should be_new_record + comment.post.should be_new end it "should save objects made within a passed-in block" do @@ -124,8 +124,8 @@ class Comment Comment.blueprint { } comment = nil post = Post.make_unsaved { comment = Comment.make } - post.should be_new_record - comment.should_not be_new_record + post.should be_new + comment.should_not be_new end end