We’re using SimpleFrom because of its powers in customization. We consider creating custom inputs every time we’re going to write something like this:
1 2 3 |
|
It’s worth to have simple input definition in views without need to specify all those options every time. The following looks much better:
1
|
|
Or even:
1
|
|
But every time implementing custom inputs we should bother about writing tests for them. Let me show the way we write tests for custom SimpleFrom inputs. We’ll use RSpec and put input specs into spec/inputs
directory.
Before writing tests we need to implement an example group module that will be included into input specs. To do this you can create spec/support/inputs.rb
with the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Now you have input_for
test helper method available in your input spec. So it’s time to describe the birth date input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
We use input_for
test helper method here and assert_select
method from Rails.
app/inputs/birth_date_input.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Finally we need to configure input mappings in order to use f.input :birth_date
instead of f.input :birth_date, as: :birth_date
for attributes that have birth_date
in their names. This is done modifying SimpleForm configuration in config/initializers/simple_form.rb
:
1 2 3 4 5 |
|
Using this approach we’re able to clean views using custom SimpleForm inputs and easily write tests for them.