Skip to content

Commit

Permalink
Merge pull request #54 from DigitalNZ/ed/reposition-stpry-items
Browse files Browse the repository at this point in the history
REPOSITION STORY ITEMS
  • Loading branch information
Edwin Rozario authored Jun 17, 2021
2 parents 7bd9c9a + 90c96b5 commit 1525db4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 37 deletions.
17 changes: 17 additions & 0 deletions lib/supplejack/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ def destroy
end
end

# Executes a POST request to the API with the Story ID, the user's api_key &
# an Array of Hashes for item positions ex: [{ id: 'storyitemid', position: 100 }]
#
# @return [ true, false ] True if the API response was successful, false if not.
#
def reposition_items(positions)
self.class.post("/stories/#{id}/reposition_items", { user_key: api_key }, items: positions)

Rails.cache.delete("/users/#{api_key}/stories") if Supplejack.enable_caching

true
rescue StandardError => e
self.errors = e.message

false
end

# Fetches the Story information from the API again, in case it had changed.
#
# This can be useful if they items for a Story changed and you want the relation
Expand Down
12 changes: 0 additions & 12 deletions lib/supplejack/story_item_relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,6 @@ def create(attributes = {})
story_item.save
end

def move_item(item_id, position)
response = post("/stories/#{story.id}/items/#{item_id}/moves", { api_key: story.api_key, user_key: story.api_key }, item_id: item_id, position: position)

build_items(response)

true
rescue StandardError => e
@errors = e.inspect

false
end

def find(id)
@items.detect { |i| i.id.to_s == id.to_s }
end
Expand Down
25 changes: 0 additions & 25 deletions spec/supplejack/story_item_relation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,6 @@ module Supplejack
end
end

describe '#move_item' do
let(:item) { relation.all.first }

before do
expect(relation).to receive(:post).with(
"/stories/#{supplejack_story.id}/items/#{item.id}/moves",
{ api_key: 'foobar', user_key: 'foobar' },
item_id: 1, position: 2
).and_return([
{ id: 2, type: 'embed', sub_type: 'supplejack_user', position: 1 },
{ id: 1, type: 'embed', sub_type: 'supplejack_user', position: 2 }
])
end

it 'calls the api move item endpoint with the new position' do
relation.move_item(item.id, 2)
end

it 'updates the items with the response' do
relation.move_item(item.id, 2)

expect(relation.all.first.id).to eq(2)
end
end

context 'items array behaviour' do
it 'executes array methods on the @items array' do
expect(relation.any? { |x| x.id == 1 }).to eq(true)
Expand Down
12 changes: 12 additions & 0 deletions spec/supplejack/story_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,18 @@ module Supplejack
end
end

describe '#reposition_items' do
let(:user) { { api_key: 'foobar' } }
let(:story) { Supplejack::Story.new({ name: 'Story Name', description: 'desc', user: user, id: '123' }) }
let(:reposition_attributes) { [{ id: '111', position: 1 }, { id: '112', position: 2 }] }

it 'triggers a POST request to reposition_items' do
expect(Supplejack::Story).to receive(:post).with('/stories/123/reposition_items', { user_key: user[:api_key] }, items: reposition_attributes)

story.reposition_items(reposition_attributes)
end
end

describe '#update_attributes' do
let(:story) { Supplejack::Story.new(name: 'test', description: 'test') }

Expand Down

0 comments on commit 1525db4

Please sign in to comment.