Skip to content

Commit

Permalink
Method to add multiple fields
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Hollinger <[email protected]>
  • Loading branch information
jhollinger committed Oct 28, 2024
1 parent 25f7964 commit af1bc20
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
8 changes: 4 additions & 4 deletions lib/blueprinter/v2/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class << self
# @api private The fully-qualified name, e.g. "MyBlueprint", or "MyBlueprint.foo.bar"
attr_accessor :blueprint_name
# @api private
attr_accessor :views, :fields, :excludes, :partials, :used_partials, :eval_mutex
attr_accessor :views, :schema, :excludes, :partials, :used_partials, :eval_mutex
end

self.views = ViewBuilder.new(self)
self.fields = {}
self.schema = {}
self.excludes = []
self.partials = {}
self.used_partials = []
Expand All @@ -35,7 +35,7 @@ class << self
# Initialize subclass
def self.inherited(subclass)
subclass.views = ViewBuilder.new(subclass)
subclass.fields = fields.transform_values(&:dup)
subclass.schema = schema.transform_values(&:dup)
subclass.excludes = []
subclass.partials = partials.dup
subclass.used_partials = []
Expand Down Expand Up @@ -114,7 +114,7 @@ def self.run_eval!
class_eval(&p)
end

excludes.each { |f| fields.delete f }
excludes.each { |f| schema.delete f }
@evaled = true
end

Expand Down
15 changes: 12 additions & 3 deletions lib/blueprinter/v2/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,23 @@ def use(*names)
# @return [Blueprinter::V2::Field]
#
def field(name, from: name, **options, &definition)
fields[name.to_sym] = Field.new(
schema[name.to_sym] = Field.new(
name: name,
from: from,
value_proc: definition,
options: options.dup
)
end

#
# Add multiple fields at once.
#
def fields(*names)
names.each do |name|
schema[name.to_sym] = Field.new(name: name, options: {})
end
end

#
# Define an association to a single object.
#
Expand All @@ -68,7 +77,7 @@ def field(name, from: name, **options, &definition)
def object(name, blueprint, from: name, view: nil, **options, &definition)
raise ArgumentError, 'The :view argument may not be used with V2 Blueprints' if view && blueprint.is_a?(V2)

fields[name.to_sym] = Association.new(
schema[name.to_sym] = Association.new(
name: name,
blueprint: blueprint,
collection: false,
Expand All @@ -92,7 +101,7 @@ def object(name, blueprint, from: name, view: nil, **options, &definition)
def collection(name, blueprint, from: name, view: nil, **options, &definition)
raise ArgumentError, 'The :view argument may not be used with V2 Blueprints' if view && blueprint.is_a?(V2)

fields[name.to_sym] = Association.new(
schema[name.to_sym] = Association.new(
name: name,
blueprint: blueprint,
collection: true,
Expand Down
6 changes: 3 additions & 3 deletions lib/blueprinter/v2/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class View
# @api private
def initialize(blueprint, name)
@name = name
@fields = blueprint.fields.select { |_, f| f.is_a? Field }
@objects = blueprint.fields.select { |_, f| f.is_a?(Association) && !f.collection }
@collections = blueprint.fields.select { |_, f| f.is_a?(Association) && f.collection }
@fields = blueprint.schema.select { |_, f| f.is_a? Field }
@objects = blueprint.schema.select { |_, f| f.is_a?(Association) && !f.collection }
@collections = blueprint.schema.select { |_, f| f.is_a?(Association) && f.collection }
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions spec/v2/fields_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@
expect(ref.fields[:foo].name).to eq :foo
expect(ref.fields[:foo].value_proc.class.name).to eq "Proc"
end

it 'should add multiple fields' do
blueprint = Class.new(Blueprinter::V2::Base) do
fields :name, :description, :status
end

ref = blueprint.reflections[:default]
expect(ref.fields[:name].class.name).to eq "Blueprinter::V2::Field"
expect(ref.fields[:name].name).to eq :name
expect(ref.fields[:name].options).to eq({})

expect(ref.fields[:description].class.name).to eq "Blueprinter::V2::Field"
expect(ref.fields[:description].name).to eq :description
expect(ref.fields[:description].options).to eq({})

expect(ref.fields[:status].class.name).to eq "Blueprinter::V2::Field"
expect(ref.fields[:status].name).to eq :status
expect(ref.fields[:status].options).to eq({})
end
end

context "associations" do
Expand Down

0 comments on commit af1bc20

Please sign in to comment.