From 0b2fb798edff295111e57c4d8e4c77d232669cd2 Mon Sep 17 00:00:00 2001 From: Jeremy Prevost Date: Wed, 21 Oct 2015 16:50:47 -0400 Subject: [PATCH] Initial workflow pages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This initial commit includes view support for the following form elements: -
- diff --git a/app/views/workflows/elements/_fieldset_open.html.erb b/app/views/workflows/elements/_fieldset_open.html.erb new file mode 100644 index 0000000..189f488 --- /dev/null +++ b/app/views/workflows/elements/_fieldset_open.html.erb @@ -0,0 +1,4 @@ +
+<% if element.label %> + <%= element.label %> +<% end %> diff --git a/app/views/workflows/elements/_form.html.erb b/app/views/workflows/elements/_form.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/app/views/workflows/elements/_form_close.html.erb b/app/views/workflows/elements/_form_close.html.erb new file mode 100644 index 0000000..5582354 --- /dev/null +++ b/app/views/workflows/elements/_form_close.html.erb @@ -0,0 +1 @@ + diff --git a/app/views/workflows/elements/_form_open.html.erb b/app/views/workflows/elements/_form_open.html.erb new file mode 100644 index 0000000..db60d98 --- /dev/null +++ b/app/views/workflows/elements/_form_open.html.erb @@ -0,0 +1,3 @@ +
+ + * indicates required field. diff --git a/app/views/workflows/elements/_submit.html.erb b/app/views/workflows/elements/_submit.html.erb new file mode 100644 index 0000000..def2258 --- /dev/null +++ b/app/views/workflows/elements/_submit.html.erb @@ -0,0 +1,5 @@ +
+
+ +
+
diff --git a/app/views/workflows/elements/_submit_close.html.erb b/app/views/workflows/elements/_submit_close.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/app/views/workflows/elements/_submit_open.html.erb b/app/views/workflows/elements/_submit_open.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/app/views/workflows/elements/_text.html.erb b/app/views/workflows/elements/_text.html.erb new file mode 100644 index 0000000..766a8a6 --- /dev/null +++ b/app/views/workflows/elements/_text.html.erb @@ -0,0 +1,16 @@ +<% on_error = true if @form_errors && @form_errors.include?(element.display_id) %> + +
+ +
+ + <% if element.help %> + <%= element.help %> + <% end %> + <% if on_error %> + <%= element.error %> + <% end %> +
+
diff --git a/app/views/workflows/elements/_text_close.html.erb b/app/views/workflows/elements/_text_close.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/app/views/workflows/elements/_text_open.html.erb b/app/views/workflows/elements/_text_open.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/app/views/workflows/show.html.erb b/app/views/workflows/show.html.erb new file mode 100644 index 0000000..c7d69ee --- /dev/null +++ b/app/views/workflows/show.html.erb @@ -0,0 +1,6 @@ +

<%= @workflow.name %>

+ +<% @workflow.elements.first.traverse do |element, partial| %> + <%= render partial: "workflows/elements/#{element.element_type.name}#{partial}", + locals: { element: element } %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 13d3c62..95d4510 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,10 @@ delete 'sign_out', to: 'devise/sessions#destroy', as: :destroy_user_session end + resources :workflows, only: :show + post '/workflows/submit/:id', to: 'workflows#form_submit', + as: 'workflow_submit' + get 'static_pages/home' root 'static_pages#home' end diff --git a/db/seeds.rb b/db/seeds.rb index 4edb1e8..e84b0a6 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,7 +1,37 @@ -# This file should contain all the record creation needed to seed the database with its default values. -# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). -# -# Examples: -# -# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) -# Mayor.create(name: 'Emanuel', city: cities.first) +# Remove Existing ElementTypes +ElementType.delete_all + +# Setup ElementTypes +ElementType.create(id: 1, name: 'form') +ElementType.create(id: 2, name: 'fieldset') +ElementType.create(id: 3, name: 'text') +ElementType.create(id: 4, name: 'textarea') +ElementType.create(id: 5, name: 'select') +ElementType.create(id: 6, name: 'option') +ElementType.create(id: 7, name: 'checkbox') +ElementType.create(id: 8, name: 'radio_group') +ElementType.create(id: 9, name: 'radio') +ElementType.create(id: 10, name: 'submit') + +# Remove Existing ElementTypeAccepts +ElementTypeAccept.delete_all + +# Setup ElementTypeAccepts +ElementTypeAccept.create(element_type_id: 1, accepts_element_type_id: 2) +ElementTypeAccept.create(element_type_id: 1, accepts_element_type_id: 3) +ElementTypeAccept.create(element_type_id: 1, accepts_element_type_id: 4) +ElementTypeAccept.create(element_type_id: 1, accepts_element_type_id: 5) +ElementTypeAccept.create(element_type_id: 1, accepts_element_type_id: 7) +ElementTypeAccept.create(element_type_id: 1, accepts_element_type_id: 8) +ElementTypeAccept.create(element_type_id: 1, accepts_element_type_id: 10) + +ElementTypeAccept.create(element_type_id: 2, accepts_element_type_id: 2) +ElementTypeAccept.create(element_type_id: 2, accepts_element_type_id: 3) +ElementTypeAccept.create(element_type_id: 2, accepts_element_type_id: 4) +ElementTypeAccept.create(element_type_id: 2, accepts_element_type_id: 5) +ElementTypeAccept.create(element_type_id: 2, accepts_element_type_id: 7) +ElementTypeAccept.create(element_type_id: 2, accepts_element_type_id: 8) + +ElementTypeAccept.create(element_type_id: 5, accepts_element_type_id: 6) + +ElementTypeAccept.create(element_type_id: 8, accepts_element_type_id: 9) diff --git a/test/controllers/workflows_controller_test.rb b/test/controllers/workflows_controller_test.rb new file mode 100644 index 0000000..3eca3c2 --- /dev/null +++ b/test/controllers/workflows_controller_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class WorkflowsControllerTest < ActionController::TestCase + test 'show' do + get :show, id: workflows(:workflow_one) + assert_response :success + end +end diff --git a/test/features/staticpages_test.rb b/test/features/staticpages_test.rb new file mode 100644 index 0000000..98bec09 --- /dev/null +++ b/test/features/staticpages_test.rb @@ -0,0 +1,33 @@ +require 'test_helper' + +class StaticPagesTest < Capybara::Rails::TestCase + def setup + Rails.application.env_config['devise.mapping'] = Devise.mappings[:user] + Rails.application.env_config['omniauth.auth'] = + OmniAuth.config.mock_auth[:mit_oauth2] + OmniAuth.config.test_mode = true + end + + def teardown + OmniAuth.config.test_mode = false + OmniAuth.config.mock_auth[:mit_oauth2] = nil + end + + test 'sign in link' do + visit root_path + assert page.has_link?('Sign in') + refute page.has_link?('Sign out') + end + + test 'sign out link' do + OmniAuth.config.mock_auth[:mit_oauth2] = + OmniAuth::AuthHash.new(provider: 'mit_oauth2', + uid: '123545', + info: { email: 'bob@asdf.com' }) + + visit '/users/auth/mit_oauth2/callback' + visit root_path + assert page.has_link?('Sign out') + refute page.has_link?('Sign in') + end +end diff --git a/test/features/workflow_pages_test.rb b/test/features/workflow_pages_test.rb new file mode 100644 index 0000000..f1b1736 --- /dev/null +++ b/test/features/workflow_pages_test.rb @@ -0,0 +1,46 @@ +require 'test_helper' + +class WorkflowPagesTest < Capybara::Rails::TestCase + def setup + @workflow = workflows(:workflow_one) + parent = elements(:one) + fs = Element.create(workflow: @workflow, label: 'Contact Information', + element_type: element_types(:fieldset), parent: parent) + @input_name = Element.create(workflow: @workflow, label: 'Name', + element_type: element_types(:text), + parent: fs, required: true, + error: 'Your name is required.') + @input_email = Element.create(workflow: @workflow, label: 'Email', + element_type: element_types(:text), + parent: fs, required: false, + help: 'Add your email if you want.') + Element.create(workflow: @workflow, label: 'Submit!!!', + element_type: element_types(:submit), parent: parent) + end + + test 'can submit valid form' do + visit workflow_path(@workflow) + fill_in(@input_name.display_id, with: 'Example User') + click_on 'Submit!!!' + assert page.has_content?('Submission was valid.') + end + + test 'invalid form submission retains values' do + visit workflow_path(@workflow) + fill_in(@input_email.display_id, with: 'bob@example.com') + click_on 'Submit!!!' + assert_equal(find_field(@input_email.display_id).value, 'bob@example.com') + refute page.has_content?('Submission was valid.') + end + + test 'invalid form submission displays errors' do + visit workflow_path(@workflow) + page.assert_selector('div.has-error', count: 0) + fill_in(@input_email.display_id, with: 'bob@example.com') + click_on 'Submit!!!' + refute page.has_content?('Submission was valid.') + assert page.has_content?('There was a problem with the form submission.') + assert page.has_content?('Your name is required.') + page.assert_selector('div.has-error', count: 1) + end +end diff --git a/test/fixtures/element_type_accepts.yml b/test/fixtures/element_type_accepts.yml index 31c27b9..e0334f4 100644 --- a/test/fixtures/element_type_accepts.yml +++ b/test/fixtures/element_type_accepts.yml @@ -15,14 +15,18 @@ # model remove the '{}' from the fixture names and add the columns immediately # below each fixture, per the syntax in the comments below # -form_input_text: - element_type_id: 1 - accepts_element_type_id: 2 +form_text: + element_type_id: <%= ActiveRecord::FixtureSet.identify(:form) %> + accepts_element_type_id: <%= ActiveRecord::FixtureSet.identify(:text) %> + +form_submit: + element_type_id: <%= ActiveRecord::FixtureSet.identify(:form) %> + accepts_element_type_id: <%= ActiveRecord::FixtureSet.identify(:submit) %> form_fieldset: - element_type_id: 1 - accepts_element_type_id: 7 + element_type_id: <%= ActiveRecord::FixtureSet.identify(:form) %> + accepts_element_type_id: <%= ActiveRecord::FixtureSet.identify(:fieldset) %> -fieldset_input_text: - element_type_id: 7 - accepts_element_type_id: 2 +fieldset_text: + element_type_id: <%= ActiveRecord::FixtureSet.identify(:fieldset) %> + accepts_element_type_id: <%= ActiveRecord::FixtureSet.identify(:text) %> diff --git a/test/fixtures/element_types.yml b/test/fixtures/element_types.yml index 2ce2f68..f50ad7a 100644 --- a/test/fixtures/element_types.yml +++ b/test/fixtures/element_types.yml @@ -15,37 +15,31 @@ # below each fixture, per the syntax in the comments below # form: - id: 1 name: 'form' -input_text: - id: 2 - name: 'input_text' - -input_radio: - id: 3 - name: 'input_radio' - -input_checkbox: - id: 4 - name: 'input_checkbox' +fieldset: + name: 'fieldset' -input_submit: - id: 5 - name: 'input_submit' +text: + name: 'text' textarea: - id: 6 name: 'textarea' -fieldset: - id: 7 - name: 'fieldset' - select: - id: 8 name: 'select' option: - id: 9 name: 'option' + +checkbox: + name: 'checkbox' + +radio_group: + name: 'radio_group' + +radio: + name: 'radio' + +submit: + name: 'submit' diff --git a/test/fixtures/elements.yml b/test/fixtures/elements.yml index c50e77f..c14aa4f 100644 --- a/test/fixtures/elements.yml +++ b/test/fixtures/elements.yml @@ -22,5 +22,5 @@ # below each fixture, per the syntax in the comments below # one: - workflow_id: 1 - element_type_id: 1 + workflow: workflow_one + element_type: form diff --git a/test/fixtures/workflows.yml b/test/fixtures/workflows.yml index 18c5cc5..0d611b6 100644 --- a/test/fixtures/workflows.yml +++ b/test/fixtures/workflows.yml @@ -15,9 +15,7 @@ # below each fixture, per the syntax in the comments below # workflow_one: - id: 1 name: 'popcorn flow' two: - id: 2 name: 'doe' diff --git a/test/models/element_test.rb b/test/models/element_test.rb index 8b84af2..593e945 100644 --- a/test/models/element_test.rb +++ b/test/models/element_test.rb @@ -38,7 +38,7 @@ def setup end test 'accepted_element_type_ids' do - input_text_id = element_types(:input_text).id + input_text_id = element_types(:text).id fieldset_id = element_types(:fieldset).id form_id = element_types(:form).id textarea_id = element_types(:textarea).id @@ -49,14 +49,14 @@ def setup end test 'accepted_element_types' do - assert @element.accepted_element_types.include?(element_types(:input_text)) + assert @element.accepted_element_types.include?(element_types(:text)) assert @element.accepted_element_types.include?(element_types(:fieldset)) assert @element.accepted_element_types.exclude?(element_types(:form)) assert @element.accepted_element_types.exclude?(element_types(:textarea)) end test 'accepts_type' do - assert @element.accepts_type?(element_types(:input_text)) + assert @element.accepts_type?(element_types(:text)) assert @element.accepts_type?(element_types(:fieldset)) assert_not @element.accepts_type?(element_types(:form)) assert_not @element.accepts_type?(element_types(:textarea)) @@ -64,23 +64,25 @@ def setup test 'valid with legal_parent' do parent = elements(:one) - e = Element.new(workflow_id: 1, element_type_id: 2, parent_id: parent.id) + e = Element.new(workflow: workflows(:workflow_one), + element_type: element_types(:fieldset), parent: parent) assert e.valid? end test 'invalid with illegal_parent' do parent = elements(:one) - e = Element.new(workflow_id: 1, element_type_id: 3, parent_id: parent.id) + e = Element.new(workflow: workflows(:workflow_one), + element_type: element_types(:radio), parent: parent) assert e.invalid? end test 'can move up in order' do workflow = workflows(:workflow_one) parent = elements(:one) - e1 = Element.create(workflow_id: workflow.id, label: 'e1', - element_type_id: 2, parent_id: parent.id) - e2 = Element.create(workflow_id: workflow.id, label: 'e2', - element_type_id: 2, parent_id: parent.id) + e1 = Element.create(workflow: workflow, label: 'e1', + element_type: element_types(:text), parent: parent) + e2 = Element.create(workflow: workflow, label: 'e2', + element_type: element_types(:text), parent: parent) assert_equal(1, e1.position) assert_equal(2, e2.position) assert_equal(e1, parent.children.first) @@ -97,21 +99,32 @@ def setup top_form = elements(:one) top_form.label = 'form' top_form.save! - Element.create(workflow_id: workflow.id, label: 'input_text', - element_type_id: 2, parent_id: top_form.id) - fs1 = Element.create(workflow_id: workflow.id, label: 'fieldset_one', - element_type_id: 7, parent_id: top_form.id) - Element.create(workflow_id: workflow.id, label: 'fieldset_two', - element_type_id: 7, parent_id: top_form.id) - Element.create(workflow_id: workflow.id, label: 'input_text2', - element_type_id: 2, parent_id: fs1.id) + Element.create(workflow: workflow, label: 'input_text', + element_type: element_types(:text), parent: top_form) + fs1 = Element.create(workflow: workflow, label: 'fieldset_one', + element_type: element_types(:fieldset), + parent: top_form) + Element.create(workflow: workflow, label: 'fieldset_two', + element_type: element_types(:fieldset), parent: top_form) + Element.create(workflow: workflow, label: 'input_text2', + element_type: element_types(:text), parent: fs1) a = [] top_form.traverse { |e, f| a << "#{e.label}#{f}" } - assert_equal(5, a.size) - assert_equal('form', a[0]) - assert_equal('input_text', a[1]) - assert_equal('fieldset_one', a[2]) - assert_equal('input_text2', a[3]) - assert_equal('fieldset_two', a[4]) + assert_equal(15, a.size) + assert_equal('form_open', a[0]) + assert_equal('form', a[1]) + assert_equal('input_text_open', a[2]) + assert_equal('input_text', a[3]) + assert_equal('input_text_close', a[4]) + assert_equal('fieldset_one_open', a[5]) + assert_equal('fieldset_one', a[6]) + assert_equal('input_text2_open', a[7]) + assert_equal('input_text2', a[8]) + assert_equal('input_text2_close', a[9]) + assert_equal('fieldset_one_close', a[10]) + assert_equal('fieldset_two_open', a[11]) + assert_equal('fieldset_two', a[12]) + assert_equal('fieldset_two_close', a[13]) + assert_equal('form_close', a[14]) end end diff --git a/test/models/element_type_accept_test.rb b/test/models/element_type_accept_test.rb index 2493d74..79045c5 100644 --- a/test/models/element_type_accept_test.rb +++ b/test/models/element_type_accept_test.rb @@ -13,7 +13,7 @@ class ElementTypeAcceptTest < ActiveSupport::TestCase def setup - @element_type_accepts = element_type_accepts(:form_input_text) + @element_type_accepts = element_type_accepts(:form_text) end test 'is valid' do @@ -32,11 +32,11 @@ def setup test 'element type can be nested if allowed' do @element_type_one = element_types(:form) - @element_type_two = element_types(:input_text) + @element_type_two = element_types(:text) end test 'element type cannot be nested if not allowed' do @element_type_one = element_types(:form) - @element_type_two = element_types(:input_text) + @element_type_two = element_types(:text) end end diff --git a/test/models/element_type_test.rb b/test/models/element_type_test.rb index 5e74a5e..1fdee45 100644 --- a/test/models/element_type_test.rb +++ b/test/models/element_type_test.rb @@ -16,7 +16,7 @@ def setup end test 'element_type_count' do - assert_equal(9, ElementType.count) + assert_equal(10, ElementType.count) end test 'is valid' do diff --git a/test/test_helper.rb b/test/test_helper.rb index c31d6eb..fa0e1c0 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -3,6 +3,7 @@ ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' +require 'minitest/rails/capybara' require 'minitest/reporters' Minitest::Reporters.use!