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

[Question/Support] Set one view as the default view #398

Closed
1 task done
araccaine opened this issue Mar 5, 2024 · 3 comments
Closed
1 task done

[Question/Support] Set one view as the default view #398

araccaine opened this issue Mar 5, 2024 · 3 comments

Comments

@araccaine
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe your question or ask for support

Question

Is there something like a "default view" that I can set when using an association?

Currently, I also have to specify an association with an explicit view:

association :sections, blueprint: SectionBlueprint, view: :normal

Example

# The base serializer, e.g. chapters of a book
class ChapterBlueprint < Blueprinter::Base
  identifier :id

  view :normal do
    fields :name, :description
  end

  view :extended do
    include_view :normal

    association :sections, blueprint: SectionBlueprint # Here I have to write view: :normal, otherwise it only renders the section's :id
  end
end

# Each chapter has 0 to n sections
class SectionBlueprint < Blueprinter::Base
  identifier :id

  view :normal do
    fields :name, :page_ref
  end

  view :extended do
    include_view :normal

    fields :created_at, :updated_at
  end
end

What I am looking for

Something like this:

Blueprinter.configure do |config|
  config.default_view = :normal
end
@ritikesh
Copy link
Collaborator

ritikesh commented Mar 15, 2024

Hi @araccaine, whatever you define at the root level, automatically already becomes your default view. So you could still achieve this with -

# The base serializer, e.g. chapters of a book
class ChapterBlueprint < Blueprinter::Base
  identifier :id
  fields :name, :description

  view :extended do
    association :sections, blueprint: SectionBlueprint # Here it renders the section's default view
  end
end

# Each chapter has 0 to n sections
class SectionBlueprint < Blueprinter::Base
  identifier :id
  fields :name, :page_ref

  view :extended do
    fields :created_at, :updated_at
  end
end

Let me know if that helps!

@araccaine
Copy link
Author

Thank you very much for your detailed answer!

Now after reading my own code and this issue again after a few weeks I see that my question was not what I intended to achieve. What I really need is:

  • The :extended view would be the default view
  • The :normal view (with less information/fields) would be used only for special occasions.

A better naming may be something like :detailed and :simple, where :detailed is the default view with more fields. For the other way round (like my first question was worded) your suggestion would work. But having more fields in the "default view" would not work.

Copy link

This issue 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.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants