Skip to content

Commit

Permalink
add option to disable cents field
Browse files Browse the repository at this point in the history
  • Loading branch information
ajb committed Mar 10, 2014
1 parent 72eae28 commit 51d71b6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
13 changes: 12 additions & 1 deletion app/models/formbuilder/response_field_price.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,34 @@ class ResponseFieldPrice < ResponseField
}

def render_input(value, opts = {})
"""
value ||= {}

str = """
<div class='input-line'>
<span class='above-line'>$</span>
<span class='dollars'>
<input type='text' name='response_fields[#{self[:id]}][dollars]' id='response_fields_#{self[:id]}' value='#{value['dollars']}' />
<label>Dollars</label>
</span>
"""

unless self.field_options['disable_cents']
str += """
<span class='above-line'>.</span>
<span class='cents'>
<input type='text' name='response_fields[#{self[:id]}][cents]' value='#{value['cents']}' maxlength='2' />
<label>Cents</label>
</span>
"""
end

str += """
</div>
"""

str
end

def render_entry(value, opts = {})
Expand Down
4 changes: 3 additions & 1 deletion spec/lib/formbuilder/entry_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@
entry.should be_valid
entry.save_response({ 'cents' => '1a' }, field)
entry.should_not be_valid
entry.save_response({ 'cents' => '1' }, field)
entry.save_response({ 'cents' => '1', 'dollars' => '' }, field)
entry.should be_valid
entry.save_response({ 'dollars' => '1', 'cents' => '' }, field)
entry.should be_valid
entry.save_response({ 'dollars' => '3a', 'cents' => '1' }, field)
entry.should_not be_valid
Expand Down
30 changes: 30 additions & 0 deletions spec/models/formbuilder/response_field_price_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'spec_helper'

describe Formbuilder::ResponseFieldPrice do

let!(:form) { Formbuilder::Form.create }
let!(:response_field) { Formbuilder::ResponseFieldPrice.create(form: form) }
let!(:entry) { Entry.create(form: form) }

subject { response_field }
subject(:rf) { response_field }

describe '#render_input' do
it 'renders properly' do
rendered_html = rf.render_input(nil)
expect(rendered_html).to match 'Dollars'
expect(rendered_html).to match 'Cents'
end

context 'with cents field disabled' do
before { rf.update_attributes(field_options: { 'disable_cents' => true } )}

it 'does not render the cents field' do
rendered_html = rf.render_input(nil)
expect(rendered_html).to match 'Dollars'
expect(rendered_html).to_not match 'Cents'
end
end
end

end

0 comments on commit 51d71b6

Please sign in to comment.