# Good - concise
fields :id, :name, :email, :created_at
# Only use singular when needed
field :full_name do |user|
"#{user.first_name} #{user.last_name}"
end
fields :email, :name, :created_at
end
Usage
UserBlueprint.render(user) # JSON string
UserBlueprint.render_as_hash(user) # Ruby Hash
</pattern>
<pattern name="computed-field">
```ruby
field :full_name do |user|
"#{user.first_name} #{user.last_name}"
end
# With options access
field :display_name do |user, options|
options[:admin] ? user.admin_name : user.public_name
end
Usage
UserBlueprint.render(user, view: :extended)
</pattern>
<pattern name="conditional-field">
```ruby
field :salary, if: ->(field_name, user, options) { options[:show_salary] }
field :age, unless: ->(field_name, user, options) { user.hide_age? }
- Create a new blueprint
- Understand a specific concept (views, associations, transformers, etc.)
- Debug a blueprint issue
- Something else
For option 2, specify the concept and I'll load the relevant reference.
After reading, apply the knowledge to the user's specific situation.
Core Concepts:
- fields-and-identifiers.md - Fields, identifiers, computed fields, field options
- views.md - Views, include/exclude, view inheritance
- associations.md - Nested blueprints, polymorphic associations
Advanced:
- configuration.md - Global config, JSON generators, field sorting
- transformers-and-extractors.md - Custom transformers and extractors
- conditionals-and-defaults.md - Conditional fields, defaults, nil handling
Troubleshooting:
- anti-patterns.md - Common mistakes and how to fix them
1---2name: blueprint-gem3description: Work with Blueprinter gem for JSON serialization in Ruby. Covers fields, views, associations, transformers, extractors, and configuration. Use when creating, modifying, or debugging blueprints.4---56<essential_principles>78<principle name="blueprints-are-presenters">9Blueprints are JSON object presenters - they transform Ruby objects into simple hashes for JSON serialization. They are NOT models, NOT decorators. Keep them focused on presentation logic only.10</principle>1112<principle name="views-for-context">13Use views to provide different serialization contexts for the same blueprint. Don't create separate blueprint classes when views suffice. Views can include other views and exclude fields.14</principle>1516<principle name="fields-shorthand">17Use `fields` (plural) for multiple simple attributes. Use `field` (singular) only when you need a block, renaming, or options. This keeps blueprints concise.1819```ruby20# Good - concise21fields :id, :name, :email, :created_at2223# Only use singular when needed24field :full_name do |user|25 "#{user.first_name} #{user.last_name}"26end27```28</principle>2930<principle name="identifier-always-renders">31`identifier` fields always render in every view (they have their own implicit `:identifier` view). Use for primary keys and unique identifiers only.32</principle>3334</essential_principles>3536<quick_reference>3738<pattern name="basic-blueprint">39```ruby40class UserBlueprint < Blueprinter::Base41 identifier :id4243 fields :email, :name, :created_at44end4546# Usage47UserBlueprint.render(user) # JSON string48UserBlueprint.render_as_hash(user) # Ruby Hash49```50</pattern>5152<pattern name="computed-field">53```ruby54field :full_name do |user|55 "#{user.first_name} #{user.last_name}"56end5758# With options access59field :display_name do |user, options|60 options[:admin] ? user.admin_name : user.public_name61end62```63</pattern>6465<pattern name="association">66```ruby67association :company, blueprint: CompanyBlueprint68association :posts, blueprint: PostBlueprint, view: :summary69```70</pattern>7172<pattern name="view">73```ruby74view :extended do75 fields :phone, :address76 association :orders, blueprint: OrderBlueprint77end7879# Usage80UserBlueprint.render(user, view: :extended)81```82</pattern>8384<pattern name="conditional-field">85```ruby86field :salary, if: ->(field_name, user, options) { options[:show_salary] }87field :age, unless: ->(field_name, user, options) { user.hide_age? }88```89</pattern>9091</quick_reference>9293<intake>94What do you need help with?95961. Create a new blueprint972. Understand a specific concept (views, associations, transformers, etc.)983. Debug a blueprint issue994. Something else100101**For option 2, specify the concept and I'll load the relevant reference.**102</intake>103104<routing>105| Response | Action |106|----------|--------|107| 1, "create", "new" | Read `workflows/create-blueprint.md` |108| 2, "fields", "identifier" | Read `references/fields-and-identifiers.md` |109| 2, "views", "view" | Read `references/views.md` |110| 2, "association", "nested" | Read `references/associations.md` |111| 2, "config", "configure", "setup" | Read `references/configuration.md` |112| 2, "transformer", "extractor" | Read `references/transformers-and-extractors.md` |113| 2, "conditional", "default", "if", "unless" | Read `references/conditionals-and-defaults.md` |114| 3, "debug", "issue", "problem" | Read `references/anti-patterns.md` first |115| 4, other | Clarify need, then select appropriate reference |116117**After reading, apply the knowledge to the user's specific situation.**118</routing>119120<reference_index>121122**Core Concepts:**123- fields-and-identifiers.md - Fields, identifiers, computed fields, field options124- views.md - Views, include/exclude, view inheritance125- associations.md - Nested blueprints, polymorphic associations126127**Advanced:**128- configuration.md - Global config, JSON generators, field sorting129- transformers-and-extractors.md - Custom transformers and extractors130- conditionals-and-defaults.md - Conditional fields, defaults, nil handling131132**Troubleshooting:**133- anti-patterns.md - Common mistakes and how to fix them134135</reference_index>136137<workflows_index>138| Workflow | Purpose |139|----------|---------|140| create-blueprint.md | Create a new blueprint with proper structure |141</workflows_index>