-
Notifications
You must be signed in to change notification settings - Fork 5
Make array like classes configurable #6
Changes from 5 commits
0219143
9a3550b
9cf8c8e
b90e0d8
4b1436e
713b5ad
6ef0869
feae8b3
1b6a379
f31aa3a
d9080a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,19 @@ | |
module Blueprinter | ||
module TypeHelpers | ||
private | ||
def active_record_relation?(object) | ||
!!(defined?(ActiveRecord::Relation) && | ||
object.is_a?(ActiveRecord::Relation)) | ||
|
||
def array_like_classes | ||
[ | ||
Array, | ||
defined?(ActiveRecord::Relation) && ActiveRecord::Relation, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if it's not defined this becomes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since this is config and won't change at runtime, should we consider overriding the default getter or setter and inject this behaviour there? that way, it'll be memoized and we won't create/compute this array at runtime for each render call. |
||
*Blueprinter.configuration.array_like_classes | ||
].compact | ||
end | ||
|
||
def array_like?(object) | ||
object.is_a?(Array) || active_record_relation?(object) | ||
array_like_classes.any? do |klass| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I got rid of the |
||
object.is_a?(klass) | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,29 @@ | |
end | ||
end | ||
|
||
context 'Given passed object is array-like' do | ||
let(:blueprint) { blueprint_with_block } | ||
let(:additional_object) { OpenStruct.new(obj_hash.merge(id: 2)) } | ||
let(:obj) { Set.new([object_with_attributes, additional_object]) } | ||
|
||
context 'and is an instance of a configured array-like class' do | ||
before do | ||
Blueprinter.configure { |config| config.array_like_classes = [Set] } | ||
end | ||
after { reset_blueprinter_config! } | ||
|
||
it 'should return the expected array of hashes' do | ||
should eq('[{"id":1,"position_and_company":"Manager at Procore"},{"id":2,"position_and_company":"Manager at Procore"}]') | ||
end | ||
end | ||
|
||
context 'and is not an instance of a configured array-like class' do | ||
it 'should raise an error' do | ||
expect { blueprint.render(obj) }.to raise_error(NoMethodError) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there's not really anything else to assert here. We already have tests for that cover a hash being handled properly, and hashes are "array-like" in that they are |
||
end | ||
end | ||
end | ||
|
||
context 'Inside Rails project' do | ||
include FactoryBot::Syntax::Methods | ||
let(:obj) { create(:user) } | ||
|
@@ -383,6 +406,34 @@ def extract(association_name, object, _local_options, _options={}) | |
end | ||
end | ||
end | ||
|
||
context 'Given passed object is an instance of a configured array-like class' do | ||
let(:blueprint) do | ||
Class.new(Blueprinter::Base) do | ||
identifier :id | ||
fields :make | ||
end | ||
end | ||
let(:vehicle1) { build(:vehicle, id: 1) } | ||
let(:vehicle2) { build(:vehicle, id: 2, make: 'Mediocre Car') } | ||
let(:vehicle3) { build(:vehicle, id: 3, make: 'Terrible Car') } | ||
let(:vehicles) { [vehicle1, vehicle2, vehicle3] } | ||
let(:obj) { Set.new(vehicles) } | ||
let(:result) do | ||
vehicles_json = vehicles.map do |vehicle| | ||
"{\"id\":#{vehicle.id},\"make\":\"#{vehicle.make}\"}" | ||
end.join(',') | ||
"[#{vehicles_json}]" | ||
end | ||
|
||
before do | ||
Blueprinter.configure do |config| | ||
config.array_like_classes = [Set] | ||
end | ||
end | ||
|
||
it('returns the expected result') { should eq(result) } | ||
end | ||
end | ||
end | ||
describe '::render_as_hash' do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you pls create a changelog entry and bump the patch version?