forked from alloy/complex-form-examples
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated to work with NestedAttributes instead of NestedParams.
Made tests green with new ActiveSupport::TestCase.
- Loading branch information
Showing
11 changed files
with
211 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
class Project < ActiveRecord::Base | ||
# Automatically turns on autosave and thus also validates | ||
has_one :author, :nested_params => true | ||
has_many :tasks, :dependent => :destroy, :nested_params => true, :destroy_missing => true | ||
has_and_belongs_to_many :tags, :nested_params => true, :destroy_missing => true | ||
has_one :author, :accept_nested_attributes => true | ||
has_many :tasks, :dependent => :destroy, :accept_nested_attributes => true, :destroy_missing => true | ||
has_and_belongs_to_many :tags, :accept_nested_attributes => true, :destroy_missing => true | ||
|
||
validates_presence_of :name | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
vendor/plugins/on_test_spec/lib/test/spec/add_allow_switch.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require 'active_support' | ||
|
||
class Class | ||
def add_allow_switch(method, options={}) | ||
default = options[:default] || false | ||
|
||
class_eval do | ||
cattr_accessor "allow_#{method}" | ||
self.send("allow_#{method}=", default) | ||
|
||
alias_method "original_#{method}", method | ||
|
||
eval %{ | ||
def #{method}(*args) | ||
if allow_#{method} | ||
original_#{method}(*args) | ||
else | ||
raise RuntimeError, "You're trying to call `#{method}' on `#{self}', which you probably don't want in a test." | ||
end | ||
end | ||
}, binding, __FILE__, __LINE__ | ||
end | ||
end | ||
end | ||
|
||
class Module | ||
def add_allow_switch(method, options={}) | ||
default = options[:default] || false | ||
|
||
mattr_accessor "allow_#{method}" | ||
send("allow_#{method}=", default) | ||
|
||
unless respond_to?(:__metaclass___) | ||
def __metaclass__ | ||
class << self; self; end | ||
end | ||
end | ||
|
||
__metaclass__.class_eval do | ||
alias_method "original_#{method}", method | ||
|
||
eval %{ | ||
def #{method}(*args) | ||
if allow_#{method} | ||
original_#{method}(*args) | ||
else | ||
raise RuntimeError, "You're trying to call `#{method}' on `#{self}', which you probably don't want in a test." | ||
end | ||
end | ||
}, binding, __FILE__, __LINE__ | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
vendor/plugins/on_test_spec/test/add_allow_switch_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
require 'rubygems' rescue LoadError | ||
|
||
require 'test/spec' | ||
require 'mocha' | ||
|
||
require File.expand_path('../../lib/test/spec/add_allow_switch', __FILE__) | ||
|
||
module Factory | ||
def self.run | ||
true | ||
end | ||
end | ||
Factory.add_allow_switch :run, :default => true | ||
|
||
describe "Factory with an allow switch on run" do | ||
it "should alias the original method" do | ||
Factory.respond_to?(:original_run, include_private=true).should == true | ||
end | ||
|
||
it "should define a getter and setter" do | ||
Factory.should.respond_to(:allow_run) | ||
Factory.should.respond_to(:allow_run=) | ||
end | ||
|
||
it "should switch off" do | ||
Factory.allow_run = false | ||
lambda { | ||
Factory.run | ||
}.should.raise(RuntimeError) | ||
end | ||
|
||
it "should switch on" do | ||
Factory.allow_run = true | ||
lambda { | ||
Factory.run.should == true | ||
}.should.not.raise | ||
end | ||
end | ||
|
||
class Bunny | ||
def hop | ||
'Hop hop!' | ||
end | ||
end | ||
Bunny.add_allow_switch :hop | ||
|
||
describe "Bunny with an allow switch on hop" do | ||
before do | ||
@bunny = Bunny.new | ||
end | ||
|
||
it "should alias the original method" do | ||
@bunny.respond_to?(:original_hop).should == true | ||
end | ||
|
||
it "should define a getter and setter" do | ||
Bunny.should.respond_to(:allow_hop) | ||
Bunny.should.respond_to(:allow_hop=) | ||
end | ||
|
||
it "should switch off" do | ||
Bunny.allow_hop = false | ||
lambda { | ||
@bunny.hop | ||
}.should.raise(RuntimeError) | ||
end | ||
|
||
it "should switch on" do | ||
Bunny.allow_hop = true | ||
lambda { | ||
@bunny.hop.should == 'Hop hop!' | ||
}.should.not.raise | ||
end | ||
end | ||
|
||
|
||
Kernel.add_allow_switch :system | ||
|
||
describe "Kernel with an allow switch on system" do | ||
SILENT_COMMANT = 'ls > /dev/null' | ||
|
||
it "should alias the original method" do | ||
Kernel.respond_to?(:original_system, include_private=true).should == true | ||
end | ||
|
||
it "should define a getter and setter" do | ||
Factory.should.respond_to(:allow_system) | ||
Factory.should.respond_to(:allow_system=) | ||
end | ||
|
||
it "should switch off" do | ||
Kernel.allow_system = false | ||
lambda { | ||
Kernel.system(SILENT_COMMANT) | ||
}.should.raise(RuntimeError) | ||
end | ||
|
||
it "should switch on" do | ||
Kernel.allow_system = true | ||
lambda { | ||
Kernel.system(SILENT_COMMANT) | ||
}.should.not.raise | ||
end | ||
end |