Skip to content

Unify decorate_attribute_type and attribute - #39929

Merged
kaspth merged 1 commit into
rails:masterfrom
jonathanhefner:decorate-original-attribute-type
Nov 30, 2020
Merged

kaspth merged 1 commit into
rails:masterfrom
jonathanhefner:decorate-original-attribute-type

Conversation

@jonathanhefner

@jonathanhefner jonathanhefner commented Jul 26, 2020 •

Copy link
Copy Markdown
Member

Follow-up to 75c309c.

As a result of these changes, attributes can have their type and default value configured separately. Similar behavior was implemented in #39830, but only for attributes that derive (and do not override) their type from the database.

@kamipo

kamipo commented Jul 27, 2020

Copy link
Copy Markdown
Member

I'd not love to inline complexity (inlining user-defined type decoration) in attribute.
I've just added back decorate_attribute_type as before #39882.

@jonathanhefner

Copy link
Copy Markdown
Member Author

@kamipo Would you mind elaborating a little bit? The approach in this PR does not seem very complex to me. Is there future functionality that you are thinking of that this PR would compromise?

I can imagine the approach in this PR being extended to provide options refinement (e.g. options = prev_options.merge(options)) and decorator stacking (e.g. blocks = [*prev_blocks, *block]) in the future, if we want.

Additionally, it seems strange to me that decorate_attribute_type (from 75c309c) has a **default (i.e. options) parameter. I see how it is used, but it feels out of place to me.

Comment on lines +212 to +215
self.attributes_to_define_after_schema_loads =
attributes_to_define_after_schema_loads.merge(
name => [cast_type || block, options]
)
prev_cast_type, prev_options, prev_block = (attributes_to_define_after_schema_loads[name] unless cast_type)

self.attributes_to_define_after_schema_loads = attributes_to_define_after_schema_loads.merge(
name => [cast_type || prev_cast_type, options.presence || prev_options || {}, block || prev_block]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure if you would agree, this line is most hard to read part than before.

It will allow to work prev_cast_type with options (not prev_options), people might not actually do that though.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Working prev_cast_type with options is maybe intended?
Even so, I think this line is still hard to understand how works as intended than before.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, I did that to ensure the behavior from #39830 also works for attributes that are not from the database.

class Post < ActiveRecord::Base
  attribute :x, :integer
end

class SpecialPost < Post
  attribute :x, default: 42
end

# BEFORE (ignore prev_cast_type if options.present?)
SpecialPost.type_for_attribute(:x) # => #<ActiveModel::Type::Value...>

# AFTER (allow prev_cast_type)
SpecialPost.type_for_attribute(:x) # => #<ActiveModel::Type::Integer...>

I can remove it though, and make it a separate PR.

But to try to explain the overall idea: When attribute is called with a cast_type, it signals a brand new attribute definition (which overwrites any previous definition of the same name). When attribute is called without a cast_type, it signals a modification to an existing attribute definition (e.g. new options or new block).

Would writing it like this be more readable?

if cast_type.nil?
  cast_type = attributes_to_define_after_schema_loads.dig(name, 0)
  options = attributes_to_define_after_schema_loads.dig(name, 1).to_h if options.empty?
  block = attributes_to_define_after_schema_loads.dig(name, 2) if block.nil?
end

self.attributes_to_define_after_schema_loads = attributes_to_define_after_schema_loads.merge(
  name => [cast_type, options, block]
)

Or would it be better to break up that line for better readability?

self.attributes_to_define_after_schema_loads = attributes_to_define_after_schema_loads.merge(
  name => [
    cast_type || prev_cast_type,
    options.presence || prev_options || {},
    block || prev_block
  ]
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't quite understand what it is we're trying to do in this code? Even with the default: explanation?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The underlying idea is that an attribute's type, options (e.g. default value), and decorator could all be declared separately. This is already partially supported, but only when the type is derived from the database and not overridden. The changes here extend that behavior in a general way.

I've tried to clarify that code a bit. Please let me know what you think.

type = type_for_attribute(name)
end

type = transform[type] if transform

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This feature (specific type with decoration attribute :type do |type| ... end) is intentionally limited since it is not internally used.

If we'd like to provide internally unused feature, I don't think it's appropriate to sneak it in as part of the refactoring.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah, I thought the block of attribute was meant to be an internal-only feature, since it is undocumented. Would it be good to make it an internal-only feature? If so, should we rename it to _block?

But if not, I can enforce the limitation by raising an ArgumentError when both cast_type and block are specified.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

But if not, I can enforce the limitation by raising an ArgumentError when both cast_type and block are specified.

That sounds right to me 👍

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, looks like this is actually used internally for the EnumType. Not sure how to do that in any other way without the block form? I guess you'd end up in awkward type_for_attribute before then calling attribute, doesn't sound right.

@jonathanhefner

Copy link
Copy Markdown
Member Author

@kamipo Thank you for taking the time to review this! 🙏

@rails-bot

rails-bot Bot commented Oct 26, 2020

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot Bot added the stale label Oct 26, 2020
@rails-bot rails-bot Bot closed this Nov 2, 2020

@kaspth kaspth left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sweet! Let me reopen this.


type = transform[type] if transform

define_attribute(name, type, **options.slice(:default))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
define_attribute(name, type, **options.slice(:default))
define_attribute(name, type, default: options[:default])

Since we're only passing one value, would it be clearer as just this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That would behave differently when there is no default. When there is no default value, **options.slice(:default) is equivalent to **{}, but default: options[:default] is equivalent to default: nil, which would override NO_DEFAULT_PROVIDED.

type = type_for_attribute(name)
end

type = transform[type] if transform

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, looks like this is actually used internally for the EnumType. Not sure how to do that in any other way without the block form? I guess you'd end up in awkward type_for_attribute before then calling attribute, doesn't sound right.

Comment on lines +212 to +215
self.attributes_to_define_after_schema_loads =
attributes_to_define_after_schema_loads.merge(
name => [cast_type || block, options]
)
prev_cast_type, prev_options, prev_block = (attributes_to_define_after_schema_loads[name] unless cast_type)

self.attributes_to_define_after_schema_loads = attributes_to_define_after_schema_loads.merge(
name => [cast_type || prev_cast_type, options.presence || prev_options || {}, block || prev_block]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't quite understand what it is we're trying to do in this code? Even with the default: explanation?

@kaspth kaspth reopened this Nov 3, 2020
@rails-bot rails-bot Bot removed the stale label Nov 3, 2020
@jonathanhefner
jonathanhefner force-pushed the decorate-original-attribute-type branch from 57cd297 to ac58275 Compare November 4, 2020 22:12
@jonathanhefner jonathanhefner changed the title Retain original attribute type when decorating Unify decorate_attribute_type and attribute Nov 4, 2020

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would this be clearer if we had a separate setter method?

def attribute(name, cast_type = nil, **options, &decorator)
  name = name.to_s
  reload_schema_from_cache

  override_attribute_defined_after_schema_load name do |prev_cast_type, prev_options, prev_decorator|
    [ cast_type || prev_cast_type,
      options.presence || prev_options,
      decorator || prev_decorator ] unless cast_type && prev_cast_type
  end
end

Or potentially the more wasteful option (hey zip!):

def attribute(name, cast_type = nil, **options, &decorator)
  name = name.to_s
  reload_schema_from_cache

  override_attribute_defined_after_schema_load name do |previous_definition|
    [ cast_type, options, decorator ].zip(previous_definition).map { |option| option.find(&:present?) }
  end
end

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This intersects a bit with #39487. After #39487, the merge would become:

update_heritable_value_of(:attributes_to_define_after_schema_loads, name, [cast_type, options, decorator])

Follow-up to 75c309c.

As a result of these changes, attributes can have their type and default
value configured separately.  Similar behavior was implemented in rails#39380,
but only for attributes that derive (and do not override) their type
from the database.
@jonathanhefner
jonathanhefner force-pushed the decorate-original-attribute-type branch from ac58275 to 4cc9c0f Compare November 30, 2020 21:52
@jonathanhefner

Copy link
Copy Markdown
Member Author

Rebased since 8f2decd and #40545. (This PR accommodates both of those behaviors.)

@kaspth
kaspth merged commit 79d0c17 into rails:master Nov 30, 2020
@kaspth

kaspth commented Nov 30, 2020

Copy link
Copy Markdown
Contributor

Cool, let's go for this and then the PR intersection with heritable value can swoop back in here.

kamipo added a commit to kamipo/rails that referenced this pull request Jan 16, 2021
…riginal-attribute-type"

This reverts commit 79d0c17, reversing
changes made to bc828f7.

Fixes rails#41138.
jorgemanrubia added a commit to basecamp/rails that referenced this pull request Apr 1, 2021
This uses the changes from rails#39929. Instead of doing a manual casting of
the previous value as part of the type, it leverages the existing support to do this now.

It also fixes a problem when loading a class with an encrypt declaration that wasn't backed
by a table was failing.

CC @kaspth
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants