-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract state selector to cell. [#47]
- Loading branch information
Showing
7 changed files
with
101 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
= form_with url: url, local: true, class: 'state-selector' do |form| | ||
%p | ||
= label_tag :states, _('Choose one or more states:') | ||
- locale_state_options = states_for_menu.sort_by {|state| _(state.name)}.map {|state| [_(state.name), state.abbr]} | ||
= select_tag :states, options_for_select(locale_state_options, @states&.map(&:abbr)), multiple: true | ||
%p | ||
= submit_tag _('Go') |
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,22 @@ | ||
class StateSelectorCell < Cell::ViewModel | ||
include ActionView::Helpers::FormHelper | ||
include ActionView::Helpers::FormOptionsHelper | ||
|
||
attr_reader :states, :url | ||
|
||
def initialize(states, options) | ||
super | ||
@states = states | ||
@url = options[:url] | ||
end | ||
|
||
def show | ||
render | ||
end | ||
|
||
private | ||
|
||
def states_for_menu | ||
@states_for_menu ||= State.all | ||
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 |
---|---|---|
@@ -1,5 +1,2 @@ | ||
module ApplicationHelper | ||
def states | ||
State.all | ||
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 was deleted.
Oops, something went wrong.
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,71 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe StateSelectorCell, type: :cell do | ||
let(:states) { [] } | ||
let(:url) { nil } | ||
let(:options) { {url: url} } | ||
|
||
subject { described_class.new states, options } | ||
|
||
context 'constructor' do | ||
it { is_expected.to be_a_kind_of Cell::ViewModel } | ||
|
||
it 'includes form helpers' do | ||
['FormHelper', 'FormOptionsHelper'].each do |helper| | ||
expect(subject).to be_a_kind_of "ActionView::Helpers::#{helper}".constantize | ||
end | ||
end | ||
end | ||
|
||
context '#states' do | ||
let(:states) { State.all.sample rand(2..5) } | ||
|
||
it 'returns the given states' do | ||
expect(subject.states).to be == states | ||
end | ||
end | ||
|
||
context '#url' do | ||
let(:url) { File.join *Faker::Lorem.words(rand 2..5) } | ||
|
||
it 'returns the given URL' do | ||
expect(subject.url).to be == url | ||
end | ||
end | ||
|
||
context '#show' do | ||
controller Class.new(ApplicationController) # see https://github.com/trailblazer/rspec-cells#url-helpers | ||
|
||
let(:url) { File.join *Faker::Lorem.words(rand 2..5) } | ||
let(:menu) { 'select[multiple][name="states[]"]' } | ||
|
||
subject { Capybara.string cell(described_class, states, options).call } | ||
|
||
it 'renders a form that will submit to the given URL' do | ||
expect(subject).to have_selector "form[action='#{url}']" | ||
end | ||
|
||
it 'renders a multi-select with all the states as options, sorted by nam,e' do | ||
subject.find menu do |menu| | ||
selector = './/' + State.all.sort_by(&:name).map {|state| "option[@value='#{state.abbr}'][text()='#{state.name}']"}.join('/following-sibling::') | ||
expect(menu).to have_xpath selector | ||
end | ||
end | ||
|
||
context 'states given' do | ||
let(:states) { State.all.sample rand(2..5) } | ||
|
||
it 'marks all the states given as selected' do | ||
subject.find(menu) do |menu| | ||
expect(menu.all('option[selected]').map &:text).to match_array states.map(&:name) | ||
end | ||
end | ||
end | ||
|
||
context 'no states given' do | ||
it 'marks no states as selected' do | ||
expect(subject.find menu).not_to have_css 'option[selected]' | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.