Skip to content
Browse files

fix many issues with .create_without_callbacks and rename it to .stor…

…e_with_values
  • Loading branch information...
1 parent bca048a commit ff2b6c82b3ded7aecf7ef3c86778a3f0a0cc2b92 @henning-koch henning-koch committed Feb 22, 2013
View
5 README.md
@@ -154,7 +154,7 @@ If you only care about the matchers or helpers you can also more specifically re
### Extensions to **ActiveRecord::Base**
-**create_without_callbacks**
+**store_with_values**
Only on Rails.
@@ -164,7 +164,8 @@ If you only care about the matchers or helpers you can also more specifically re
##Changes from previous versions:
-- `new_and_store` has been renamed `create_without_callbacks`
+- `new_and_store` has been renamed to `store_with_values`
+- `create_without_callbacks` has been renamed to `store_with_values`
- `keep_invalid!` has been renamed to `prevent_storage`
- `Object#should_not_receive_and_execute` has been removed (same as `Object#should_not_receive`)
- `should_receive_all_with` (over-generic method name for a helper that is rarely useful)
View
2 lib/rspec_candy/helpers.rb
@@ -9,7 +9,7 @@
require 'rspec_candy/helpers/stub_existing'
if RSpecCandy::Switcher.rails_loaded?
- require 'rspec_candy/helpers/rails/create_without_callbacks'
+ require 'rspec_candy/helpers/rails/store_with_values'
require 'rspec_candy/helpers/rails/it_should_run_callbacks'
require 'rspec_candy/helpers/rails/prevent_storage'
end
View
29 lib/rspec_candy/helpers/rails/create_without_callbacks.rb
@@ -1,29 +0,0 @@
-module RSpecCandy
- module Helpers
- module Rails
- module CreateWithoutCallbacks
-
- def create_without_callbacks(*args)
- table_name = self.table_name
- plain_model = Class.new(ActiveRecord::Base) do
- self.table_name = table_name
- end
- plain_record = plain_model.new(*args)
- if plain_record.respond_to?(:type=)
- plain_record.type = name
- end
- plain_record.save!
- find plain_record.id
- end
-
- def new_and_store(*args)
- warn 'new_and_store is deprecated. Use create_without_callbacks instead.'
- create_without_callbacks(*args)
- end
-
- ActiveRecord::Base.send(:extend, self)
-
- end
- end
- end
-end
View
34 lib/rspec_candy/helpers/rails/store_with_values.rb
@@ -0,0 +1,34 @@
+module RSpecCandy
+ module Helpers
+ module Rails
+ module StoreWithValues
+
+ def store_with_values(values = {})
+ record = new
+ if Switcher.rails_version == :rails2
+ record.send(:attributes=, values, false)
+ record.send(:create_without_callbacks)
+ else
+ require 'sneaky-save'
+ record.assign_attributes(values, :without_protection => true)
+ record.sneaky_save
+ end
+ record
+ end
+
+ def new_and_store(*args)
+ warn 'new_and_store is deprecated. Use store_with_values instead.'
+ store_with_values(*args)
+ end
+
+ def create_without_callbacks(*args)
+ warn 'create_without_callbacks is deprecated because the name suggested that it honors mass-assignment protection. Use store_with_values instead.'
+ store_with_values(*args)
+ end
+
+ ActiveRecord::Base.send(:extend, self)
+
+ end
+ end
+ end
+end
View
2 lib/rspec_candy/version.rb
@@ -1,3 +1,3 @@
module RSpecCandy
- VERSION = '0.2.7'
+ VERSION = '0.2.8'
end
View
1 rspec_candy.gemspec
@@ -16,4 +16,5 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.add_dependency('rspec')
+ s.add_dependency('sneaky-save')
end
View
3 spec/rspec1/Gemfile
@@ -2,15 +2,14 @@ source :rubygems
gem 'sqlite3'
gem 'rails', '~>2'
+gem 'has_defaults'
gem 'rspec', '~>1'
gem 'rspec-rails', '~>1'
gem 'ruby-debug', :platforms => :ruby_18
gem 'debugger', :platforms => :ruby_19
gem 'rspec_candy', :path => '../..'
gem 'test-unit', '~>1.2', :platforms => :ruby_19
-
group :state_machine do
gem 'state_machine'
end
-
View
3 spec/rspec2/Gemfile
@@ -2,6 +2,7 @@ source :rubygems
gem 'sqlite3'
gem 'rails', '~>3'
+gem 'has_defaults'
gem 'rspec', '~>2'
gem 'rspec-rails', '~>2'
gem 'ruby-debug', :platforms => :ruby_18
@@ -10,4 +11,4 @@ gem 'rspec_candy', :path => '../..'
group :state_machine do
gem 'state_machine'
-end
+end
View
2 spec/shared/app_root/app/models/model.rb
@@ -4,6 +4,8 @@ class Model < ActiveRecord::Base
after_save :after_save_callback1, :after_save_callback2
before_save :before_save_callback1, :before_save_callback2
+ belongs_to :associated_model, :class_name => 'Model'
+
if respond_to? :around_save
around_save :around_save_callback1, :around_save_callback2
end
View
1 spec/shared/app_root/db/migrate/001_create_model.rb
@@ -3,6 +3,7 @@ class CreateModel < ActiveRecord::Migration
def self.up
create_table :models do |t|
t.string :string_field
+ t.references :associated_model
end
end
View
51 spec/shared/rspec_candy/helpers/rails/create_without_callbacks_spec.rb
@@ -1,51 +0,0 @@
-require 'spec_helper'
-
-describe RSpecCandy::Helpers::Rails::CreateWithoutCallbacks do
-
- describe ActiveRecord::Base do
-
- describe '.create_without_callbacks' do
-
- it 'should create a record without running callbacks or validations' do
- model = Model.disposable_copy do
- before_save :crash
- validate :crash
- def crash
- raise 'callback was run'
- end
- end
- record = nil
- expect do
- record = model.create_without_callbacks(:string_field => 'foo')
- end.to_not raise_error
- record.string_field.should == 'foo'
- record.should be_a(Model)
- record.id.should be_a(Fixnum)
- record.should_not be_new_record
- end
-
- it 'should work with single table inheritance' do
- child = StiChild.create_without_callbacks(:string_field => 'foo')
- child.string_field.should == 'foo'
- child.should be_a(StiChild)
- child.id.should be_a(Fixnum)
- child.type.should == 'StiChild'
- child.should_not be_new_record
- StiChild.all.should == [child]
- end
-
- end
-
- describe '.new_and_store' do
-
- it 'should print a deprecation warning urging to use .create_without_callbacks instead' do
- Model.should_receive(:warn).with(/Use create_without_callbacks instead/i)
- Model.should_receive(:create_without_callbacks).with('args')
- Model.new_and_store('args')
- end
-
- end
-
- end
-
-end
View
84 spec/shared/rspec_candy/helpers/rails/store_with_values_spec.rb
@@ -0,0 +1,84 @@
+require 'spec_helper'
+require 'has_defaults'
+
+describe RSpecCandy::Helpers::Rails::StoreWithValues do
+
+ describe ActiveRecord::Base do
+
+ describe '.store_with_values' do
+
+ it 'should run after_initialize callbacks' do
+ model = Model.disposable_copy do
+ has_defaults :string_field => 'Hello Universe'
+ end
+
+ record = model.store_with_values
+ record.string_field.should == 'Hello Universe'
+ end
+
+ it 'should allow setting associations' do
+ associated_model = Model.create!
+
+ record = Model.store_with_values(:associated_model => associated_model)
+ record.reload
+ record.associated_model.should == associated_model
+ end
+
+ it 'should allow setting primary keys' do
+ record = Model.store_with_values(:id => 1337)
+ record.id.should == 1337
+ end
+
+ it 'should create a record without running callbacks or validations' do
+ model = Model.disposable_copy do
+ before_save :crash
+ validate :crash
+ def crash
+ raise 'callback was run'
+ end
+ end
+ record = nil
+ expect do
+ record = model.store_with_values(:string_field => 'foo')
+ end.to_not raise_error
+ record.string_field.should == 'foo'
+ record.should be_a(Model)
+ record.id.should be_a(Fixnum)
+ record.should_not be_new_record
+ end
+
+ it 'should work with single table inheritance' do
+ child = StiChild.store_with_values(:string_field => 'foo')
+ child.string_field.should == 'foo'
+ child.should be_a(StiChild)
+ child.id.should be_a(Fixnum)
+ child.type.should == 'StiChild'
+ child.should_not be_new_record
+ StiChild.all.should == [child]
+ end
+
+ end
+
+ describe '.create_without_callbacks' do
+
+ it 'should print a deprecation warning urging to use .store_with_values instead' do
+ Model.should_receive(:warn).with(/Use store_with_values instead/i)
+ Model.should_receive(:store_with_values).with('args')
+ Model.create_without_callbacks('args')
+ end
+
+ end
+
+ describe '.new_and_store' do
+
+ it 'should print a deprecation warning urging to use .store_with_values instead' do
+ Model.should_receive(:warn).with(/Use store_with_values instead/i)
+ Model.should_receive(:store_with_values).with('args')
+ Model.new_and_store('args')
+ end
+
+ end
+
+ end
+
+end

0 comments on commit ff2b6c8

Please sign in to comment.
Something went wrong with that request. Please try again.