Skip to content

Commit

Permalink
Merge pull request #85 from bmic-development/wth-form-functionality-v…
Browse files Browse the repository at this point in the history
…alidations-part-two

wth - (SPARCCatalog) Form Functionality Form Builder Part II
  • Loading branch information
Stuart-Johnson committed Feb 15, 2017
2 parents 692e94e + e1b095c commit 8a13425
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 5 deletions.
8 changes: 8 additions & 0 deletions app/assets/javascripts/additional_details/item_options.coffee
Expand Up @@ -8,17 +8,25 @@ $ ->
itemId = $(this).data('item-form-id')
if showOptions($(this).val(), needOptions)
$(".item-options[data-item-form-id=#{itemId}]").removeClass('hidden')
$.each $(".set-validate-content[data-item-form-id=#{itemId}]"), (key, value) ->
$(value).val('true')
else
$(".item-options[data-item-form-id=#{itemId}]").addClass('hidden')
$.each $(".set-validate-content[data-item-form-id=#{itemId}]"), (key, value) ->
$(value).val('false')

$(document).on 'fields_added.nested_form_fields', (event, param) ->
$('.select-type').trigger('change')
$('.select-type').on 'change', ->
itemId = $(this).data('item-form-id')
if showOptions($(this).val(), needOptions)
$(".item-options[data-item-form-id=#{itemId}]").removeClass('hidden')
else
$(".item-options[data-item-form-id=#{itemId}]").addClass('hidden')

$(document).on 'fields_removed.nested_form_fields', (event, param) ->
$('.select-type').trigger('change')


$.each $('.select-type :selected'), (key, value) ->
itemFormId = $(value).parent().data('item-form-id')
Expand Down
2 changes: 2 additions & 0 deletions app/models/item_option.rb
@@ -1,3 +1,5 @@
class ItemOption < ActiveRecord::Base
belongs_to :item

validates :content, presence: true, if: :validate_content?
end
3 changes: 3 additions & 0 deletions app/views/additional_details/questionnaires/_form.html.haml
Expand Up @@ -31,6 +31,9 @@
%hr
- i.object.item_options.build unless i.object.item_options.any?
= i.nested_fields_for :item_options, html: { class: 'form-horizontal' } do |io|
= io.hidden_field :validate_content, value: false,
class: 'set-validate-content',
data: { item_form_id: '__nested_field_for_replace_with_index__' }
.col-sm-offset-2
.form-group
= io.label :content, class: 'col-sm-2 control-label'
Expand Down
4 changes: 4 additions & 0 deletions config/locales/en.yml
Expand Up @@ -56,6 +56,10 @@ en:
attributes:
name:
blank: "Name can't be blank"
item_option:
attributes:
content:
blank: "Content can't be blank"
attributes:
arm:
name: "Arm Name"
Expand Down
@@ -0,0 +1,5 @@
class AddValidateContentToItemOptions < ActiveRecord::Migration
def change
add_column :item_options, :validate_content, :boolean, after: :content
end
end
11 changes: 6 additions & 5 deletions db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170209152118) do
ActiveRecord::Schema.define(version: 20170213212738) do

create_table "admin_rates", force: :cascade do |t|
t.integer "line_item_id", limit: 4
Expand Down Expand Up @@ -427,10 +427,11 @@
add_index "ip_patents_info", ["protocol_id"], name: "index_ip_patents_info_on_protocol_id", using: :btree

create_table "item_options", force: :cascade do |t|
t.string "content", limit: 255
t.integer "item_id", limit: 4
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "content", limit: 255
t.boolean "validate_content"
t.integer "item_id", limit: 4
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "item_options", ["item_id"], name: "index_item_options_on_item_id", using: :btree
Expand Down
7 changes: 7 additions & 0 deletions spec/factories/item_option.rb
@@ -0,0 +1,7 @@
FactoryGirl.define do
factory :item_option do
content 'text'
validate_content true
item_id nil
end
end
@@ -0,0 +1,53 @@
require 'rails_helper'

RSpec.describe 'User should see validation errors on item content', js: true do
scenario 'successfully' do
service = create(:service, :with_ctrc_organization)
visit new_service_additional_details_questionnaire_path(service)
fill_in 'questionnaire_name', with: 'New Questionnaire'
fill_in 'questionnaire_items_attributes_0_content', with: 'What is your favorite color?'
select 'Radio Button', from: 'questionnaire_items_attributes_0_item_type'

check 'questionnaire_items_attributes_0_required'

click_button 'Create Questionnaire'

expect(page).to have_content "Content can't be blank"
end

scenario 'successfully' do
service = create(:service, :with_ctrc_organization)
visit new_service_additional_details_questionnaire_path(service)
fill_in 'questionnaire_name', with: 'New Questionnaire'
fill_in 'questionnaire_items_attributes_0_content', with: 'What is your favorite color?'
select 'Radio Button', from: 'questionnaire_items_attributes_0_item_type'
fill_in 'questionnaire_items_attributes_0_item_options_attributes_0_content', with: 'Green'
click_link 'Add another Option'

check 'questionnaire_items_attributes_0_required'

click_button 'Create Questionnaire'

expect(page).to have_content "Content can't be blank"
end

scenario 'successfully' do
service = create(:service, :with_ctrc_organization)
visit new_service_additional_details_questionnaire_path(service)
fill_in 'questionnaire_name', with: 'New Questionnaire'
fill_in 'questionnaire_items_attributes_0_content', with: 'What is your favorite color?'
select 'Radio Button', from: 'questionnaire_items_attributes_0_item_type'
fill_in 'questionnaire_items_attributes_0_item_options_attributes_0_content', with: 'Green'
click_link 'Add another Option'
fill_in 'questionnaire_items_attributes_0_item_options_attributes_1_content', with: 'Red'

check 'questionnaire_items_attributes_0_required'

click_button 'Create Questionnaire'

expect(page).not_to have_content "Content can't be blank"
expect(current_path).to eq service_additional_details_questionnaires_path(service)
expect(Questionnaire.count).to eq 1
end
end

13 changes: 13 additions & 0 deletions spec/models/item_option_spec.rb
Expand Up @@ -2,4 +2,17 @@

RSpec.describe ItemOption, type: :model do
it { is_expected.to belong_to(:item) }

it 'should validate presence if validate_content is true' do
item_option = build(:item_option, content: nil, validate_content: true)

expect(item_option).not_to be_valid
end

it 'should carry along if validate_content is false' do
item_option = build(:item_option, content: nil, validate_content: false)

expect(item_option).to be_valid
end
end

0 comments on commit 8a13425

Please sign in to comment.