Skip to content

Commit

Permalink
Workaround bug with multipart date in ActiveInteraction::Base#given?
Browse files Browse the repository at this point in the history
  • Loading branch information
solebared committed Mar 25, 2021
1 parent 4a8186c commit 0ccbcc3
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/forms/base_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ def self.build params
end

def given_inputs
inputs.select{|key| given? key }
# FIXME: ActiveInteraction::Base#given? seems to have a bug recognizing multipart date params
raw_inputs = @_interaction_inputs
inputs.select do |key|
given?(key) || (
raw_inputs.key?(:"#{key}(1i)") &&
raw_inputs.key?(:"#{key}(2i)") &&
raw_inputs.key?(:"#{key}(3i)")
)
end
end
end
80 changes: 80 additions & 0 deletions spec/forms/base_form_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BaseForm do
describe '#given_inputs' do
let(:form) do
Class.new(BaseForm) {
string :string, default: nil
date :date, default: nil

def execute
[inputs, given_inputs]
end

def self.model_name # needed for anonymous classes
ActiveModel::Name.new(self, nil, "BaseFormSpec")
end
}
end

let(:result) { form.build params }
let(:inputs) { result[0] }
let(:given_inputs) { result[1] }

context 'with no params given' do
let(:params) {{}}

it 'recognizes no params were given' do
expect(inputs).to eq(string: nil, date: nil)
expect(given_inputs).to be_empty
end
end

context 'with an explicitly nil param' do
let(:params) {{ date: nil }}

specify 'inputs show all params with defaults' do
expect(inputs).to eq(string: nil, date: nil)
end

specify 'given_inputs only show params that were explicitly given' do
expect(given_inputs).to eq(date: nil)
end
end

context 'with params given' do
let(:params) {{ string: 'a string', date: '2020-12-31' }}

it 'recognizes the given params' do
expect(inputs).to eq(string: 'a string', date: Date.new(2020, 12, 31))
expect(given_inputs).to eq inputs
end
end

context 'with a complete multi-part date param' do
let(:params) {{
'date(1i)' => '2020',
'date(2i)' => '12',
'date(3i)' => '31',
}}

it 'recognizes the multi-part date was given' do
expect(inputs).to eq(string: nil, date: Date.new(2020, 12, 31))
expect(given_inputs).to eq(date: Date.new(2020, 12, 31))
end
end

context 'with a partial multi-part date param' do
let(:params) {{
'date(1i)' => '2020',
}}

it 'ignores the date' do
expect(inputs).to eq(string: nil, date: nil)
expect(given_inputs).to be_empty
end
end
end
end

0 comments on commit 0ccbcc3

Please sign in to comment.