Skip to content
This repository has been archived by the owner on Feb 26, 2019. It is now read-only.

Commit

Permalink
Merge pull request #5 from MITLibraries/workflow_element_views
Browse files Browse the repository at this point in the history
Initial workflow pages
  • Loading branch information
JPrevost committed Oct 22, 2015
2 parents 23f5db0 + 0b2fb79 commit 35a3faa
Show file tree
Hide file tree
Showing 32 changed files with 303 additions and 68 deletions.
2 changes: 2 additions & 0 deletions .hound.yml
@@ -0,0 +1,2 @@
StringLiterals:
EnforcedStyle: single_quotes
1 change: 1 addition & 0 deletions .ruby-version
@@ -0,0 +1 @@
2.2.3
5 changes: 5 additions & 0 deletions Gemfile
Expand Up @@ -9,6 +9,7 @@ gem 'jbuilder', '~> 2.0'
gem 'jquery-rails'
gem 'less-rails'
gem 'minitest-reporters'
gem 'minitest-rails'
gem 'omniauth-mit-oauth2'
gem 'rails', '4.2.4'
gem 'sass-rails', '~> 5.0'
Expand All @@ -26,3 +27,7 @@ end
group :development do
gem 'web-console', '~> 2.0'
end

group :test do
gem 'minitest-rails-capybara'
end
24 changes: 24 additions & 0 deletions Gemfile.lock
Expand Up @@ -48,6 +48,12 @@ GEM
debug_inspector (>= 0.0.1)
builder (3.2.2)
byebug (6.0.2)
capybara (2.5.0)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
coffee-rails (4.1.0)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
Expand Down Expand Up @@ -108,6 +114,20 @@ GEM
mime-types (2.6.2)
mini_portile (0.6.2)
minitest (5.8.1)
minitest-capybara (0.7.2)
capybara (~> 2.2)
minitest (~> 5.0)
rake
minitest-metadata (0.5.3)
minitest (>= 4.7, < 6.0)
minitest-rails (2.2.0)
minitest (~> 5.7)
railties (~> 4.1)
minitest-rails-capybara (2.1.1)
capybara (~> 2.0)
minitest-capybara (~> 0.7.0)
minitest-metadata (~> 0.5.0)
minitest-rails (~> 2.1)
minitest-reporters (1.1.4)
ansi
builder
Expand Down Expand Up @@ -224,6 +244,8 @@ GEM
binding_of_caller (>= 0.7.2)
railties (>= 4.0)
sprockets-rails (>= 2.0, < 4.0)
xpath (2.0.0)
nokogiri (~> 1.3)

PLATFORMS
ruby
Expand All @@ -238,6 +260,8 @@ DEPENDENCIES
jbuilder (~> 2.0)
jquery-rails
less-rails
minitest-rails
minitest-rails-capybara
minitest-reporters
omniauth-mit-oauth2
rails (= 4.2.4)
Expand Down
25 changes: 25 additions & 0 deletions app/controllers/workflows_controller.rb
@@ -0,0 +1,25 @@
class WorkflowsController < ApplicationController
def show
@workflow = Workflow.find(params[:id])
end

def form_submit
@workflow = Workflow.find(params[:id])
@form_errors = error_check(@workflow, params)

if @form_errors.empty?
flash[:success] = 'Submission was valid.'
redirect_to root_path
else
flash[:danger] = 'There was a problem with the form submission.'
render 'show'
end
end

private

def error_check(wf, params)
required_fields = wf.elements.map { |e| e.display_id if e.required }.compact
required_fields.map { |field| field unless params[field].present? }.compact
end
end
11 changes: 11 additions & 0 deletions app/models/element.rb
Expand Up @@ -40,14 +40,25 @@ def accepted_element_types
ElementType.where(id: accepted_element_type_ids).order(:name)
end

def display_id
"#{element_type.name}_#{id}"
end

def display_label
return label unless required
"* #{label}"
end

def legal_parent
return unless parent.present?
return if parent.accepts_type?(element_type)
errors.add(:element_type_id, "You can't add this Element here")
end

def traverse(&block)
yield self, '_open'
yield self
children.map { |child| child.traverse(&block) }
yield self, '_close'
end
end
Empty file.
1 change: 1 addition & 0 deletions app/views/workflows/elements/_fieldset_close.html.erb
@@ -0,0 +1 @@
</fieldset>
4 changes: 4 additions & 0 deletions app/views/workflows/elements/_fieldset_open.html.erb
@@ -0,0 +1,4 @@
<fieldset>
<% if element.label %>
<legend><%= element.label %></legend>
<% end %>
Empty file.
1 change: 1 addition & 0 deletions app/views/workflows/elements/_form_close.html.erb
@@ -0,0 +1 @@
</form>
3 changes: 3 additions & 0 deletions app/views/workflows/elements/_form_open.html.erb
@@ -0,0 +1,3 @@
<form class="form-horizontal" action="<%= workflow_submit_path(@workflow.id) %>" method="post">
<input name="authenticity_token" type="hidden" value="#{session[:_csrf_token]}" />
<span id="helpBlock" class="help-block">* indicates required field.</span>
5 changes: 5 additions & 0 deletions app/views/workflows/elements/_submit.html.erb
@@ -0,0 +1,5 @@
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary"><%= element.label %></button>
</div>
</div>
Empty file.
Empty file.
16 changes: 16 additions & 0 deletions app/views/workflows/elements/_text.html.erb
@@ -0,0 +1,16 @@
<% on_error = true if @form_errors && @form_errors.include?(element.display_id) %>

<div class="form-group <%= 'has-error' if on_error %>">
<label for="<%= element.display_id %>" class="col-sm-2 control-label"><%= element.display_label %></label>
<div class="col-sm-10">
<input type="text" class="form-control" id="<%= element.display_id %>"
name="<%= element.display_id %>" placeholder="<%= element.label %>"
value="<%= params[element.display_id] %>">
<% if element.help %>
<span id="helpBlock" class="help-block"><%= element.help %></span>
<% end %>
<% if on_error %>
<span class='help-block'><%= element.error %></span>
<% end %>
</div>
</div>
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions app/views/workflows/show.html.erb
@@ -0,0 +1,6 @@
<h1><%= @workflow.name %></h1>

<% @workflow.elements.first.traverse do |element, partial| %>
<%= render partial: "workflows/elements/#{element.element_type.name}#{partial}",
locals: { element: element } %>
<% end %>
4 changes: 4 additions & 0 deletions config/routes.rb
Expand Up @@ -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
44 changes: 37 additions & 7 deletions 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)
8 changes: 8 additions & 0 deletions 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
33 changes: 33 additions & 0 deletions 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
46 changes: 46 additions & 0 deletions 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
20 changes: 12 additions & 8 deletions test/fixtures/element_type_accepts.yml
Expand Up @@ -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) %>
38 changes: 16 additions & 22 deletions test/fixtures/element_types.yml
Expand Up @@ -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'

0 comments on commit 35a3faa

Please sign in to comment.