text_field doesn't display correctly with attribute default value of Proc #24249

Closed
willnet opened this Issue Mar 19, 2016 · 4 comments

Projects

None yet

4 participants

@willnet
Contributor
willnet commented Mar 19, 2016

Steps to reproduce

executable test case

  • create new rails project
  • rails g scaffold post title
  • rails db:migrate
  • modify post model like following
class Post < ApplicationRecord
  attribute :title, :string, default: -> { 'hello' }
end

Expected behavior

image

Actual behavior

image

System configuration

Rails version:
5.0.0.beta3 and master

Ruby version:
2.3.0

@maclover7
Member

Is passing a Proc supported, as per the documentation?

@willnet
Contributor
willnet commented Mar 20, 2016

@maclover7

This example seems to say that we can use Proc.

@Sen-Zhang
Contributor

Replaced the old solution with a new one.

@sgrif sgrif added a commit that closed this issue Mar 24, 2016
@sgrif sgrif Memoize user provided defaults before type casting
When a proc is given as a default value, the form builder ends up
displaying `Proc#to_s` when the default is used. That's because we
didn't handle the proc until type casting. This issue technically can
occur any time that a proc is the value before type casting, but in
reality the only place that will occur is when a proc default is
provided through the attributes API, so the best place to handle this
edge case is there.

I've opted to memoize instead of just moving the `Proc#call` up, as this
made me realize that it could potentially interact very poorly with
dirty checking.

The code here is a little redundant, but I don't want to rely on how
`value_before_type_cast` is implemented in the super class, even if it's
just an `attr_reader`.

Fixes #24249

Close #24306
ba06dab
@sgrif sgrif closed this in ba06dab Mar 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment