Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Removed has_autosave_and_nested_params plugin and vendored rails.

  • Loading branch information...
commit 1ff8042b022ccddf49344186a7ae08792568907c 1 parent c8b8e99
@alloy authored
View
3  .gitmodules
@@ -1,3 +0,0 @@
-[submodule "vendor/rails"]
- path = vendor/rails
- url = git://github.com/ddollar/rails.git
View
20 vendor/plugins/has_autosave_and_nested_params/MIT-LICENSE
@@ -1,20 +0,0 @@
-Copyright (c) 2008 Fingertips, Eloy Duran <eloy@fngtps.com>
-
-Permission is hereby granted, free of charge, to any person obtaining
-a copy of this software and associated documentation files (the
-"Software"), to deal in the Software without restriction, including
-without limitation the rights to use, copy, modify, merge, publish,
-distribute, sublicense, and/or sell copies of the Software, and to
-permit persons to whom the Software is furnished to do so, subject to
-the following conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
35 vendor/plugins/has_autosave_and_nested_params/README
@@ -1,35 +0,0 @@
-== AutosaveAssociation
-Adds :autosave to the options for has_one and has_many associations,
-which will automatically save the associated models after
-the parent has been saved.
-
-== NestedParams
-Adds :nested_params to the options for has_one and has_many associations,
-which allows attributes of the associated model to be set directly
-with a hash to ActiveRecord::Base#attributes=.
-
-== NestedParamsFormBuilder
-Use this form builder to get a NestedParams aware fields_for method.
-This will handle has_many associated models, that have NestedParams enabled.
-
-=== License
-Copyright (c) 2008 Fingertips, Eloy Duran <eloy@fngtps.com>
-
-Permission is hereby granted, free of charge, to any person obtaining
-a copy of this software and associated documentation files (the
-"Software"), to deal in the Software without restriction, including
-without limitation the rights to use, copy, modify, merge, publish,
-distribute, sublicense, and/or sell copies of the Software, and to
-permit persons to whom the Software is furnished to do so, subject to
-the following conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
22 vendor/plugins/has_autosave_and_nested_params/Rakefile
@@ -1,22 +0,0 @@
-require 'rake'
-require 'rake/testtask'
-require 'rake/rdoctask'
-
-desc 'Default: run unit tests.'
-task :default => :test
-
-desc 'Test the has_autosave_and_nested_params plugin.'
-Rake::TestTask.new(:test) do |t|
- t.libs << 'lib'
- t.pattern = 'test/**/*_test.rb'
- t.verbose = true
-end
-
-desc 'Generate documentation for the has_autosave_and_nested_params plugin.'
-Rake::RDocTask.new(:rdoc) do |rdoc|
- rdoc.rdoc_dir = 'rdoc'
- rdoc.title = 'HasAutosaveAndNestedParams'
- rdoc.options << '--line-numbers' << '--inline-source'
- rdoc.rdoc_files.include('README')
- rdoc.rdoc_files.include('lib/**/*.rb')
-end
View
12 vendor/plugins/has_autosave_and_nested_params/TODO
@@ -1,12 +0,0 @@
-AutosaveAssociation:
-- Wrap everything in a transaction:
- - Right now all saving is done within a transaction, also for non NestedParams enabled associations. This is bad.
- Need to think of a way to only wrap these in transactions.
- - Also atm the option to save without validations is no longer working, because we always use save!.
- Maybe set an instance variable indicating what kind of save we are doing, seems ugly maybe there's
- already such a kept around. Should only wrap association saving in a transaction if we are doing a normal save,
- if it's a save without validations it should also be saved without validations.
- Actually we can probably safely always save the association without validations because if the parent needs to be
- validated it will already validate the association and thus will only be saved if validations are turned off.
-NestedParams:
-- We don't need to support arrays of ids anymore, because when using the NestedParamsFormBuilder we always get a hash.
View
117 vendor/plugins/has_autosave_and_nested_params/lib/autosave_association.rb
@@ -1,117 +0,0 @@
-# Adds <tt>:autosave</tt> to the options for has_one and has_many associations.
-# Which will automatically save the associated models after
-# the parent has been saved, but only if all validations pass on both
-# the parent and the assocatiated models.
-#
-# All saving will be done within a transaction, so an exception raised in one
-# of the associated models should not leave the db in an inconsistent state.
-#
-# Note that using ActiveRecord#save(false) on the parent to bypass validations, will also
-# bypass any validations on the associated models.
-#
-# Example of a has_one association:
-#
-# class Member < ActiveRecord::Base
-# extend AutosaveAssociation
-# has_one :avatar, :autosave => true
-# end
-#
-# member.avatar.name # => 'smiley'
-# member.avatar.name = 'sadly'
-#
-# member.save
-# member.reload
-#
-# member.avatar.name # => 'sadly'
-#
-# Example of a has_many association:
-#
-# class Member < ActiveRecord::Base
-# extend AutosaveAssociation
-# has_many :avatars, :autosave => true
-# end
-#
-# member.avatars.map(&:name) # => ['smiley', 'frowny']
-#
-# member.avatars.first.name = 'sadley'
-# member.avatars.last.name = 'browny'
-#
-# member.save
-# member.reload
-#
-# member.avatars.map(&:name) # => ['sadley', 'browny']
-module AutosaveAssociation
- def has_many_with_autosave(*args)
- autosave = args.last.delete(:autosave) if args.last.is_a?(Hash)
- has_many_without_autosave(*args)
- define_autosave_and_validation_for_has_many_association(args.first) if autosave
- end
-
- def has_one_with_autosave(*args)
- autosave = args.last.delete(:autosave) if args.last.is_a?(Hash)
- has_one_without_autosave(*args)
- define_autosave_and_validation_for_has_one_association(args.first) if autosave
- end
-
- def define_autosave_and_validation_for_has_many_association(attr)
- class_eval do
- define_method("autosave_#{attr}") do
- send(attr).each { |x| x.save(false) }
- end
- after_save "autosave_#{attr}"
- end
-
- define_method("validate_#{attr}") do
- send(attr).each do |associated_model|
- unless associated_model.valid?
- associated_model.errors.each do |attribute, message|
- name = "#{attr}_#{attribute}"
- errors.add(name, message) if errors.on(name).blank?
- end
- end
- end
- end
- validate "validate_#{attr}"
- end
-
- def define_autosave_and_validation_for_has_one_association(attr)
- class_eval do
- define_method("autosave_#{attr}") do
- if associated_model = send(attr)
- associated_model.save(false)
- end
- end
- after_save "autosave_#{attr}"
-
- define_method("validate_#{attr}") do
- if (associated_model = send(attr)) and not associated_model.valid?
- associated_model.errors.each { |attribute, message| errors.add attribute, message }
- end
- end
- validate "validate_#{attr}"
- end
- end
-
- module InstanceMethods
- def save_with_autosave(run_validations = true)
- transaction { run_validations ? save! : save_without_autosave(false) }
- true
- rescue Exception => e
- # TODO: We rescue everything.. Is that ok? Or should we only rescue certain exceptions?
- false
- end
-
- def self.included(klass)
- klass.class_eval { alias_method_chain :save, :autosave }
- end
- end
-
- def self.extended(klass)
- return if klass.respond_to? :has_one_without_autosave
- class << klass
- alias_method_chain :has_many, :autosave
- alias_method_chain :has_one, :autosave
- end
- klass.send(:include, InstanceMethods)
- end
-end
View
151 vendor/plugins/has_autosave_and_nested_params/lib/nested_params.rb
@@ -1,151 +0,0 @@
-require File.expand_path('../autosave_association', __FILE__)
-
-# Adds <tt>:nested_params</tt>, <tt>:reject_empty</tt> and <tt>:destroy_missing</tt> to the options for <tt>has_one</tt> and <tt>has_many</tt> associations.
-# Which allows attributes of the associated model to be set directly
-# with a hash to ActiveRecord::Base#attributes=.
-# This is handy for when you have a form consisting of a parent and one or more child records.
-#
-# All the associations that you enable <tt>:nested_params</tt> on, will automatically have <tt>:autosave</tt> turned on as well.
-# See AutosaveAssociation for more info.
-#
-# Examples for a has_one association:
-#
-# class Member < ActiveRecord::Base
-# extend NestedParams
-# has_one :avatar, :nested_params => true
-# end
-#
-# # Adding an associated model:
-# params[:member] # => { 'name' => 'jack', 'avatar' => { 'name' => 'smiley' }}
-#
-# member = Member.create(params[:member])
-# member.avatar.name # => 'smiley'
-#
-# # Updating the associated model:
-# params # => { 'id' => '1', 'member' => { 'name' => 'joe', 'avatar' => { 'name' => 'sadly' }}}
-#
-# member = Member.find(params[:id])
-# member.update_attributes params[:member]
-# member.avatar.name # => 'sadly'
-#
-# Examples for a has_many association:
-#
-# class Member < ActiveRecord::Base
-# extend NestedParams
-# has_many :posts, :nested_params => true, :destroy_missing => true, :reject_empty => true
-# end
-#
-# # Creating associated models. Use the :reject_empty option to not create associated models for any empty hashes (Turned off by default):
-# params[:member] # => { 'name' => 'joe', 'posts' => {
-# 'new_12345' => { 'title' => 'first psot' },
-# 'new_54321' => { 'title' => 'other post' },
-# 'new_67890' => { 'title' => '' } # This one is empty and will be rejected.
-# }}
-#
-# member = Member.create(params[:member])
-# member.posts.length # => 2
-# member.posts.first.title # => 'first psot'
-# member.posts.last.title # => 'other post'
-#
-# # Updating the associated models:
-# params[:member] # => { 'name' => 'joe', 'posts' => {
-# '1' => { 'title' => '[UPDATED] first psot' },
-# '2' => { 'title' => '[UPDATED] other post' }
-# }}
-#
-# member.update_attributes params[:member]
-# member.posts.first.title # => '[UPDATED] first psot'
-# member.posts.last.title # => '[UPDATED] other post'
-#
-# # Destroy an associated model by leaving it out of the attributes hash. Turn it on with the :destroy_missing option (Turned off by default):
-# params[:member] # => { 'name' => 'joe', 'posts' => {
-# '2' => { 'title' => '[UPDATED] other post is now the only post' }
-# }}
-#
-# member.update_attributes params[:member]
-# member.posts.length # => 1
-# member.posts.first.title # => '[UPDATED] other post is now the only post'
-module NestedParams
- def has_many_with_nested_params(*args)
- if (options = args.last).is_a?(Hash)
- destroy_missing = options.delete(:destroy_missing)
- nested_params = options.delete(:nested_params)
- reject_empty = options.delete(:reject_empty)
- end
-
- has_many_without_nested_params(*args)
-
- if nested_params
- attr = args.first
- define_nested_params_for_has_many_association(attr, destroy_missing, reject_empty)
- define_autosave_and_validation_for_has_many_association(attr)
- end
- end
-
- def has_one_with_nested_params(*args)
- nested_params = args.last.delete(:nested_params) if args.last.is_a?(Hash)
- has_one_without_nested_params(*args)
- if nested_params
- attr = args.first
- define_nested_params_for_has_one_association(attr)
- define_autosave_and_validation_for_has_one_association(attr)
- end
- end
-
- def define_nested_params_for_has_many_association(attr, destroy_missing, reject_empty)
- class_eval do
- define_method("#{attr}_attributes=") do |value|
- if value.is_a?(Hash) || value.is_a?(ActiveSupport::OrderedHash)
- if destroy_missing
- # Get all ids and subtract the ones we received, destroy the remainder
- keys = value.keys.map { |key| key.to_s }
- send(attr).reject { |x| keys.include? x.id.to_s }.each { |record| record.destroy }
- end
-
- new_records = []
- value.each do |id, attributes|
- if id.is_a?(String) && id.starts_with?('new_')
- # Collect new records marked by an id that starts with 'new_
- new_records << [id, attributes] unless reject_empty && attributes.values.all? { |v| v.blank? }
- else
- # Find the existing record for this id and assign the attributes
- send(attr).detect { |x| x.id == id.to_i }.attributes = attributes
- end
- end
- # Sort and build new records
- new_records.sort_by { |id, _| id }.each { |_, attributes| send(attr).build attributes }
- else
- if value.is_a?(Array) && value.all? { |x| x.is_a?(Hash) }
- # For an array full of new record hashes
- value.each do |attributes|
- send(attr).build attributes
- end
- else
- # TODO: Raise ArgumentError
- end
- end
- end
- end
- end
-
- def define_nested_params_for_has_one_association(attr)
- class_eval do
- define_method("#{attr}_attributes=") do |value|
- if value.is_a? Hash
- send("build_#{attr}") if send(attr).nil?
- send(attr).attributes = value
- else
- # TODO: Raise ArgumentError
- end
- end
- end
- end
-
- def self.extended(klass)
- klass.extend AutosaveAssociation
- class << klass
- alias_method_chain :has_many, :nested_params
- alias_method_chain :has_one, :nested_params
- end
- end
-end
View
31 vendor/plugins/has_autosave_and_nested_params/lib/nested_params_form_builder.rb
@@ -1,31 +0,0 @@
-class NestedParamsFormBuilder < ActionView::Helpers::FormBuilder
- def fields_for(record_or_name_or_array, *args, &block)
- if record_or_name_or_array.is_a?(String) || record_or_name_or_array.is_a?(Symbol)
- if reflection = @object.class.reflect_on_association(record_or_name_or_array.to_sym)
- name = "#{object_name}[#{record_or_name_or_array}_attributes]"
-
- case reflection.macro
- when :has_one then return @template.fields_for(name, @object.send(record_or_name_or_array), *args, &block)
- when :has_many
- records = args.first.is_a?(ActiveRecord::Base) ? [args.first] : @object.send(record_or_name_or_array)
- return records.map do |record|
- # In the case of a new object use a fictive id which is composited with "new_" and the @child_counter.
- record_name = "#{name}[#{ record.new_record? ? "new_#{child_counter}" : record.id }]"
- @template.fields_for(record_name, record, *args) do |form_builder|
- block.arity == 2 ? block.call(form_builder, record) : block.call(form_builder)
- end
- end.join
- end
- end
- end
- super
- end
-
- private
-
- def child_counter
- value = (@child_counter ||= 1)
- @child_counter += 1
- value
- end
-end
View
143 vendor/plugins/has_autosave_and_nested_params/test/autosave_association_test.rb
@@ -1,143 +0,0 @@
-require File.expand_path('../test_helper', __FILE__)
-
-describe "AutosaveAssociation, on a has_one association" do
- before do
- setup_db
-
- @member = Member.create(:email => 'paco@example.com')
- @avatar = @member.create_avatar(:name => 'smiley')
- end
-
- after do
- teardown_db
- end
-
- it "should still work as normal" do
- @member.email = 'pablo@example.com'
- @member.save!; @member.reload
- @member.email.should == 'pablo@example.com'
- end
-
- it "should also still work without an associated model" do
- member = Member.create(:email => '')
- member.email = 'pablo@example.com'
- lambda { member.save! }.should.not.raise Exception
- end
-
- it "should automatically save the associated model" do
- @member.avatar.name = 'sadly'
- @member.save!; @member.reload
- @member.avatar.name.should == 'sadly'
- end
-
- it "should automatically validate the associated model" do
- @member.avatar.name = ''
- @member.should.not.be.valid
- @member.errors.on(:name).should.not.be.blank
- end
-
- it "should rollback any changes if an exception occurred while saving" do
- @member.avatar.stubs(:save).raises(RuntimeError, 'Error!')
-
- @member.avatar.visitor_id = 123
- @member.address = 'Another address 1'
-
- lambda {
- @member.save.should.be false
- }.should.not.raise(Exception)
-
- @member.reload
- @member.address.should.be.blank
- @member.avatar.visitor_id.should.be.blank
- end
-
- it "should still allow to bypass validations on the associated model" do
- @member.email = ''
- @member.avatar.name = ''
-
- @member.save(false).should.be true
- @member.reload
-
- @member.email.should.be.blank
- @member.avatar.name.should.be.blank
- end
-
- it "should still raise an ActiveRecord::RecordInvalid exception if we want that" do
- @member.avatar.name = ''
-
- lambda {
- @member.save!
- }.should.raise(ActiveRecord::RecordInvalid)
- end
-
-end
-
-describe "AutosaveAssociation, on a has_many association" do
- before do
- setup_db
-
- @visitor = Visitor.create(:email => 'paco@example.com')
- @avatar1 = @visitor.avatars.create(:name => 'smiley1')
- @avatar2 = @visitor.avatars.create(:name => 'smiley2')
- end
-
- after do
- teardown_db
- end
-
- it "should still work as normal" do
- @visitor.attributes = { :email => 'pablo@example.com' }
- @visitor.email.should == 'pablo@example.com'
- end
-
- it "should automatically save the associated models" do
- @avatar1.name = 'sadly1'
- @avatar2.name = 'sadly2'
- @visitor.save!; @visitor.reload
-
- @visitor.avatars.map(&:name).sort.should == %w{ sadly1 sadly2 }
- end
-
- it "should automatically validate the associated models" do
- @visitor.avatars.first.name = ''
- @visitor.avatars.last.name = ''
-
- @visitor.should.not.be.valid
- @visitor.errors.on(:avatars_name).should == "can't be blank"
- @visitor.errors.on(:avatars).should.be.blank
- end
-
- it "should rollback any changes if an exception occurred while saving" do
- @visitor.avatars.first.stubs(:save).raises(RuntimeError, 'Error!')
-
- @visitor.avatars.last.member_id = 123
- @visitor.address = 'Another address 1'
-
- lambda {
- @visitor.save.should.be false
- }.should.not.raise(Exception)
-
- @visitor.reload
- @visitor.address.should.be.blank
- @visitor.avatars.last.member_id.should.be.blank
- end
-
- it "should still allow to bypass validations on the associated models" do
- @visitor.email = ''
- @visitor.avatars.first.name = ''
-
- @visitor.save(false).should.be true
- @visitor.reload
-
- @visitor.email.should.be.blank
- @visitor.avatars.first.name.should.be.blank
- end
-
- it "should still raise an ActiveRecord::RecordInvalid exception if we want that" do
- @visitor.avatars.first.name = ''
-
- lambda {
- @visitor.save!
- }.should.raise(ActiveRecord::RecordInvalid)
- end
-end
View
205 vendor/plugins/has_autosave_and_nested_params/test/nested_params_form_builder_test.rb
@@ -1,205 +0,0 @@
-require File.expand_path('../test_helper', __FILE__)
-
-require 'action_controller'
-require 'action_controller/test_process'
-require 'action_view/test_case'
-
-require 'nested_params_form_builder'
-
-class NestedParamsFormBuilderForHasOneTest < ActionView::TestCase
- tests ActionView::Helpers::FormHelper
-
- def setup
- @controller = Class.new do
- attr_reader :url_for_options
- def url_for(options)
- @url_for_options = options
- "http://www.example.com"
- end
- end
- @controller = @controller.new
-
- setup_db
- @member = Member.create(:email => 'paco@example.com')
- end
-
- def teardown
- teardown_db
- end
-
- def test_should_build_a_form_for_an_existing_record
- @member.create_artist(:name => 'Paco')
-
- _erbout = ''
-
- form_for(:member, @member, :builder => NestedParamsFormBuilder) do |f|
- f.fields_for(:artist) do |af|
- _erbout.concat af.text_field(:name)
- end
- end
-
- expected = '<form action="http://www.example.com" method="post">' +
- '<input id="member_artist_attributes_name" name="member[artist_attributes][name]" size="30" type="text" value="Paco" />'
- '</form>'
-
- assert_dom_equal expected, _erbout
- end
-
- def test_should_build_a_form_for_a_new_record
- @member.build_artist(:name => 'Paco')
-
- _erbout = ''
-
- form_for(:member, @member, :builder => NestedParamsFormBuilder) do |f|
- f.fields_for(:artist) do |af|
- _erbout.concat af.text_field(:name)
- end
- end
-
- expected = '<form action="http://www.example.com" method="post">' +
- '<input id="member_artist_attributes_name" name="member[artist_attributes][name]" size="30" type="text" value="Paco" />'
- '</form>'
-
- assert_dom_equal expected, _erbout
- end
-
- private
-
- def protect_against_forgery?
- false
- end
-end
-
-class NestedParamsFormBuilderForHasManyTest < ActionView::TestCase
- tests ActionView::Helpers::FormHelper
-
- def setup
- @controller = Class.new do
- attr_reader :url_for_options
- def url_for(options)
- @url_for_options = options
- "http://www.example.com"
- end
- end
- @controller = @controller.new
-
- setup_db
- @visitor = Visitor.create(:email => 'paco@example.com')
- end
-
- def teardown
- teardown_db
- end
-
- def test_should_build_a_form_for_existing_records
- @visitor.artists.create(:name => 'paco')
- @visitor.artists.create(:name => 'poncho')
-
- _erbout = ''
-
- form_for(:visitor, @visitor, :builder => NestedParamsFormBuilder) do |vf|
- @visitor.artists.each do |artist|
- vf.fields_for(:artists, artist) do |af|
- _erbout.concat af.text_field(:name)
- end
- end
- end
-
- expected = '<form action="http://www.example.com" method="post">' +
- '<input id="visitor_artists_attributes__1_name" name="visitor[artists_attributes][1][name]" size="30" type="text" value="paco" />' +
- '<input id="visitor_artists_attributes__2_name" name="visitor[artists_attributes][2][name]" size="30" type="text" value="poncho" />' +
- '</form>'
-
- assert_dom_equal expected, _erbout
- end
-
- def test_should_build_a_form_for_new_records_using_a_incremental_counter_as_a_composited_id
- paco = @visitor.artists.build(:name => 'paco')
- poncho = @visitor.artists.build(:name => 'poncho')
-
- _erbout = ''
-
- form_for(:visitor, @visitor, :builder => NestedParamsFormBuilder) do |vf|
- @visitor.artists.each do |artist|
- vf.fields_for(:artists, artist) do |af|
- _erbout.concat af.text_field(:name)
- end
- end
- end
-
- expected = '<form action="http://www.example.com" method="post">' +
- "<input id=\"visitor_artists_attributes__new_1_name\" name=\"visitor[artists_attributes][new_1][name]\" size=\"30\" type=\"text\" value=\"paco\" />" +
- "<input id=\"visitor_artists_attributes__new_2_name\" name=\"visitor[artists_attributes][new_2][name]\" size=\"30\" type=\"text\" value=\"poncho\" />" +
- '</form>'
-
- assert_dom_equal expected, _erbout
- end
-
- def test_should_build_a_form_for_existing_and_new_records
- @visitor.artists.create(:name => 'paco')
- poncho = @visitor.artists.build(:name => 'poncho')
-
- _erbout = ''
-
- form_for(:visitor, @visitor, :builder => NestedParamsFormBuilder) do |vf|
- @visitor.artists.each do |artist|
- vf.fields_for(:artists, artist) do |af|
- _erbout.concat af.text_field(:name)
- end
- end
- end
-
- expected = '<form action="http://www.example.com" method="post">' +
- '<input id="visitor_artists_attributes__1_name" name="visitor[artists_attributes][1][name]" size="30" type="text" value="paco" />' +
- "<input id=\"visitor_artists_attributes__new_1_name\" name=\"visitor[artists_attributes][new_1][name]\" size=\"30\" type=\"text\" value=\"poncho\" />" +
- '</form>'
-
- assert_dom_equal expected, _erbout
- end
-
- def test_should_build_a_form_and_yield_the_form_builder_and_each_record
- @visitor.artists.create(:name => 'paco')
- @visitor.artists.create(:name => 'poncho')
-
- _erbout = ''
-
- form_for(:visitor, @visitor, :builder => NestedParamsFormBuilder) do |vf|
- vf.fields_for(:artists) do |af, artist|
- _erbout.concat af.text_field(:name)
- end
- end
-
- expected = '<form action="http://www.example.com" method="post">' +
- '<input id="visitor_artists_attributes__1_name" name="visitor[artists_attributes][1][name]" size="30" type="text" value="paco" />' +
- '<input id="visitor_artists_attributes__2_name" name="visitor[artists_attributes][2][name]" size="30" type="text" value="poncho" />' +
- '</form>'
-
- assert_dom_equal expected, _erbout
- end
-
- def test_should_build_a_form_and_yield_the_form_builder_but_without_the_record
- @visitor.artists.create(:name => 'paco')
- @visitor.artists.create(:name => 'poncho')
-
- _erbout = ''
-
- form_for(:visitor, @visitor, :builder => NestedParamsFormBuilder) do |vf|
- vf.fields_for(:artists) do |af|
- _erbout.concat af.text_field(:name)
- end
- end
-
- expected = '<form action="http://www.example.com" method="post">' +
- '<input id="visitor_artists_attributes__1_name" name="visitor[artists_attributes][1][name]" size="30" type="text" value="paco" />' +
- '<input id="visitor_artists_attributes__2_name" name="visitor[artists_attributes][2][name]" size="30" type="text" value="poncho" />' +
- '</form>'
-
- assert_dom_equal expected, _erbout
- end
-
- private
-
- def protect_against_forgery?
- false
- end
-end
View
223 vendor/plugins/has_autosave_and_nested_params/test/nested_params_test.rb
@@ -1,223 +0,0 @@
-require File.expand_path('../test_helper', __FILE__)
-
-describe "NestedParams, on a has_many association" do
- before do
- setup_db
-
- @visitor = Visitor.create(:email => 'paco@example.com')
- @artist1 = @visitor.artists.create(:name => 'paco')
- @artist2 = @visitor.artists.create(:name => 'poncho')
-
- @valid_alt_params = {
- :artists_attributes => {
- @artist1.id => { :name => 'joe' },
- @artist2.id => { :name => 'jack' }
- }
- }
- end
-
- after do
- teardown_db
- Visitor.class_eval do
- has_many :artists, :nested_params => true, :destroy_missing => true, :reject_empty => true
- end
- end
-
- it "should still work as normal" do
- @visitor.artists = [@artist1]
- @visitor.save!; @visitor.reload
- @visitor.artists.should == [@artist1]
- end
-
- it "should take a hash with string keys and assign the attributes to the associated models" do
- @valid_alt_params[:artists_attributes].stringify_keys!
- @visitor.attributes = @valid_alt_params
- @visitor.artists.map(&:name).sort.should == %w{ jack joe }
- end
-
- it "should take a hash with integer keys and assign the attributes to the associated models" do
- @visitor.attributes = @valid_alt_params
- @visitor.artists.map(&:name).sort.should == %w{ jack joe }
- end
-
- it "should automatically enable autosave on the association" do
- @visitor.attributes = @valid_alt_params
- @visitor.save!; @visitor.reload
-
- @visitor.artists.map(&:name).sort.should == %w{ jack joe }
- end
-
- it "should automatically build a new associated model if there is none" do
- @visitor.artists.destroy_all; @visitor.reload
- @visitor.attributes = { :artists_attributes => [{ :name => 'Pablo' }, { :name => 'Paco' }] }
-
- @visitor.artists.first.should.be.new_record
- @visitor.artists.first.name.should == 'Pablo'
-
- @visitor.artists.last.should.be.new_record
- @visitor.artists.last.name.should == 'Paco'
- end
-
- it "should work with update_attributes as well" do
- @visitor.update_attributes @valid_alt_params
- @visitor.reload
-
- @visitor.artists.map(&:name).sort.should == %w{ jack joe }
- end
-
- it "should update existing records and add new ones that have an id that start with the string 'new_'" do
- @valid_alt_params[:artists_attributes]["new_12345"] = { :name => 'jill' }
-
- assert_difference("Artist.count", +1) do
- @visitor.update_attributes @valid_alt_params
- end
- @visitor.reload
-
- @visitor.artists.map(&:name).sort.should == %w{ jack jill joe }
- end
-
- it "should automatically reject any new record which is empty" do
- @valid_alt_params[:artists_attributes]["new_12345"] = { :name => '' }
-
- assert_no_difference("Artist.count") do
- @visitor.update_attributes @valid_alt_params
- end
-
- @visitor.should.be.valid
- end
-
- it "should automatically destroy a missing record" do
- @valid_alt_params[:artists_attributes].delete(@artist1.id)
-
- assert_difference("Artist.count", -1) do
- @visitor.update_attributes @valid_alt_params
- end
- end
-
- it "should automatically destroy all missing records" do
- assert_difference("Artist.count", -2) do
- @visitor.update_attributes :artists_attributes => {}
- end
- end
-
- it "should create new records and destroy a missing record" do
- @valid_alt_params[:artists_attributes].delete(@artist1.id)
- @valid_alt_params[:artists_attributes]["new_12345"] = { :name => 'jill' }
-
- assert_no_difference("Artist.count") do
- @visitor.update_attributes @valid_alt_params
- end
- @visitor.reload
-
- @visitor.artists.map(&:name).sort.should == %w{ jack jill }
- end
-
- it "should not destroy any missing records by default" do
- Visitor.class_eval do
- has_many :artists, :nested_params => true
- end
-
- @valid_alt_params[:artists_attributes].delete(@artist1.id)
-
- assert_no_difference("Artist.count") do
- @visitor.update_attributes @valid_alt_params
- end
- end
-
- it "should not destroy any missing records when turned off" do
- Visitor.class_eval do
- has_many :artists, :nested_params => true, :destroy_missing => false
- end
-
- @valid_alt_params[:artists_attributes].delete(@artist1.id)
-
- assert_no_difference("Artist.count") do
- @visitor.update_attributes @valid_alt_params
- end
- end
-
- it "should rollback any changes if an exception occurred while saving" do
- Artist.any_instance.stubs(:save).raises(RuntimeError, 'Error!')
-
- lambda {
- @visitor.update_attributes({
- :email => 'poncho@example.com',
- :artists_attributes => { @visitor.artists.last.id => { :member_id => 123 } }
- }).should.be false
- }.should.not.raise(Exception)
-
- @visitor.reload
- @visitor.email.should == 'paco@example.com'
- @visitor.artists.last.member_id.should.be.blank
- end
-end
-
-describe "NestedParams, on a has_one association" do
- before do
- setup_db
-
- @member = Member.create(:email => 'paco@example.com')
- @artist = @member.create_artist(:name => 'Mister Paco')
- end
-
- after do
- teardown_db
- end
-
- it "should still work as normal" do
- @member.artist = Artist.new(:name => 'Poncho')
- @member.artist.name.should == 'Poncho'
- end
-
- it "should take a hash and assign the attributes to the associated model" do
- @member.attributes = { :email => 'pablo@example.com', :artist_attributes => { :name => 'Pablo' } }
- @member.email.should == 'pablo@example.com'
- @member.artist.name.should == 'Pablo'
- end
-
- it "should also work with a HashWithIndifferentAccess" do
- @member.attributes = HashWithIndifferentAccess.new(:email => 'pablo@example.com', :artist_attributes => HashWithIndifferentAccess.new(:name => 'Pablo'))
- @member.email.should == 'pablo@example.com'
- @member.artist.name.should == 'Pablo'
- end
-
- it "should work with update_attributes as well" do
- @member.update_attributes({ :email => 'pablo@example.com', :artist_attributes => { :name => 'Pablo' } })
- @member.reload
- @member.artist.name.should == 'Pablo'
- end
-
- it "should automatically instantiate an associated model if there is none" do
- @artist.destroy; @member.reload
- @member.attributes = { :artist_attributes => { :name => 'Pablo' } }
-
- @member.artist.should.be.new_record
- @member.artist.name.should == 'Pablo'
- end
-
- it "should automatically extend the model class with the AutosaveAssociation module" do
- klass = Class.new do
- class << self
- def has_one(*args); end
- def has_many(*args); end
- end
-
- def save(*args); end
-
- extend NestedParams
- end
- (class << klass; self; end).should.include AutosaveAssociation
- end
-
- it "should automatically enable autosave on the association" do
- @member.attributes = { :email => 'pablo@example.com', :artist_attributes => { :name => 'Pablo' } }
- @member.save!; @member.reload
- @member.artist.name.should == 'Pablo'
- end
-
- it "should automatically validate the associated model" do
- @member.artist.name = ''
- @member.should.not.be.valid
- @member.errors.on(:name).should.not.be.blank
- end
-end
View
73 vendor/plugins/has_autosave_and_nested_params/test/test_helper.rb
@@ -1,73 +0,0 @@
-require 'rubygems'
-require 'test/spec'
-require 'mocha'
-
-require 'activerecord'
-
-$: << File.expand_path('../../lib', __FILE__)
-require 'nested_params'
-
-ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
-ActiveRecord::Migration.verbose = false
-# ActiveRecord::Base.logger = Logger.new($stdout)
-# ActiveRecord::Base.logger.level = Logger::DEBUG
-
-def setup_db
- ActiveRecord::Schema.define(:version => 1) do
- create_table :members do |t|
- t.column :email, :string
- t.column :address, :string
- end
-
- create_table :visitors do |t|
- t.column :email, :string
- t.column :address, :string
- end
-
- create_table :artists do |t|
- t.integer :member_id
- t.integer :visitor_id
- t.string :name
- end
-
- create_table :avatars do |t|
- t.integer :member_id
- t.integer :visitor_id
- t.string :name
- end
- end
-end
-
-def teardown_db
- ActiveRecord::Base.connection.tables.each do |table|
- ActiveRecord::Base.connection.drop_table(table)
- end
-end
-
-class Member < ActiveRecord::Base
- extend NestedParams
- has_one :artist, :nested_params => true
-
- extend AutosaveAssociation
- has_one :avatar, :autosave => true
-end
-
-class Visitor < ActiveRecord::Base
- extend NestedParams
- has_many :artists, :nested_params => true, :destroy_missing => true, :reject_empty => true
-
- extend AutosaveAssociation
- has_many :avatars, :autosave => true
-end
-
-class Artist < ActiveRecord::Base
- belongs_to :member
-
- validates_presence_of :name
-end
-
-class Avatar < ActiveRecord::Base
- belongs_to :member
-
- validates_presence_of :name
-end
1  vendor/rails
@@ -1 +0,0 @@
-Subproject commit 14a16844bbb3ba9edb14269ce2d0b61c9a43637e
Please sign in to comment.
Something went wrong with that request. Please try again.