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 options_for_select generic #295

Merged
merged 1 commit into from
Mar 5, 2020

Conversation

paulcsmith
Copy link
Member

Closes #292

This works, the problem is some Fields are nilable, which would mean the
second item in the tuple would be nilable. That's probably not what the
user wants. On the other hand, it seems unlikely that a nil option
would end up there.

@paulcsmith
Copy link
Member Author

paulcsmith commented Dec 15, 2017

Maybe the nil option could be handled like this:

field : LuckyRecord::AllowedField, select_options : Array(Tuple(String, Nil)

# Or if the AllowedField needs a type
field : LuckyRecord::AllowedField(Object?), select_options : Array(Tuple(String, Nil)

I think this might work. I'll try it out later since this PR is not a priority

@jwoertink
Copy link
Member

Documenting this method, and realized that it's restrictive. This PR fixes it, so maybe we can revisit this? I think doing Tuple(String, AnyPKeyType) would be fine. Even if it's not a pkey type, String being in there would catch any other possible value.

@paulcsmith
Copy link
Member Author

Thanks for brining this back up. Now that I look at it, I think having nil is a-ok. What if you want an option like "Disassociate user" or something. Then you should be able to do that by setting the value to nil. So I think if this is rebased it can be merged as-is!

Thoughts?

@jwoertink
Copy link
Member

Yeah, that makes sense. Does this support a way to set the label like how rails has the include_blank: true option or the prompt: "Select your thing" option?

@paulcsmith
Copy link
Member Author

No it doesn't so we'd have to document that.

I think what would be even better is a separate method rather than yet another option (Rails helpers have too many options IMO).

# If one is chosen then don't render anything, if not render the prompt
select_prompt op.country, "Select a country" 
select_options op.country, # blah blah blah

For including a blank item I'd add another tuple manually: {"-", nil}. Which is nice because it won't allow it if the field doesn't allow nil to be set.

Thoughts?

@jwoertink
Copy link
Member

Totally agree on the option bloat. I love that idea of a separate method for adding a prompt option.

select_tag(op.opts) do
  select_prompt("Select an option")
  options_for_select(op.opts, my_options)
end

@paulcsmith
Copy link
Member Author

Yeah that looks good! Only change is select_prompt needs the attribute so it can skip rendering if a value is already set. But maybe you can skip passing the attribute if you want it to always show up

@paulcsmith paulcsmith force-pushed the ps-292-generic-options-for-select branch from 80f290a to edd16dd Compare March 5, 2020 18:55
@paulcsmith
Copy link
Member Author

Ready for re-review. Could you create an issue for adding select_prompt?

@@ -41,14 +41,14 @@ describe Lucky::SelectHelpers do
end

it "renders options" do
view.render_options(form.company_id, [{"Volvo", 2}, {"BMW", 3}])
view.render_options(form.company_id, [{"Volvo", "2"}, {"BMW", "3"}])
Copy link
Member

Choose a reason for hiding this comment

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

Does this mean it always needs to be Tuple(String, String)? I'm just thinking of the example from a query

CarQuery.new.all.map do |car|
  {car.make, car.id.to_s}
end

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope! It means that it has to be whatever the type is on the attribute. In this case company_id is an attribute with the String type. I can change it to Int32 though so this makes more sense

Copy link
Member

Choose a reason for hiding this comment

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

Oh, ok. I like that.

Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

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

Alright, this looks great to me!

Closes #292

This works, the problem is some Fields are nilable, which would mean the
second item in the tuple would be nilable. That's probably not what the
user wants. On the other hand, it seems unlikely that a `nil` option
would end up there.
@paulcsmith paulcsmith force-pushed the ps-292-generic-options-for-select branch from edd16dd to dc2e03a Compare March 5, 2020 19:14
@paulcsmith paulcsmith merged commit bd3034f into master Mar 5, 2020
@paulcsmith paulcsmith deleted the ps-292-generic-options-for-select branch March 5, 2020 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make options_for_select generic so it works with any kind of field
2 participants