Skip to content

Commit

Permalink
Use Cells API more idiomatically. [#47]
Browse files Browse the repository at this point in the history
  • Loading branch information
marnen committed Apr 14, 2020
1 parent 57f14da commit 0d3a076
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 30 deletions.
10 changes: 5 additions & 5 deletions app/cells/state_list_cell.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class StateListCell < Cell::ViewModel
include ERB::Util

def initialize(states, options = {})
@states = states
end

private

def states
model
end

def state_names_and_abbrs
@states.map.with_index(1) {|state, index| name_and_abbr state, index }
states.map.with_index(1) {|state, index| name_and_abbr state, index }
end

def name_and_abbr(state, index)
Expand Down
13 changes: 5 additions & 8 deletions app/cells/state_selector_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ class StateSelectorCell < Cell::ViewModel
include ActionView::Helpers::FormHelper
include ActionView::Helpers::FormOptionsHelper

attr_reader :states, :url
private

def initialize(states, options)
super
@states = states
@url = options[:url]
def states
model
end

def show
render
def url
@url ||= options[:url]
end

private

def locale_state_options
@locale_state_options ||= states_for_menu.sort_by {|state| _(state.name)}.map {|state| [_(state.name), state.abbr]}
Expand Down
18 changes: 1 addition & 17 deletions spec/cells/state_selector_cell_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,6 @@
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

Expand All @@ -45,7 +29,7 @@
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
it 'renders a multi-select with all the states as options, sorted by name' 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
Expand Down

0 comments on commit 0d3a076

Please sign in to comment.