diff --git a/.gitignore b/.gitignore index 24e4b0e..e8b77d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,18 @@ -.bundle/ -log/*.log -pkg/ -test/dummy/db/*.sqlite3 -test/dummy/db/*.sqlite3-journal -test/dummy/log/*.log -test/dummy/tmp/ -test/dummy/.sass-cache -tmp +# Ignore bundler config. +/.bundle + +# Ignore Gemfile.lock +/Gemfile.lock +/gemfiles/*.gemfile.lock + +# Ignore test files +/coverage + +# Ignore dummy app files +/test/dummy/db/*.sqlite3 +/test/dummy/db/*.sqlite3-journal +/test/dummy/log/*.log +/test/dummy/tmp/ + +# Temp files +/tmp diff --git a/.travis.yml b/.travis.yml index 1b6366a..575b833 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,18 @@ language: ruby +sudo: false +cache: bundler rvm: - - 2.1 - - 2.2 + - 2.4.1 + - 2.3.4 + - 2.2.7 +gemfile: + - gemfiles/rails_4.2.8.gemfile + - gemfiles/rails_5.0.2.gemfile + - gemfiles/rails_5.1.0.gemfile script: - cd test/dummy/ - RAILS_ENV=test bundle exec rake db:migrate - cd ../.. - bundle exec rake +after_success: + - bundle exec codeclimate-test-reporter diff --git a/Appraisals b/Appraisals new file mode 100644 index 0000000..796ffe9 --- /dev/null +++ b/Appraisals @@ -0,0 +1,12 @@ +RAILS_VERSIONS = %w( + 4.2.8 + 5.0.2 + 5.1.0 +) + +RAILS_VERSIONS.each do |version| + appraise "rails_#{version}" do + gem 'rails', version + gem 'rails-controller-testing' if version != '4.2.8' + end +end diff --git a/Gemfile b/Gemfile index 59cffb6..2eab805 100644 --- a/Gemfile +++ b/Gemfile @@ -3,4 +3,8 @@ source 'https://rubygems.org' gemspec gem 'jquery-rails' -gem 'simple_form' + +group :test do + gem 'simple_form' + gem 'codeclimate-test-reporter', require: nil +end diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index a4aa4c3..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,96 +0,0 @@ -PATH - remote: . - specs: - actionform (0.0.1) - rails (~> 4.1) - -GEM - remote: https://rubygems.org/ - specs: - actionmailer (4.1.8) - actionpack (= 4.1.8) - actionview (= 4.1.8) - mail (~> 2.5, >= 2.5.4) - actionpack (4.1.8) - actionview (= 4.1.8) - activesupport (= 4.1.8) - rack (~> 1.5.2) - rack-test (~> 0.6.2) - actionview (4.1.8) - activesupport (= 4.1.8) - builder (~> 3.1) - erubis (~> 2.7.0) - activemodel (4.1.8) - activesupport (= 4.1.8) - builder (~> 3.1) - activerecord (4.1.8) - activemodel (= 4.1.8) - activesupport (= 4.1.8) - arel (~> 5.0.0) - activesupport (4.1.8) - i18n (~> 0.6, >= 0.6.9) - json (~> 1.7, >= 1.7.7) - minitest (~> 5.1) - thread_safe (~> 0.1) - tzinfo (~> 1.1) - arel (5.0.1.20140414130214) - builder (3.2.2) - erubis (2.7.0) - hike (1.2.3) - i18n (0.6.11) - jquery-rails (3.1.1) - railties (>= 3.0, < 5.0) - thor (>= 0.14, < 2.0) - json (1.8.1) - mail (2.6.3) - mime-types (>= 1.16, < 3) - mime-types (2.4.3) - minitest (5.5.0) - multi_json (1.10.1) - rack (1.5.2) - rack-test (0.6.2) - rack (>= 1.0) - rails (4.1.8) - actionmailer (= 4.1.8) - actionpack (= 4.1.8) - actionview (= 4.1.8) - activemodel (= 4.1.8) - activerecord (= 4.1.8) - activesupport (= 4.1.8) - bundler (>= 1.3.0, < 2.0) - railties (= 4.1.8) - sprockets-rails (~> 2.0) - railties (4.1.8) - actionpack (= 4.1.8) - activesupport (= 4.1.8) - rake (>= 0.8.7) - thor (>= 0.18.1, < 2.0) - rake (10.3.2) - simple_form (3.0.2) - actionpack (~> 4.0) - activemodel (~> 4.0) - sprockets (2.12.3) - hike (~> 1.2) - multi_json (~> 1.0) - rack (~> 1.0) - tilt (~> 1.1, != 1.3.0) - sprockets-rails (2.2.4) - actionpack (>= 3.0) - activesupport (>= 3.0) - sprockets (>= 2.8, < 4.0) - sqlite3 (1.3.9) - thor (0.19.1) - thread_safe (0.3.4) - tilt (1.4.1) - tzinfo (1.2.2) - thread_safe (~> 0.1) - -PLATFORMS - ruby - -DEPENDENCIES - actionform! - jquery-rails - rake (~> 10.3.2) - simple_form - sqlite3 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..72dc60d --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +The MIT License (MIT) + +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. diff --git a/MIT-LICENSE b/MIT-LICENSE deleted file mode 100644 index ea966ec..0000000 --- a/MIT-LICENSE +++ /dev/null @@ -1,20 +0,0 @@ -Copyright 2014 YOURNAME - -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. diff --git a/README.md b/README.md index 5ab3a4a..f490afc 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,11 @@ -# Action Form - -# DISCLAIMER: This project is an experiment and should not be used in applications. - -[![Build Status](https://api.travis-ci.org/railsgsoc/actionform.svg?branch=master)](https://travis-ci.org/railsgsoc/actionform) +## Action Form + +[![GitHub license](https://img.shields.io/github/license/jbox-web/actionform.svg)](https://github.com/jbox-web/actionform/blob/master/LICENSE) +[![GitHub release](https://img.shields.io/github/release/jbox-web/actionform.svg)](https://github.com/jbox-web/actionform/releases/latest) +[![Build Status](https://travis-ci.org/jbox-web/actionform.svg?branch=master)](https://travis-ci.org/jbox-web/actionform) +[![Code Climate](https://codeclimate.com/github/jbox-web/actionform/badges/gpa.svg)](https://codeclimate.com/github/jbox-web/actionform) +[![Test Coverage](https://codeclimate.com/github/jbox-web/actionform/badges/coverage.svg)](https://codeclimate.com/github/jbox-web/actionform/coverage) +[![Dependency Status](https://gemnasium.com/badges/github.com/jbox-web/actionform.svg)](https://gemnasium.com/github.com/jbox-web/actionform) Set your models free from the `accepts_nested_attributes_for` helper. Action Form provides an object-oriented approach to represent your forms by building a form object, rather than relying on Active Record internals for doing this. Form objects provide an API to describe the models involved in the form, their attributes and validations. A form object deals with create/update actions of nested objects in a more seamless way. diff --git a/Rakefile b/Rakefile index 996df19..7cedae2 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,7 @@ +#!/usr/bin/env rake + +require 'rubygems' + begin require 'bundler/setup' rescue LoadError @@ -6,6 +10,7 @@ end require 'rdoc/task' +desc 'Generate documentation.' RDoc::Task.new(:rdoc) do |rdoc| rdoc.rdoc_dir = 'rdoc' rdoc.title = 'ActionForm' @@ -14,9 +19,6 @@ RDoc::Task.new(:rdoc) do |rdoc| rdoc.rdoc_files.include('lib/**/*.rb') end - - - Bundler::GemHelper.install_tasks require 'rake/testtask' @@ -28,5 +30,4 @@ Rake::TestTask.new(:test) do |t| t.verbose = false end - task default: :test diff --git a/actionform.gemspec b/actionform.gemspec index 888c65d..c027d2b 100644 --- a/actionform.gemspec +++ b/actionform.gemspec @@ -1,22 +1,27 @@ +# -*- encoding: utf-8 -*- $:.push File.expand_path('../lib', __FILE__) - require 'action_form/version' Gem::Specification.new do |s| s.name = 'actionform' s.version = ActionForm::VERSION + s.platform = Gem::Platform::RUBY s.authors = ['Petros Markou'] s.email = ['markoupetr@gmail.com'] - s.homepage = 'https://github.com/rails/actionform' + s.homepage = 'https://github.com/jbox-web/actionform' s.summary = 'Create nested forms with ease.' s.description = 'An alternative layer to accepts_nested_attributes_for by using Form Models.' s.license = 'MIT' - s.files = Dir['{app,config,db,lib}/**/*', 'MIT-LICENSE', 'Rakefile', 'README.md'] - s.test_files = Dir['test/**/*'] - - s.add_dependency 'rails', '~> 4.1' + s.add_dependency 'rails', '>= 4.2' s.add_development_dependency 'sqlite3' - s.add_development_dependency 'rake', '~> 10.3.2' + s.add_development_dependency 'rake' + s.add_development_dependency 'simplecov' + s.add_development_dependency 'appraisal' + + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + s.require_paths = ['lib'] end diff --git a/gemfiles/rails_4.2.8.gemfile b/gemfiles/rails_4.2.8.gemfile new file mode 100644 index 0000000..a2fa69b --- /dev/null +++ b/gemfiles/rails_4.2.8.gemfile @@ -0,0 +1,13 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "jquery-rails" +gem "rails", "4.2.8" + +group :test do + gem "simple_form" + gem "codeclimate-test-reporter", require: nil +end + +gemspec path: "../" diff --git a/gemfiles/rails_5.0.2.gemfile b/gemfiles/rails_5.0.2.gemfile new file mode 100644 index 0000000..f9f86a8 --- /dev/null +++ b/gemfiles/rails_5.0.2.gemfile @@ -0,0 +1,14 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "jquery-rails" +gem "rails", "5.0.2" +gem "rails-controller-testing" + +group :test do + gem "simple_form" + gem "codeclimate-test-reporter", require: nil +end + +gemspec path: "../" diff --git a/gemfiles/rails_5.1.0.gemfile b/gemfiles/rails_5.1.0.gemfile new file mode 100644 index 0000000..07f7e8d --- /dev/null +++ b/gemfiles/rails_5.1.0.gemfile @@ -0,0 +1,14 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "jquery-rails" +gem "rails", "5.1.0" +gem "rails-controller-testing" + +group :test do + gem "simple_form" + gem "codeclimate-test-reporter", require: nil +end + +gemspec path: "../" diff --git a/lib/action_form/base.rb b/lib/action_form/base.rb index 4d46f00..53a9e72 100644 --- a/lib/action_form/base.rb +++ b/lib/action_form/base.rb @@ -21,6 +21,10 @@ def get_model(assoc_name) form.get_model(assoc_name) end + def save! + save or raise ActiveRecord::RecordInvalid.new(self) + end + def save if valid? run_callbacks :save do diff --git a/lib/action_form/form.rb b/lib/action_form/form.rb index b343219..2463a7a 100644 --- a/lib/action_form/form.rb +++ b/lib/action_form/form.rb @@ -13,7 +13,7 @@ def initialize(assoc_name, parent, proc, model=nil) @forms = [] @proc = proc enable_autosave - instance_eval &proc + instance_eval(&proc) end def class diff --git a/lib/action_form/form_collection.rb b/lib/action_form/form_collection.rb index 8bc4e41..6f3170b 100644 --- a/lib/action_form/form_collection.rb +++ b/lib/action_form/form_collection.rb @@ -20,6 +20,7 @@ def update_models def submit(params) params.each do |key, value| + value = value.to_h if value.is_a?(ActionController::Parameters) && rails_5? if parent.persisted? create_or_update_record(value) else @@ -120,7 +121,7 @@ def assign_forms end def dynamic_key?(i) - i > forms.size + i >= forms.size end def aggregate_form_errors @@ -171,6 +172,9 @@ def create_form forms << new_form new_form end - end + def rails_5? + Rails.version[0] == '5' + end + end end diff --git a/lib/action_form/form_helpers.rb b/lib/action_form/form_helpers.rb index 6b1b932..b05e3df 100644 --- a/lib/action_form/form_helpers.rb +++ b/lib/action_form/form_helpers.rb @@ -15,7 +15,7 @@ def submit(params) def valid? super model.valid? - + collect_errors_from(model) aggregate_form_errors @@ -23,7 +23,7 @@ def valid? end def nested_params?(value) - value.is_a?(Hash) + value.is_a?(Hash) || value.is_a?(ActionController::Parameters) end def find_association_name_in(key) @@ -60,4 +60,4 @@ def collect_errors_from(validatable_object) end end end -end \ No newline at end of file +end diff --git a/test/dummy/config/environments/test.rb b/test/dummy/config/environments/test.rb index 053f5b6..8c7edd1 100644 --- a/test/dummy/config/environments/test.rb +++ b/test/dummy/config/environments/test.rb @@ -13,8 +13,13 @@ config.eager_load = false # Configure static asset server for tests with Cache-Control for performance. - config.serve_static_assets = true - config.static_cache_control = 'public, max-age=3600' + if Rails.version[0] == '4' + config.serve_static_files = true + config.static_cache_control = 'public, max-age=3600' + else + config.public_file_server.enabled = true + config.public_file_server.headers = { 'Cache-Control' => 'public, max-age=3600' } + end # Show full error reports and disable caching. config.consider_all_requests_local = true @@ -36,4 +41,7 @@ # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + + # Test order + config.active_support.test_order = :random end diff --git a/test/dummy/config/initializers/simple_form.rb b/test/dummy/config/initializers/simple_form.rb index 4937a4d..82a938e 100644 --- a/test/dummy/config/initializers/simple_form.rb +++ b/test/dummy/config/initializers/simple_form.rb @@ -78,7 +78,7 @@ # config.collection_value_methods = [ :id, :to_s ] # How the label text should be generated altogether with the required text. - config.label_text = lambda { |label, required| "#{label} #{required}" } + config.label_text = lambda { |label, required, explicit_label| "#{label} #{required}" } # You can define the class to use on all labels. Default is nil. # config.label_class = nil @@ -121,4 +121,4 @@ # Default class for buttons config.button_class = 'btn' -end \ No newline at end of file +end diff --git a/test/dummy/db/migrate/20140729162110_create_users.rb b/test/dummy/db/migrate/20140729162110_create_users.rb index 1a6785a..581e280 100644 --- a/test/dummy/db/migrate/20140729162110_create_users.rb +++ b/test/dummy/db/migrate/20140729162110_create_users.rb @@ -5,7 +5,7 @@ def change t.integer :age t.integer :gender - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140729162246_create_emails.rb b/test/dummy/db/migrate/20140729162246_create_emails.rb index 21f5452..179cbd6 100644 --- a/test/dummy/db/migrate/20140729162246_create_emails.rb +++ b/test/dummy/db/migrate/20140729162246_create_emails.rb @@ -4,7 +4,7 @@ def change t.string :address t.references :user, index: true - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140729162335_create_profiles.rb b/test/dummy/db/migrate/20140729162335_create_profiles.rb index fb8aee9..b8e30fe 100644 --- a/test/dummy/db/migrate/20140729162335_create_profiles.rb +++ b/test/dummy/db/migrate/20140729162335_create_profiles.rb @@ -5,7 +5,7 @@ def change t.string :github_name t.references :user, index: true - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140729164623_create_songs.rb b/test/dummy/db/migrate/20140729164623_create_songs.rb index 4b7a6fe..42f39af 100644 --- a/test/dummy/db/migrate/20140729164623_create_songs.rb +++ b/test/dummy/db/migrate/20140729164623_create_songs.rb @@ -4,7 +4,7 @@ def change t.string :title t.string :length - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140729164653_create_artists.rb b/test/dummy/db/migrate/20140729164653_create_artists.rb index ff53396..9536d3b 100644 --- a/test/dummy/db/migrate/20140729164653_create_artists.rb +++ b/test/dummy/db/migrate/20140729164653_create_artists.rb @@ -4,7 +4,7 @@ def change t.string :name t.references :song, index: true - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140729164713_create_producers.rb b/test/dummy/db/migrate/20140729164713_create_producers.rb index 5ee5364..2456ec8 100644 --- a/test/dummy/db/migrate/20140729164713_create_producers.rb +++ b/test/dummy/db/migrate/20140729164713_create_producers.rb @@ -5,7 +5,7 @@ def change t.string :studio t.references :artist, index: true - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140729165425_create_conferences.rb b/test/dummy/db/migrate/20140729165425_create_conferences.rb index b00f221..f645624 100644 --- a/test/dummy/db/migrate/20140729165425_create_conferences.rb +++ b/test/dummy/db/migrate/20140729165425_create_conferences.rb @@ -4,7 +4,7 @@ def change t.string :name t.string :city - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140729165445_create_speakers.rb b/test/dummy/db/migrate/20140729165445_create_speakers.rb index 703c9f5..acb3d49 100644 --- a/test/dummy/db/migrate/20140729165445_create_speakers.rb +++ b/test/dummy/db/migrate/20140729165445_create_speakers.rb @@ -5,7 +5,7 @@ def change t.string :occupation t.references :conference, index: true - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140729165506_create_presentations.rb b/test/dummy/db/migrate/20140729165506_create_presentations.rb index 478decb..87f03c7 100644 --- a/test/dummy/db/migrate/20140729165506_create_presentations.rb +++ b/test/dummy/db/migrate/20140729165506_create_presentations.rb @@ -5,7 +5,7 @@ def change t.string :duration t.references :speaker, index: true - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140729170014_create_surveys.rb b/test/dummy/db/migrate/20140729170014_create_surveys.rb index 4ccf117..c9bb4b5 100644 --- a/test/dummy/db/migrate/20140729170014_create_surveys.rb +++ b/test/dummy/db/migrate/20140729170014_create_surveys.rb @@ -3,7 +3,7 @@ def change create_table :surveys do |t| t.string :name - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140729170034_create_questions.rb b/test/dummy/db/migrate/20140729170034_create_questions.rb index 70a0c2a..3db8bc1 100644 --- a/test/dummy/db/migrate/20140729170034_create_questions.rb +++ b/test/dummy/db/migrate/20140729170034_create_questions.rb @@ -4,7 +4,7 @@ def change t.text :content t.references :survey, index: true - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140729170112_create_answers.rb b/test/dummy/db/migrate/20140729170112_create_answers.rb index b042ed6..a7dfeae 100644 --- a/test/dummy/db/migrate/20140729170112_create_answers.rb +++ b/test/dummy/db/migrate/20140729170112_create_answers.rb @@ -4,7 +4,7 @@ def change t.text :content t.references :question, index: true - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140806122636_create_projects.rb b/test/dummy/db/migrate/20140806122636_create_projects.rb index 8c93aba..a2905bc 100644 --- a/test/dummy/db/migrate/20140806122636_create_projects.rb +++ b/test/dummy/db/migrate/20140806122636_create_projects.rb @@ -4,7 +4,7 @@ def change t.string :name t.string :description - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140806122735_create_tasks.rb b/test/dummy/db/migrate/20140806122735_create_tasks.rb index 2665660..1b762bf 100644 --- a/test/dummy/db/migrate/20140806122735_create_tasks.rb +++ b/test/dummy/db/migrate/20140806122735_create_tasks.rb @@ -6,7 +6,7 @@ def change t.boolean :done t.references :project, index: true - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140806122805_create_sub_tasks.rb b/test/dummy/db/migrate/20140806122805_create_sub_tasks.rb index afdff0b..4e6bc23 100644 --- a/test/dummy/db/migrate/20140806122805_create_sub_tasks.rb +++ b/test/dummy/db/migrate/20140806122805_create_sub_tasks.rb @@ -6,7 +6,7 @@ def change t.boolean :done t.references :task, index: true - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140806122834_create_people.rb b/test/dummy/db/migrate/20140806122834_create_people.rb index 1350fe9..462a442 100644 --- a/test/dummy/db/migrate/20140806122834_create_people.rb +++ b/test/dummy/db/migrate/20140806122834_create_people.rb @@ -6,7 +6,7 @@ def change t.string :description t.references :project, index: true - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140806122945_create_tags.rb b/test/dummy/db/migrate/20140806122945_create_tags.rb index 048be7c..965cd7a 100644 --- a/test/dummy/db/migrate/20140806122945_create_tags.rb +++ b/test/dummy/db/migrate/20140806122945_create_tags.rb @@ -3,7 +3,7 @@ def change create_table :tags do |t| t.string :name - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140806123008_create_project_tags.rb b/test/dummy/db/migrate/20140806123008_create_project_tags.rb index cc784eb..93c61f1 100644 --- a/test/dummy/db/migrate/20140806123008_create_project_tags.rb +++ b/test/dummy/db/migrate/20140806123008_create_project_tags.rb @@ -4,7 +4,7 @@ def change t.references :project, index: true t.references :tag, index: true - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/db/migrate/20140820071344_create_assignments.rb b/test/dummy/db/migrate/20140820071344_create_assignments.rb index fc240cf..fa0e823 100644 --- a/test/dummy/db/migrate/20140820071344_create_assignments.rb +++ b/test/dummy/db/migrate/20140820071344_create_assignments.rb @@ -3,7 +3,7 @@ def change create_table :assignments do |t| t.string :name - t.timestamps + t.timestamps null: false end end end diff --git a/test/dummy/test/controllers/assignments_controller_test.rb b/test/dummy/test/controllers/assignments_controller_test.rb index d2b916d..d025d31 100644 --- a/test/dummy/test/controllers/assignments_controller_test.rb +++ b/test/dummy/test/controllers/assignments_controller_test.rb @@ -20,15 +20,18 @@ class AssignmentsControllerTest < ActionController::TestCase test "should create assignment" do assert_difference('Assignment.count') do - post :create, assignment: { - name: "Life", - - tasks_attributes: { - "0" => { name: "Eat" }, - "1" => { name: "Pray" }, - "2" => { name: "Love" }, + params = { + assignment: { + name: "Life", + + tasks_attributes: { + "0" => { name: "Eat" }, + "1" => { name: "Pray" }, + "2" => { name: "Love" }, + } } } + post :create, wrapped_params(params) end assignment_form = assigns(:assignment_form) @@ -53,15 +56,18 @@ class AssignmentsControllerTest < ActionController::TestCase assignment = assignments(:yard) assert_difference('Assignment.count', 0) do - post :create, assignment: { - name: assignment.name, - - tasks_attributes: { - "0" => { name: nil }, - "1" => { name: nil }, - "2" => { name: nil }, + params = { + assignment: { + name: assignment.name, + + tasks_attributes: { + "0" => { name: nil }, + "1" => { name: nil }, + "2" => { name: nil }, + } } } + post :create, wrapped_params(params) end assignment_form = assigns(:assignment_form) @@ -75,26 +81,29 @@ class AssignmentsControllerTest < ActionController::TestCase end test "should show assignment" do - get :show, id: @assignment + get :show, wrapped_params(id: @assignment) assert_response :success end test "should get edit" do - get :edit, id: @assignment + get :edit, wrapped_params(id: @assignment) assert_response :success end test "should update assignment" do assert_difference('Assignment.count', 0) do - patch :update, id: @assignment, assignment: { - name: "Car service", - - tasks_attributes: { - "0" => { name: "Wash tires", id: tasks(:rake).id }, - "1" => { name: "Clean inside", id: tasks(:paint).id }, - "2" => { name: "Check breaks", id: tasks(:clean).id }, + params = { + id: @assignment, assignment: { + name: "Car service", + + tasks_attributes: { + "0" => { name: "Wash tires", id: tasks(:rake).id }, + "1" => { name: "Clean inside", id: tasks(:paint).id }, + "2" => { name: "Check breaks", id: tasks(:clean).id }, + } } } + patch :update, wrapped_params(params) end assignment_form = assigns(:assignment_form) @@ -112,7 +121,7 @@ class AssignmentsControllerTest < ActionController::TestCase test "should destroy assignment" do assert_difference('Assignment.count', -1) do - delete :destroy, id: @assignment + delete :destroy, wrapped_params(id: @assignment) end assert_redirected_to assignments_path diff --git a/test/dummy/test/controllers/conferences_controller_test.rb b/test/dummy/test/controllers/conferences_controller_test.rb index b631832..66c2e8a 100644 --- a/test/dummy/test/controllers/conferences_controller_test.rb +++ b/test/dummy/test/controllers/conferences_controller_test.rb @@ -20,20 +20,23 @@ class ConferencesControllerTest < ActionController::TestCase test "should create conference" do assert_difference('Conference.count') do - post :create, conference: { - name: "Euruco", - city: "Athens", - - speaker_attributes: { - name: "Petros Markou", - occupation: "Developer", - - presentations_attributes: { - "0" => { topic: "Ruby OOP", duration: "1h" }, - "1" => { topic: "Ruby Closures", duration: "1h" }, + params = { + conference: { + name: "Euruco", + city: "Athens", + + speaker_attributes: { + name: "Petros Markou", + occupation: "Developer", + + presentations_attributes: { + "0" => { topic: "Ruby OOP", duration: "1h" }, + "1" => { topic: "Ruby Closures", duration: "1h" }, + } } } } + post :create, wrapped_params(params) end conference_form = assigns(:conference_form) @@ -43,10 +46,10 @@ class ConferencesControllerTest < ActionController::TestCase assert_equal "Euruco", conference_form.name assert_equal "Athens", conference_form.city - + assert_equal "Petros Markou", conference_form.speaker.name assert_equal "Developer", conference_form.speaker.occupation - + assert_equal "Ruby OOP", conference_form.speaker.presentations[0].topic assert_equal "1h", conference_form.speaker.presentations[0].duration assert_equal "Ruby Closures", conference_form.speaker.presentations[1].topic @@ -56,27 +59,30 @@ class ConferencesControllerTest < ActionController::TestCase conference_form.speaker.presentations.each do |presentation| presentation.persisted? end - + assert_equal "Conference: #{conference_form.name} was successfully created.", flash[:notice] end test "should create dynamically added presentation to speaker" do assert_difference('Conference.count') do - post :create, conference: { - name: "Euruco", - city: "Athens", - - speaker_attributes: { - name: "Petros Markou", - occupation: "Developer", - - presentations_attributes: { - "0" => { topic: "Ruby OOP", duration: "1h" }, - "1" => { topic: "Ruby Closures", duration: "1h" }, - "12312" => { topic: "Ruby Metaprogramming", duration: "2h" } + params = { + conference: { + name: "Euruco", + city: "Athens", + + speaker_attributes: { + name: "Petros Markou", + occupation: "Developer", + + presentations_attributes: { + "0" => { topic: "Ruby OOP", duration: "1h" }, + "1" => { topic: "Ruby Closures", duration: "1h" }, + "12312" => { topic: "Ruby Metaprogramming", duration: "2h" } + } } } } + post :create, wrapped_params(params) end conference_form = assigns(:conference_form) @@ -86,12 +92,12 @@ class ConferencesControllerTest < ActionController::TestCase assert_equal "Euruco", conference_form.name assert_equal "Athens", conference_form.city - + assert_equal "Petros Markou", conference_form.speaker.name assert_equal "Developer", conference_form.speaker.occupation assert_equal 3, conference_form.speaker.presentations.size - + assert_equal "Ruby OOP", conference_form.speaker.presentations[0].topic assert_equal "1h", conference_form.speaker.presentations[0].duration assert_equal "Ruby Closures", conference_form.speaker.presentations[1].topic @@ -103,7 +109,7 @@ class ConferencesControllerTest < ActionController::TestCase conference_form.speaker.presentations.each do |presentation| presentation.persisted? end - + assert_equal "Conference: #{conference_form.name} was successfully created.", flash[:notice] end @@ -111,29 +117,32 @@ class ConferencesControllerTest < ActionController::TestCase conference = conferences(:ruby) assert_difference(['Conference.count', 'Speaker.count'], 0) do - post :create, conference: { - name: conference.name, - city: nil, - - speaker_attributes: { - name: conference.speaker.name, - occupation: "Developer", - - presentations_attributes: { - "0" => { topic: nil, duration: "1h" }, - "1" => { topic: "Ruby Closures", duration: nil }, + params = { + conference: { + name: conference.name, + city: nil, + + speaker_attributes: { + name: conference.speaker.name, + occupation: "Developer", + + presentations_attributes: { + "0" => { topic: nil, duration: "1h" }, + "1" => { topic: "Ruby Closures", duration: nil }, + } } } } + post :create, wrapped_params(params) end conference_form = assigns(:conference_form) assert_not conference_form.valid? - assert_includes conference_form.errors.messages[:name], "has already been taken" + assert_includes conference_form.errors.messages[:name], "has already been taken" assert_includes conference_form.errors.messages[:city], "can't be blank" - + assert_includes conference_form.speaker.errors.messages[:name], "has already been taken" assert_includes conference_form.speaker.presentations[0].errors.messages[:topic], "can't be blank" @@ -141,90 +150,96 @@ class ConferencesControllerTest < ActionController::TestCase end test "should show conference" do - get :show, id: @conference + get :show, wrapped_params(id: @conference) assert_response :success end test "should get edit" do - get :edit, id: @conference + get :edit, wrapped_params(id: @conference) assert_response :success end test "should update conference" do assert_difference('Conference.count', 0) do - patch :update, id: @conference, conference: { - name: "GoGaruco", - city: "Golden State", - - speaker_attributes: { - name: "John Doe", - occupation: "Developer", - - presentations_attributes: { - "0" => { topic: "Rails OOP", duration: "1h", id: presentations(:ruby_oop).id }, - "1" => { topic: "Rails Patterns", duration: "1h", id: presentations(:ruby_closures).id }, + params = { + id: @conference, conference: { + name: "GoGaruco", + city: "Golden State", + + speaker_attributes: { + name: "John Doe", + occupation: "Developer", + + presentations_attributes: { + "0" => { topic: "Rails OOP", duration: "1h", id: presentations(:ruby_oop).id }, + "1" => { topic: "Rails Patterns", duration: "1h", id: presentations(:ruby_closures).id }, + } } } } + patch :update, wrapped_params(params) end conference_form = assigns(:conference_form) assert_redirected_to conference_path(conference_form) - + assert_equal "GoGaruco", conference_form.name assert_equal "Golden State", conference_form.city - + assert_equal "John Doe", conference_form.speaker.name assert_equal "Developer", conference_form.speaker.occupation - + assert_equal "Rails Patterns", conference_form.speaker.presentations[0].topic assert_equal "1h", conference_form.speaker.presentations[0].duration assert_equal "Rails OOP", conference_form.speaker.presentations[1].topic assert_equal "1h", conference_form.speaker.presentations[1].duration - + assert_equal "Conference: #{conference_form.name} was successfully updated.", flash[:notice] end test "should destroy dynamically removed presentation from speaker" do assert_difference('Conference.count', 0) do - patch :update, id: @conference, conference: { - name: "GoGaruco", - city: "Golden State", - - speaker_attributes: { - name: "John Doe", - occupation: "Developer", - - presentations_attributes: { - "0" => { topic: "Rails OOP", duration: "1h", id: presentations(:ruby_oop).id }, - "1" => { topic: "Rails Patterns", duration: "1h", id: presentations(:ruby_closures).id, _destroy: "1" }, + params = { + id: @conference, conference: { + name: "GoGaruco", + city: "Golden State", + + speaker_attributes: { + name: "John Doe", + occupation: "Developer", + + presentations_attributes: { + "0" => { topic: "Rails OOP", duration: "1h", id: presentations(:ruby_oop).id }, + "1" => { topic: "Rails Patterns", duration: "1h", id: presentations(:ruby_closures).id, _destroy: "1" }, + } } } } + patch :update, wrapped_params(params) end conference_form = assigns(:conference_form) assert_redirected_to conference_path(conference_form) - + assert_equal "GoGaruco", conference_form.name assert_equal "Golden State", conference_form.city - + assert_equal "John Doe", conference_form.speaker.name assert_equal "Developer", conference_form.speaker.occupation - + assert_equal "Rails OOP", conference_form.speaker.presentations[0].topic assert_equal "1h", conference_form.speaker.presentations[0].duration assert_equal 1, conference_form.speaker.presentations.size - + assert_equal "Conference: #{conference_form.name} was successfully updated.", flash[:notice] end test "should destroy conference" do assert_difference('Conference.count', -1) do - delete :destroy, id: @conference + delete :destroy, wrapped_params(id: @conference) end assert_redirected_to conferences_path diff --git a/test/dummy/test/controllers/projects_controller_test.rb b/test/dummy/test/controllers/projects_controller_test.rb index d2d0ca0..361b612 100644 --- a/test/dummy/test/controllers/projects_controller_test.rb +++ b/test/dummy/test/controllers/projects_controller_test.rb @@ -2,7 +2,7 @@ class ProjectsControllerTest < ActionController::TestCase fixtures :projects - + setup do @project = projects(:yard) end @@ -20,30 +20,30 @@ class ProjectsControllerTest < ActionController::TestCase test "should create project" do assert_difference('Project.count') do - post :create, project: { description: @project.description, name: @project.name } + post :create, wrapped_params(project: { description: @project.description, name: @project.name }) end assert_redirected_to project_path(assigns(:project)) end test "should show project" do - get :show, id: @project + get :show, wrapped_params(id: @project) assert_response :success end test "should get edit" do - get :edit, id: @project + get :edit, wrapped_params(id: @project) assert_response :success end test "should update project" do - patch :update, id: @project, project: { description: @project.description, name: @project.name } + patch :update, wrapped_params(id: @project, project: { description: @project.description, name: @project.name }) assert_redirected_to project_path(assigns(:project)) end test "should destroy project" do assert_difference('Project.count', -1) do - delete :destroy, id: @project + delete :destroy, wrapped_params(id: @project) end assert_redirected_to projects_path diff --git a/test/dummy/test/controllers/songs_controller_test.rb b/test/dummy/test/controllers/songs_controller_test.rb index 3e04a8c..11bd96f 100644 --- a/test/dummy/test/controllers/songs_controller_test.rb +++ b/test/dummy/test/controllers/songs_controller_test.rb @@ -20,19 +20,22 @@ class SongsControllerTest < ActionController::TestCase test "should create song" do assert_difference(['Song.count', 'Artist.count', 'Producer.count']) do - post :create, song: { - title: "Diamonds", - length: "360", - - artist_attributes: { - name: "Karras", - - producer_attributes: { - name: "Phoebos", - studio: "MADog" + params = { + song: { + title: "Diamonds", + length: "360", + + artist_attributes: { + name: "Karras", + + producer_attributes: { + name: "Phoebos", + studio: "MADog" + } } } } + post :create, wrapped_params(params) end song_form = assigns(:song_form) @@ -56,19 +59,22 @@ class SongsControllerTest < ActionController::TestCase test "should not create song with invalid params" do assert_difference(['Song.count', 'Artist.count', 'Producer.count'], 0) do - post :create, song: { - title: nil, - length: nil, - - artist_attributes: { - name: nil, + params = { + song: { + title: nil, + length: nil, - producer_attributes: { + artist_attributes: { name: nil, - studio: nil + + producer_attributes: { + name: nil, + studio: nil + } } } } + post :create, wrapped_params(params) end song_form = assigns(:song_form) @@ -89,30 +95,33 @@ class SongsControllerTest < ActionController::TestCase end test "should show song" do - get :show, id: @song + get :show, wrapped_params(id: @song) assert_response :success end test "should get edit" do - get :edit, id: @song + get :edit, wrapped_params(id: @song) assert_response :success end test "should update song" do assert_difference(['Song.count', 'Artist.count', 'Producer.count'], 0) do - patch :update, id: @song, song: { - title: "Run this town", - length: "355", - - artist_attributes: { - name: "Rihanna", - - producer_attributes: { - name: "Eminem", - studio: "Marshall" + params = { + id: @song, song: { + title: "Run this town", + length: "355", + + artist_attributes: { + name: "Rihanna", + + producer_attributes: { + name: "Eminem", + studio: "Marshall" + } } } } + patch :update, wrapped_params(params) end song_form = assigns(:song_form) @@ -132,7 +141,7 @@ class SongsControllerTest < ActionController::TestCase test "should destroy song" do assert_difference('Song.count', -1) do - delete :destroy, id: @song + delete :destroy, wrapped_params(id: @song) end assert_redirected_to songs_path diff --git a/test/dummy/test/controllers/surveys_controller_test.rb b/test/dummy/test/controllers/surveys_controller_test.rb index fa1bfcc..c02a77d 100644 --- a/test/dummy/test/controllers/surveys_controller_test.rb +++ b/test/dummy/test/controllers/surveys_controller_test.rb @@ -2,7 +2,7 @@ class SurveysControllerTest < ActionController::TestCase fixtures :surveys, :questions, :answers - + setup do @survey = surveys(:programming) end @@ -20,31 +20,34 @@ class SurveysControllerTest < ActionController::TestCase test "should create survey" do assert_difference('Survey.count') do - post :create, survey: { - name: "Programming languages", - - questions_attributes: { - "0" => { - content: "Which language allows closures?", - - answers_attributes: { - "0" => { content: "Ruby Programming Language" }, - "1" => { content: "CSharp Programming Language" }, + params = { + survey: { + name: "Programming languages", + + questions_attributes: { + "0" => { + content: "Which language allows closures?", + + answers_attributes: { + "0" => { content: "Ruby Programming Language" }, + "1" => { content: "CSharp Programming Language" }, + } } } } } + post :create, wrapped_params(params) end survey_form = assigns(:survey_form) assert survey_form.valid? assert_redirected_to survey_path(assigns(:survey_form)) - + assert_equal "Programming languages", survey_form.name - + assert_equal "Which language allows closures?", survey_form.questions[0].content - + assert_equal "Ruby Programming Language", survey_form.questions[0].answers[0].content assert_equal "CSharp Programming Language", survey_form.questions[0].answers[1].content @@ -55,48 +58,51 @@ class SurveysControllerTest < ActionController::TestCase assert answer.persisted? end end - + assert_equal "Survey: #{survey_form.name} was successfully created.", flash[:notice] end test "should create dynamically added question to a survey" do assert_difference('Survey.count') do - post :create, survey: { - name: "Programming languages", - - questions_attributes: { - "0" => { - content: "Which language allows closures?", - - answers_attributes: { - "0" => { content: "Ruby Programming Language" }, - "1" => { content: "CSharp Programming Language" }, - } - }, - - "12343" => { - content: "Which language allows objects?", - - answers_attributes: { - "0" => { content: "Ruby Programming Language" }, - "1" => { content: "C Programming Language" }, + params = { + survey: { + name: "Programming languages", + + questions_attributes: { + "0" => { + content: "Which language allows closures?", + + answers_attributes: { + "0" => { content: "Ruby Programming Language" }, + "1" => { content: "CSharp Programming Language" }, + } + }, + + "12343" => { + content: "Which language allows objects?", + + answers_attributes: { + "0" => { content: "Ruby Programming Language" }, + "1" => { content: "C Programming Language" }, + } } } } } + post :create, wrapped_params(params) end survey_form = assigns(:survey_form) assert survey_form.valid? assert_redirected_to survey_path(assigns(:survey_form)) - + assert_equal "Programming languages", survey_form.name assert_equal 2, survey_form.questions.size - + assert_equal "Which language allows closures?", survey_form.questions[0].content - + assert_equal "Ruby Programming Language", survey_form.questions[0].answers[0].content assert_equal "CSharp Programming Language", survey_form.questions[0].answers[1].content @@ -112,40 +118,43 @@ class SurveysControllerTest < ActionController::TestCase assert answer.persisted? end end - + assert_equal "Survey: #{survey_form.name} was successfully created.", flash[:notice] end test "should create dynamically added answer to a question" do assert_difference('Survey.count') do - post :create, survey: { - name: "Programming languages", - - questions_attributes: { - "0" => { - content: "Which language allows closures?", - - answers_attributes: { - "0" => { content: "Ruby Programming Language" }, - "1" => { content: "CSharp Programming Language" }, - "12322" => { content: "C Programming Language" } + params = { + survey: { + name: "Programming languages", + + questions_attributes: { + "0" => { + content: "Which language allows closures?", + + answers_attributes: { + "0" => { content: "Ruby Programming Language" }, + "1" => { content: "CSharp Programming Language" }, + "12322" => { content: "C Programming Language" } + } } } } } + post :create, wrapped_params(params) end survey_form = assigns(:survey_form) assert survey_form.valid? assert_redirected_to survey_path(assigns(:survey_form)) - + assert_equal "Programming languages", survey_form.name - + assert_equal "Which language allows closures?", survey_form.questions[0].content assert_equal 3, survey_form.questions[0].answers.size - + assert_equal "Ruby Programming Language", survey_form.questions[0].answers[0].content assert_equal "CSharp Programming Language", survey_form.questions[0].answers[1].content assert_equal "C Programming Language", survey_form.questions[0].answers[2].content @@ -157,32 +166,35 @@ class SurveysControllerTest < ActionController::TestCase assert answer.persisted? end end - + assert_equal "Survey: #{survey_form.name} was successfully created.", flash[:notice] end test "should not create survey with invalid params" do assert_difference('Survey.count', 0) do - post :create, survey: { - name: surveys(:programming).name, - - questions_attributes: { - "0" => { - content: nil, - - answers_attributes: { - "0" => { content: "Ruby Programming Language" }, - "1" => { content: nil }, + params = { + survey: { + name: surveys(:programming).name, + + questions_attributes: { + "0" => { + content: nil, + + answers_attributes: { + "0" => { content: "Ruby Programming Language" }, + "1" => { content: nil }, + } } } } } + post :create, wrapped_params(params) end survey_form = assigns(:survey_form) assert_not survey_form.valid? - + assert_includes survey_form.errors.messages[:name], "has already been taken" assert_includes survey_form.questions[0].errors.messages[:content], "can't be blank" @@ -191,45 +203,48 @@ class SurveysControllerTest < ActionController::TestCase end test "should show survey" do - get :show, id: @survey + get :show, wrapped_params(id: @survey) assert_response :success end test "should get edit" do - get :edit, id: @survey + get :edit, wrapped_params(id: @survey) assert_response :success end test "should update survey" do assert_difference('Survey.count', 0) do - patch :update, id: @survey, survey: { - name: "Native languages", - - questions_attributes: { - "0" => { - content: "Which language is spoken in England?", - id: questions(:one).id, - - answers_attributes: { - "0" => { content: "The English Language", id: answers(:ruby).id }, - "1" => { content: "The Latin Language", id: answers(:cs).id }, - } - }, + params = { + id: @survey, survey: { + name: "Native languages", + + questions_attributes: { + "0" => { + content: "Which language is spoken in England?", + id: questions(:one).id, + + answers_attributes: { + "0" => { content: "The English Language", id: answers(:ruby).id }, + "1" => { content: "The Latin Language", id: answers(:cs).id }, + } + }, + } } } + patch :update, wrapped_params(params) end survey_form = assigns(:survey_form) assert_redirected_to survey_path(survey_form) - + assert_equal "Native languages", survey_form.name - + assert_equal "Which language is spoken in England?", survey_form.questions[0].content - + assert_equal "The Latin Language", survey_form.questions[0].answers[0].content assert_equal "The English Language", survey_form.questions[0].answers[1].content - + assert_equal "Survey: #{survey_form.name} was successfully updated.", flash[:notice] end @@ -244,47 +259,50 @@ class SurveysControllerTest < ActionController::TestCase survey.save assert_difference('Survey.count', 0) do - patch :update, id: survey, survey: { - name: "Native languages", - - questions_attributes: { - "0" => { - content: "Which language is spoken in England?", - id: survey.questions[0].id, - - answers_attributes: { - "0" => { content: "The English Language", id: survey.questions[0].answers[0].id }, - "1" => { content: "The Latin Language", id: survey.questions[0].answers[1].id }, - } - }, - - "1" => { - content: "Which language is spoken in America?", - id: survey.questions[1].id, - _destroy: "1", - - answers_attributes: { - "0" => { content: "The English Language", id: survey.questions[1].answers[0].id }, - "1" => { content: "The American Language", id: survey.questions[1].answers[1].id }, + params = { + id: survey, survey: { + name: "Native languages", + + questions_attributes: { + "0" => { + content: "Which language is spoken in England?", + id: survey.questions[0].id, + + answers_attributes: { + "0" => { content: "The English Language", id: survey.questions[0].answers[0].id }, + "1" => { content: "The Latin Language", id: survey.questions[0].answers[1].id }, + } + }, + + "1" => { + content: "Which language is spoken in America?", + id: survey.questions[1].id, + _destroy: "1", + + answers_attributes: { + "0" => { content: "The English Language", id: survey.questions[1].answers[0].id }, + "1" => { content: "The American Language", id: survey.questions[1].answers[1].id }, + } } } } } + patch :update, wrapped_params(params) end survey_form = assigns(:survey_form) assert_redirected_to survey_path(survey_form) - + assert_equal "Native languages", survey_form.name - + assert_equal "Which language is spoken in England?", survey_form.questions[0].content assert_equal 1, survey_form.questions.size - + assert_equal "The English Language", survey_form.questions[0].answers[0].content assert_equal "The Latin Language", survey_form.questions[0].answers[1].content - + assert_equal "Survey: #{survey_form.name} was successfully updated.", flash[:notice] end @@ -301,63 +319,66 @@ class SurveysControllerTest < ActionController::TestCase survey.save assert_difference('Survey.count', 0) do - patch :update, id: survey, survey: { - name: "Native languages", - - questions_attributes: { - "0" => { - content: "Which language is spoken in England?", - id: survey.questions[0].id, - - answers_attributes: { - "0" => { content: "The English Language", id: survey.questions[0].answers[0].id }, - "1" => { content: "The Latin Language", id: survey.questions[0].answers[1].id }, - "2" => { content: "The French Language", id: survey.questions[0].answers[2].id, _destroy: "1" } - } - }, - - "1" => { - content: "Which language is spoken in America?", - id: survey.questions[1].id, - - answers_attributes: { - "0" => { content: "The English Language", id: survey.questions[1].answers[0].id }, - "1" => { content: "The American Language", id: survey.questions[1].answers[1].id }, - "2" => { content: "The Italian Language", id: survey.questions[1].answers[2].id, _destroy: "1" } + params = { + id: survey, survey: { + name: "Native languages", + + questions_attributes: { + "0" => { + content: "Which language is spoken in England?", + id: survey.questions[0].id, + + answers_attributes: { + "0" => { content: "The English Language", id: survey.questions[0].answers[0].id }, + "1" => { content: "The Latin Language", id: survey.questions[0].answers[1].id }, + "2" => { content: "The French Language", id: survey.questions[0].answers[2].id, _destroy: "1" } + } + }, + + "1" => { + content: "Which language is spoken in America?", + id: survey.questions[1].id, + + answers_attributes: { + "0" => { content: "The English Language", id: survey.questions[1].answers[0].id }, + "1" => { content: "The American Language", id: survey.questions[1].answers[1].id }, + "2" => { content: "The Italian Language", id: survey.questions[1].answers[2].id, _destroy: "1" } + } } } } } + patch :update, wrapped_params(params) end survey_form = assigns(:survey_form) assert_redirected_to survey_path(survey_form) - + assert_equal "Native languages", survey_form.name assert_equal 2, survey_form.questions.size - + assert_equal "Which language is spoken in England?", survey_form.questions[0].content assert_equal 2, survey_form.questions[0].answers.size - + assert_equal "The English Language", survey_form.questions[0].answers[0].content assert_equal "The Latin Language", survey_form.questions[0].answers[1].content assert_equal "Which language is spoken in America?", survey_form.questions[1].content assert_equal 2, survey_form.questions[1].answers.size - + assert_equal "The English Language", survey_form.questions[1].answers[0].content assert_equal "The American Language", survey_form.questions[1].answers[1].content - + assert_equal "Survey: #{survey_form.name} was successfully updated.", flash[:notice] end test "should destroy survey" do assert_difference('Survey.count', -1) do - delete :destroy, id: @survey + delete :destroy, wrapped_params(id: @survey) end assert_redirected_to surveys_path diff --git a/test/dummy/test/controllers/users_controller_test.rb b/test/dummy/test/controllers/users_controller_test.rb index 4550e96..0b1c694 100644 --- a/test/dummy/test/controllers/users_controller_test.rb +++ b/test/dummy/test/controllers/users_controller_test.rb @@ -2,7 +2,7 @@ class UsersControllerTest < ActionController::TestCase fixtures :users, :emails, :profiles - + setup do @user = users(:peter) end @@ -20,33 +20,36 @@ class UsersControllerTest < ActionController::TestCase test "should create user" do assert_difference(['User.count', 'Email.count', 'Profile.count']) do - post :create, user: { - age: "23", - gender: "0", - name: "petrakos", - - email_attributes: { - address: "petrakos@gmail.com" - }, - - profile_attributes: { - twitter_name: "t_peter", - github_name: "g_peter" + params = { + user: { + age: "23", + gender: "0", + name: "petrakos", + + email_attributes: { + address: "petrakos@gmail.com" + }, + + profile_attributes: { + twitter_name: "t_peter", + github_name: "g_peter" + } } } + post :create, wrapped_params(params) end user_form = assigns(:user_form) assert user_form.valid? assert_redirected_to user_path(user_form) - + assert_equal "petrakos", user_form.name assert_equal 23, user_form.age assert_equal 0, user_form.gender - + assert_equal "petrakos@gmail.com", user_form.email.address - + assert_equal "t_peter", user_form.profile.twitter_name assert_equal "g_peter", user_form.profile.github_name @@ -57,26 +60,29 @@ class UsersControllerTest < ActionController::TestCase peter = users(:peter) assert_difference(['User.count', 'Email.count', 'Profile.count'], 0) do - post :create, user: { - name: peter.name, - age: nil, - gender: "0", - - email_attributes: { - address: peter.email.address - }, - - profile_attributes: { - twitter_name: peter.profile.twitter_name, - github_name: peter.profile.github_name + params = { + user: { + name: peter.name, + age: nil, + gender: "0", + + email_attributes: { + address: peter.email.address + }, + + profile_attributes: { + twitter_name: peter.profile.twitter_name, + github_name: peter.profile.github_name + } } } + post :create, wrapped_params(params) end user_form = assigns(:user_form) assert_not user_form.valid? - + assert_includes user_form.errors.messages[:name], "has already been taken" assert_includes user_form.errors.messages[:age], "can't be blank" @@ -87,50 +93,53 @@ class UsersControllerTest < ActionController::TestCase end test "should show user" do - get :show, id: @user + get :show, wrapped_params(id: @user) assert_response :success end test "should get edit" do - get :edit, id: @user + get :edit, wrapped_params(id: @user) assert_response :success end test "should update user" do assert_difference(['User.count', 'Email.count', 'Profile.count'], 0) do - patch :update, id: @user, user: { - age: @user.age, - gender: @user.gender, - name: "petrakos", - - email_attributes: { - address: "petrakos@gmail.com" - }, - - profile_attributes: { - twitter_name: "t_peter", - github_name: "g_peter" + params = { + id: @user, user: { + age: @user.age, + gender: @user.gender, + name: "petrakos", + + email_attributes: { + address: "petrakos@gmail.com" + }, + + profile_attributes: { + twitter_name: "t_peter", + github_name: "g_peter" + } } } + patch :update, wrapped_params(params) end user_form = assigns(:user_form) assert_redirected_to user_path(user_form) - + assert_equal "petrakos", user_form.name - + assert_equal "petrakos@gmail.com", user_form.email.address - + assert_equal "t_peter", user_form.profile.twitter_name assert_equal "g_peter", user_form.profile.github_name - + assert_equal "User: #{user_form.name} was successfully updated.", flash[:notice] end test "should destroy user" do assert_difference('User.count', -1) do - delete :destroy, id: @user + delete :destroy, wrapped_params(id: @user) end assert_redirected_to users_path diff --git a/test/dummy/test/fixtures/user_form_fixture.rb b/test/dummy/test/fixtures/user_form_fixture.rb new file mode 100644 index 0000000..740e7dc --- /dev/null +++ b/test/dummy/test/fixtures/user_form_fixture.rb @@ -0,0 +1,8 @@ +class UserFormFixture < ActionForm::Base + self.main_model = :user + + attributes :name, :age, :gender, required: true + + validates :name, length: { in: 6..20 } + validates :age, numericality: { only_integer: true } +end \ No newline at end of file diff --git a/test/dummy/test/fixtures/user_with_email_form_fixture.rb b/test/dummy/test/fixtures/user_with_email_form_fixture.rb new file mode 100644 index 0000000..b3bbd1a --- /dev/null +++ b/test/dummy/test/fixtures/user_with_email_form_fixture.rb @@ -0,0 +1,11 @@ +class UserWithEmailFormFixture < ActionForm::Base + self.main_model = :user + attributes :name, :age, :gender, required: true + + association :email do + attribute :address, required: true + end + + validates :name, length: { in: 6..20 } + validates :age, numericality: { only_integer: true } +end \ No newline at end of file diff --git a/test/forms/nested_model_rendering_test.rb b/test/dummy/test/forms/nested_model_rendering_test2.rb similarity index 66% rename from test/forms/nested_model_rendering_test.rb rename to test/dummy/test/forms/nested_model_rendering_test2.rb index f5c01dc..c85789c 100644 --- a/test/forms/nested_model_rendering_test.rb +++ b/test/dummy/test/forms/nested_model_rendering_test2.rb @@ -32,16 +32,19 @@ def form_for(*) assert_match(/method="post"/, output_buffer) assert_match(/