Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
give dropdown and multiple choice qs action_ prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NealJMD committed Dec 12, 2016
1 parent 34cfbff commit 3601d18
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 32 deletions.
23 changes: 16 additions & 7 deletions app/models/form_element.rb
Expand Up @@ -104,16 +104,25 @@ def set_position
self.position = last_position
end

def field_prefix(data_type)
case data_type
when 'paragraph', 'text'
'action_textentry_'
when 'checkbox'
'action_box_'
when 'dropdown'
'action_dropdown_'
when 'choice'
'action_choice_'
else
'action_'
end
end

def set_name
unless name.blank? || ActionKitFields::ACTIONKIT_FIELDS_WHITELIST.include?(name)
if !(name =~ ActionKitFields::VALID_PREFIX_RE) && !(name =~ /^(action_)+$/)
self.name = if data_type == 'paragraph' || data_type == 'text'
"action_textentry_#{name}"
elsif data_type == 'checkbox'
"action_box_#{name}"
else
"action_#{name}"
end
self.name = field_prefix(data_type) + name
end
end
end
Expand Down
60 changes: 35 additions & 25 deletions spec/models/form_element_spec.rb
Expand Up @@ -38,46 +38,56 @@
end

describe 'name fixing' do
let(:base_params) { { label: 'My label!', form: form, name: 'foo_bar' } }

it 'keeps name if whitelisted' do
expect(
FormElement.create(label: 'My label!', form: form, name: 'address1').name
).to eq('address1')
name = FormElement.create(label: 'My label!', form: form, name: 'address1').name
expect(name).to eq('address1')
end

it 'prefixes custom names' do
expect(
FormElement.create(label: 'My label!', form: form, name: 'foo_bar', data_type: 'email').name
).to eq('action_foo_bar')
name = FormElement.create(label: 'My label!', form: form, name: 'foo_bar', data_type: 'email').name
expect(name).to eq('action_foo_bar')
end

it 'prefixes the name to indicate checkboxes' do
name = FormElement.create(base_params.merge(data_type: 'checkbox')).name
expect(name).to eq('action_box_foo_bar')
end

it 'prefixes the name to indicate paragraphs' do
name = FormElement.create(base_params.merge(data_type: 'paragraph')).name
expect(name).to eq('action_textentry_foo_bar')
end

it 'prefixes the name to indicate text' do
name = FormElement.create(base_params.merge(data_type: 'text')).name
expect(name).to eq('action_textentry_foo_bar')
end

it 'prefixes the name to indicate dropdowns' do
name = FormElement.create(base_params.merge(data_type: 'dropdown')).name
expect(name).to eq('action_dropdown_foo_bar')
end

it 'prefixes names based on checkbox/paragraph types' do
expect(
FormElement.create(label: 'My label!', form: form, name: 'foo_bar', data_type: 'checkbox').name
).to eq('action_box_foo_bar')
expect(
FormElement.create(label: 'My label!', form: form, name: 'foo_bar', data_type: 'paragraph').name
).to eq('action_textentry_foo_bar')
expect(
FormElement.create(label: 'My label!', form: form, name: 'foo_bar', data_type: 'text').name
).to eq('action_textentry_foo_bar')
it 'prefixes the name to indicate multiple choice' do
name = FormElement.create(base_params.merge(data_type: 'choice')).name
expect(name).to eq('action_choice_foo_bar')
end

it 'does nothing when prefix is present' do
expect(
FormElement.create(label: 'My label!', form: form, name: 'action_foo_bar').name
).to eq('action_foo_bar')
name = FormElement.create(label: 'My label!', form: form, name: 'action_foo_bar').name
expect(name).to eq('action_foo_bar')
end

it 'does nothing when name is blank' do
expect(
FormElement.create(label: 'My label!', form: form, name: '').name
).to eq('')
name = FormElement.create(label: 'My label!', form: form, name: '').name
expect(name).to eq('')
end

it 'does nothing when name is "action_"' do
expect(
FormElement.create(label: 'My label!', form: form, name: 'action_').name
).to eq('action_')
name = FormElement.create(label: 'My label!', form: form, name: 'action_').name
expect(name).to eq('action_')
end
end
end
Expand Down

0 comments on commit 3601d18

Please sign in to comment.