Skip to content

[bug] Default transformer is not available in views #514

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

Open
1 of 2 tasks
sanvijay opened this issue Apr 17, 2025 · 1 comment
Open
1 of 2 tasks

[bug] Default transformer is not available in views #514

sanvijay opened this issue Apr 17, 2025 · 1 comment
Labels
bug An issue with the system

Comments

@sanvijay
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Is this a regression?

  • Yes, this used to work before

Current Behavior

This is the sample blueprint I have

class UserBlueprint < Blueprinter::Base
  transform DefaultTransformer

  view :normal do
    transform ViewTransformer
  end

  view :extended do
    include_view :normal
  end

  association :address, blueprint: AddressBlueprint

  view :without_address do
    exclude :address
  end
end

As per the document, I should be able to see DefaultTransformer, but I don't see it.

For now, I edited the view like below to make it work

view :without_address do
    transform DefaultTransformer

    exclude :address
  end

Expected Behavior

I should see the DefaultTransformer inside the view.

Steps To Reproduce

This is the sample blueprint I have

class UserBlueprint < Blueprinter::Base
  transform DefaultTransformer

  view :normal do
    transform ViewTransformer
  end

  view :extended do
    include_view :normal
  end

  association :address, blueprint: AddressBlueprint

  view :without_address do
    exclude :address
  end
end

As per the document, I should be able to see DefaultTransformer, but I don't see it.

For now, I edited the view like below to make it work

view :without_address do
    transform DefaultTransformer

    exclude :address
  end

Environment

- OS: MacOS 15.4.1 (24E263)
- Browser Name and version: Chrome
- Ruby Version: ruby-3.2.2

Anything else?

No response

@sanvijay sanvijay added the bug An issue with the system label Apr 17, 2025
@lessthanjacob
Copy link
Contributor

Hey @sanvijay! Thanks for reaching out!

Could you provide some additional information regarding the issue you're seeing (i.e. what's the output your seeing after calling .render before and after the change)?

I put together a simple example in a similar fashion, and the transformers are working as expected:

class TruncateTransformer < Blueprinter::Transformer
  def transform(hash, _object, _options)
    hash.transform_keys! { |key| key.to_s.split('').first(3).join('') }
  end
end

class DuplicateTransformer < Blueprinter::Transformer
  def transform(hash, _object, _options)
    hash.transform_keys! { |key| key.to_s + key.to_s }
  end
end

class UserBlueprint < Blueprinter::Base
  transform TruncateTransformer

  identifier :id
  field :name

  view :normal do
    transform DuplicateTransformer
  end 

  view :extended do
    include_view :normal

    field :user_access_level
  end

  view :another_view do 
    field :updated_at
  end
end

User = Struct.new(:id, :name, :user_access_level, :updated_at)

# Only runs "TruncateTransformer"
irb(main):039> UserBlueprint.render_as_hash(User.new(1, 'Jake', 3, Time.now))
=> {"id"=>1, "nam"=>"Jake"}

# Runs "TruncateTransformer" then "DuplicateTransformer"
irb(main):040> UserBlueprint.render_as_hash(User.new(1, 'Jake', 3, Time.now), view: :normal)
=> {"idid"=>1, "namnam"=>"Jake"}

# Runs "TruncateTransformer" then "DuplicateTransformer"
irb(main):041> UserBlueprint.render_as_hash(User.new(1, 'Jake', 3, Time.now), view: :extended)
=> {"idid"=>1, "namnam"=>"Jake", "useuse"=>3}

#  Only runs "TruncateTransformer" 
irb(main):042> UserBlueprint.render_as_hash(User.new(1, 'Jake', 3, Time.now), view: :another_view)
=> {"id"=>1, "nam"=>"Jake", "upd"=>2025-04-26 22:15:09.656101 -0400}
irb(main):043> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An issue with the system
Projects
None yet
Development

No branches or pull requests

2 participants