Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/has_attachments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def has_attachments(options={})
writeable: true,
setter: ->(req) {
req[:doc]['images'].each do |img|
self.images.attach(img['signed_id'])
unless self.images.detect{|i| i.signed_id == img['signed_id'] }
self.images.attach(img['signed_id'])
end
end
},
getter: ->(*) {
Expand Down
18 changes: 17 additions & 1 deletion spec/representers/api/v1/exercises/representer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ module Api::V1::Exercises
end

context 'images' do
it 'are included' do
before(:each) do
exercise.images.attach(io: File.open(Rails.root.join('public', 'favicon.ico')), filename: 'favicon.ico', content_type: 'image/jpeg')
end
it 'are included' do
expect(representation).to(
including(
'images' => [a_hash_including(
Expand All @@ -107,6 +109,20 @@ module Api::V1::Exercises
)
end

it 'only attaches when not already attached' do
expect {
# attempt to attach an image that is already attached
representer = described_class.new(exercise)
expect {
representer.from_hash('images' => [
{ 'signed_id' => exercise.images[0].signed_id },
{ 'signed_id' => exercise.images[0].signed_id },
]
)
exercise.save
}.not_to change { exercise.images.count }
end

end

context 'questions' do
Expand Down