|
55 | 55 | end |
56 | 56 | end |
57 | 57 |
|
| 58 | + context 'when using Symbol#to_proc syntax' do |
| 59 | + let(:obj) { object_with_attributes } |
| 60 | + |
| 61 | + context 'with field using &:method_name' do |
| 62 | + let(:blueprint) do |
| 63 | + Class.new(Blueprinter::Base) do |
| 64 | + identifier :id |
| 65 | + field :name, &:first_name |
| 66 | + field :job_position, &:position |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + it 'renders the field by calling the method on the object' do |
| 71 | + result = JSON.parse(blueprint.render(obj)) |
| 72 | + expect(result['id']).to eq(1) |
| 73 | + expect(result['name']).to eq('Meg') |
| 74 | + expect(result['job_position']).to eq('Manager') |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + context 'with identifier using &:method_name' do |
| 79 | + let(:blueprint) do |
| 80 | + Class.new(Blueprinter::Base) do |
| 81 | + identifier :user_id, &:id |
| 82 | + field :first_name |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + it 'renders the identifier using Symbol#to_proc' do |
| 87 | + expect(blueprint.render(obj)).to eq('{"user_id":1,"first_name":"Meg"}') |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + context 'when mixing Symbol#to_proc and regular blocks' do |
| 92 | + let(:blueprint) do |
| 93 | + Class.new(Blueprinter::Base) do |
| 94 | + identifier :id |
| 95 | + field :name, &:first_name |
| 96 | + field :full_name do |obj| |
| 97 | + "#{obj.first_name} #{obj.last_name}" |
| 98 | + end |
| 99 | + field :info do |obj, options| |
| 100 | + "#{obj.position} (options: #{options.inspect})" |
| 101 | + end |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + it 'handles both block types correctly' do |
| 106 | + result = JSON.parse(blueprint.render(obj)) |
| 107 | + expect(result['id']).to eq(1) |
| 108 | + expect(result['name']).to eq('Meg') |
| 109 | + expect(result['full_name']).to eq('Meg Ryan') |
| 110 | + expect(result['info']).to include('Manager') |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | + |
58 | 115 | context 'Outside Rails project' do |
59 | 116 | context 'Given passed object has dot notation accessible attributes' do |
60 | 117 | let(:obj) { object_with_attributes } |
|
0 commit comments