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

Allow a V2 view to specify its parent, regardless of how it's nested #487

Closed
wants to merge 1 commit into from

Conversation

jhollinger
Copy link
Contributor

@jhollinger jhollinger commented Nov 19, 2024

I've been thinking about an "impendance mismatch" between V1 and V2 that will make upgrading more difficult (whether manual or scripted). This is an attempt to patch over a common case. I'm not 100% sold on it myself - I'd like to keep the DSL as simple as possible. Feedback welcome.

Consider the following V1 Blueprint:

class WidgetBlueprint < Blueprinter::Base
  view :normal do
    field :name
  end

  view :extended do
    include_view :normal
    field :description
  end
end

There are currently two ways to represent this in V2: inheritance and partials.

# Inheritance has the drawback that the extended view's name will change to
# "normal.extended". This will require callsites to be updated with the new name. 
class WidgetBlueprint < Blueprinter::V2::Base
  view :normal do
    field :name

    view :extended do
      field :description
    end
  end
end

# Using partials avoids that, but looks...not great. A converted codebase would be
# full of Blueprints defined like this:
class WidgetBlueprint < Blueprinter::V2::Base
  partial :normal do
    field :name
  end

  view :normal do
    use :normal
  end

  view :extended do
    use :normal
    field :description
  end
end

What this PR adds

This PR allows a view to specify which view it should inherit from, without affecting the name:

class WidgetBlueprint < Blueprinter::V2::Base
  view :normal do
    field :name
  end

  view :extended, inherit: :normal do
    field :description
  end
end

Maybe a better idea??

Note that the above doesn't address cases where V1 views inherit from multiple views. Those would still need to resort to partials. What if every view implicitly defined a partial for itself? 🤯 I think that would solve everything. Much simpler to implement, too.

@jhollinger
Copy link
Contributor Author

Dropped in favor of #488.

@jhollinger jhollinger closed this Nov 19, 2024
@jhollinger jhollinger deleted the jh/view-parent branch November 21, 2024 18:42
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.

1 participant