-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 1 commit
29bab24
851b299
286c702
c785361
3940aec
935cd05
b5eb5f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
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] ], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets. |
||
arrays_nil: [ ['No status', nil], ['Approved!', 'approved'] ], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets. |
||
hash_str: { 'Pending' => 'submitted', 'Approved!' => 'approved' }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/HashSyntax: Use the new Ruby 1.9 hash syntax. |
||
} | ||
|
||
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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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.