Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Field::Select more flexible #1220

Closed
wants to merge 7 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make Field::Select more flexible
  • Loading branch information
jumjamjohn committed Sep 25, 2018
commit 29bab24ec98a0ec2b4ab6dc97ccb43542625ad45
8 changes: 2 additions & 6 deletions app/views/fields/select/_form.html.erb
Original file line number Diff line number Diff line change
@@ -21,11 +21,7 @@ to be displayed on a resource's edit form page.
<div class="field-unit__field">
<%= f.select(
field.attribute,
options_from_collection_for_select(
field.selectable_options,
:to_s,
:to_s,
field.data.presence,
)
field.selectable_options,
{ selected: field.data.presence }
) %>
</div>
2 changes: 1 addition & 1 deletion app/views/fields/select/_index.html.erb
Original file line number Diff line number Diff line change
@@ -13,4 +13,4 @@ to be displayed on a resource's index page.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Select
%>

<%= field.data %>
<%= field.label_data %>
2 changes: 1 addition & 1 deletion app/views/fields/select/_show.html.erb
Original file line number Diff line number Diff line change
@@ -13,4 +13,4 @@ to be displayed on a resource's show page.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Select
%>

<%= field.data %>
<%= field.label_data %>
14 changes: 13 additions & 1 deletion lib/administrate/field/select.rb
Original file line number Diff line number Diff line change
@@ -11,10 +11,22 @@ def selectable_options
collection
end

def label_data
case collection.first
when NilClass
nil
when Array
pair = collection.detect { |l,v| v.to_s == data.to_s } || []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint/UnusedBlockArgument: Unused block argument - l. If it's necessary, use _ or _l as an argument name to indicate that it won't be used.
Layout/SpaceAfterComma: Space missing after comma.

pair.first
else
collection.detect { |v| v.to_s == data.to_s }
end
end

private

def collection
@collection ||= options.fetch(:collection, [])
@collection ||= options.fetch(:collection, []).to_a
end
end
end
126 changes: 126 additions & 0 deletions spec/lib/fields/select_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
require "administrate/field/select"
require "support/field_matchers"
require "pry"

describe Administrate::Field::Select do
include FieldMatchers

let(:persisted) do
{
string: 'approved',
integer: 2,
nil: nil
}
end

let(:output) do
{
two: 2,
approved: 'Approved!',
no_status: 'No status'
}
end

let(:collections) do
{
empty: [],
array_int: [1, 2, 3],
arrays_str: [ ['Pending', 'submitted'], ['Approved!', 'approved'] ],
arrays_sym: [ ['Pending', :submitted], ['Approved!', :approved] ],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

arrays_nil: [ ['No status', nil], ['Approved!', 'approved'] ],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

hash_str: { 'Pending' => 'submitted', 'Approved!' => 'approved' },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

hash_sym: { 'Pending' => :submitted, 'Approved!' => :approved },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

hash_nil: { 'No status' => nil, 'Approved!' => 'approved' }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/TrailingCommaInHashLiteral: Put a comma after the last item of a multiline hash.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

}
end

describe "#to_partial_path" do
it "returns a partial based on the page being rendered" do
page = :show
field = Administrate::Field::Select.new(:select, persisted[:string], page)

path = field.to_partial_path

expect(path).to eq("/fields/select/#{page}")
end
end

it { should_permit_param(:foo, for_attribute: :foo) }

describe "#selectable_options" do
it "returns an empty array without options" do
field = Administrate::Field::Select.new(:select, persisted[:string], :show)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [81/80]


expect(field.selectable_options).to eq([])
end

context "with collection" do
expected = {
:empty => :empty,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

:array_int => :array_int,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

:arrays_str => :arrays_str,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

:arrays_sym => :arrays_sym,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

:arrays_nil => :arrays_nil,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

:hash_str => :arrays_str,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

:hash_sym => :arrays_sym,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

:hash_nil => :arrays_nil

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.
Style/TrailingCommaInHashLiteral: Put a comma after the last item of a multiline hash.

}

expected.each do |in_key, out_key|
it "`#{in_key}` returns collection `#{out_key}`" do
field = select_with_options(persisted[:string], collection: collections[in_key])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [90/80]


expect(field.selectable_options).to eq(collections[out_key])
end
end
end
end

describe "#label_data" do
it "returns nil without options" do
field = Administrate::Field::Select.new(:select, persisted[:string], :show)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [81/80]


expect(field.label_data).to eq(nil)
end

context "with collection" do
expected = {
[ :empty, :string ] => nil,
[ :array_int, nil ] => nil,
[ :array_int, :string ] => nil,
[ :array_int, :integer ] => :two,
[ :arrays_str, nil ] => nil,
[ :arrays_str, :string ] => :approved,
[ :arrays_str, :integer ] => nil,
[ :arrays_sym, nil ] => nil,
[ :arrays_sym, :string ] => :approved,
[ :arrays_sym, :integer ] => nil,
[ :arrays_nil, :nil ] => :no_status,
[ :arrays_nil, :string ] => :approved,
[ :arrays_nil, :integer ] => nil,
[ :hash_str, nil ] => nil,
[ :hash_str, :string ] => :approved,
[ :hash_str, :integer ] => nil,
[ :hash_sym, nil ] => nil,
[ :hash_sym, :string ] => :approved,
[ :hash_sym, :integer ] => nil,
[ :hash_nil, nil ] => :no_status,
[ :hash_nil, :string ] => :approved,
[ :hash_nil, :integer ] => nil,
}

expected.each do |inputs, o_key|
it "`#{inputs.first}` and value `#{inputs.last}` returns output `#{o_key}`" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [86/80]

c_key, p_key = inputs
field = select_with_options(persisted[p_key], collection: collections[c_key])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [87/80]


expect(field.label_data).to eq(output[o_key])
end
end
end
end

def select_with_options(data, options)
Administrate::Field::Select.new(:select, data, :page, options)
end
end