-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fallback to
select
if array of checkboxes
fail
* Allows fields that are arrays of strings to be passed to `<select multiple>` inputs * Update README.md * Add coverage for Array of checkboxes * Rescue `Capybara` errors and rethrow as Formulaic * Check for presence of all options in ArrayInput * Raise Formulaic::InputNotFound with useful message if not all options are present in either a select[multiple]'s options or as checkboxes. * Ensure checkboxes exist with `:has` http://dev.w3.org/csswg/selectors4/#relational * Extract `SelectInput` and `CheckboxInput` * Both are subclasses of `ArrayInput`. Now, `ArrayInput#fill` chains together successive calls to `SelectInput#fill` and `CheckboxInput#fill`, finally failing with an informative exception if neither inputs exist on the page. * Enure that I18n's aren't humanized
- Loading branch information
1 parent
ed47902
commit 4258e08
Showing
8 changed files
with
205 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,40 @@ | ||
module Formulaic | ||
module Inputs | ||
class ArrayInput < Input | ||
def initialize(label, value) | ||
@label = label | ||
@value = value | ||
end | ||
|
||
def fill | ||
value.each { |checkbox| check checkbox } | ||
attempt_to_fill_selects || | ||
attempt_to_fill_checkboxes || | ||
raise_input_error | ||
end | ||
|
||
private | ||
|
||
def attempt_to_fill_selects | ||
SelectInput.new(label, value).fill | ||
end | ||
|
||
def attempt_to_fill_checkboxes | ||
CheckboxInput.new(label, value).fill | ||
end | ||
|
||
def contains_all_options?(nodes) | ||
nodes.map(&:text).to_set.superset?(value.to_set) | ||
end | ||
|
||
def raise_input_error | ||
raise( | ||
InputNotFound, | ||
%[Unable to find checkboxes or select[multiple] "#{label}" containing all options #{value.inspect}.] | ||
) | ||
end | ||
end | ||
end | ||
end | ||
|
||
require 'formulaic/inputs/checkbox_input' | ||
require 'formulaic/inputs/select_input' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Formulaic | ||
module Inputs | ||
class CheckboxInput < ArrayInput | ||
def fill | ||
if has_check_boxes? | ||
check_boxes | ||
true | ||
else | ||
false | ||
end | ||
end | ||
|
||
private | ||
|
||
def has_check_boxes? | ||
contains_all_options?(checkbox_labels) | ||
end | ||
|
||
def check_boxes | ||
value.each { |checkbox| check checkbox } | ||
end | ||
|
||
def checkbox_labels | ||
all(checkbox_labels_selector) | ||
end | ||
|
||
def checkbox_name_selector | ||
"input[type='checkbox'][name='#{label.model_name}[#{label.attribute}][]']" | ||
end | ||
|
||
def checkbox_labels_selector | ||
"#{checkbox_name_selector} ~ label,label:has(#{checkbox_name_selector})" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Formulaic | ||
module Inputs | ||
class SelectInput < ArrayInput | ||
def fill | ||
if has_multiple_select? | ||
select_options | ||
true | ||
else | ||
false | ||
end | ||
end | ||
|
||
private | ||
|
||
def select_options | ||
value.each { |option| select option, from: label.to_str } | ||
end | ||
|
||
def has_multiple_select? | ||
has_select? && select_is_multiple? | ||
end | ||
|
||
def has_select? | ||
has_field?(label, type: "select") | ||
end | ||
|
||
def select_is_multiple? | ||
select_element[:multiple].present? && | ||
contains_all_options?(select_element.all("option")) | ||
end | ||
|
||
def select_element | ||
@select_element ||= find_field(label, type: "select") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters