Change Device#update to deal with webcam_url

pull/269/head
Rick Carlino 2016-09-14 21:16:03 -05:00
parent c46cca1ab3
commit c324be9fd5
7 changed files with 45 additions and 34 deletions

View File

@ -12,14 +12,14 @@ module Api
# POST /api/device
def create
mutate Devices::Create.run(device_params, user: current_user)
mutate Devices::Create.run(params, user: current_user)
end
# PATCH/PUT /api/device
def update
# Because of the way bots are shared, there is no true 'update' action.
# Just a creation/reasignment of bots based on UUID / Token.
create
current_device
.if_null { create }
.if_not_null { mutate Devices::Update.run(params, device: current_device) }
end
# DELETE /api/devices/1

View File

@ -9,18 +9,18 @@ module Devices
optional do
string :uuid
string :name
string :webcam_url
end
def execute
merge_default_values
device = Device.new(inputs.except(:user))
ActiveRecord::Base.transaction do
device.update_attributes!(inputs.except(:user))
old_device = user.device
user.update_attributes!(device_id: device.id)
if device.users.count < 1
old_device.destroy! # Clean up "orphan" devices.
end
old_device = user.device # Ref. to old device
device.save! # Create the new one.
user.update_attributes!(device_id: device.id) # Detach from old one.
# Remove userless devices.
old_device.destroy! if old_device && device.users.count < 1
end
device
@ -31,9 +31,5 @@ module Devices
inputs[:uuid] ||= SecureRandom.uuid
inputs[:name] ||= Haikunator.haikunate(9999)
end
def device
@device ||= Device.find_by(uuid: uuid) || Device.new(uuid: uuid)
end
end
end

View File

@ -0,0 +1,16 @@
module Devices
class Update < Mutations::Command
required do
model :device, class: Device
end
optional do
string :name
string :webcam_url
end
def execute
device.update_attributes inputs.except(:device)
end
end
end

View File

@ -19,15 +19,5 @@ describe Api::DevicesController do
expect(user.device).to eq(new_device)
expect(response.status).to eq(200)
end
it 'shares devices between two users' do
bot = user.device
sign_in user2
params = {name: 'QQQ', uuid: bot.uuid}
post :create, params
expect(user.reload.device.reload.name).to eq("QQQ")
expect(user2.reload.device.reload.name).to eq("QQQ")
expect(user.device_id).to eq(user2.device_id)
end
end
end

View File

@ -32,14 +32,5 @@ describe Api::DevicesController do
expect(user.device['name'].length).to be > 4 # Haikunator
expect(user.device['uuid']).to eq('1')
end
it 'shares devices between two users' do
bot = user.device
sign_in user2
post :update, { name: 'Frank', uuid: bot.uuid }
user.reload
user2.reload
expect(user.device.id).to eq(user2.device.id)
end
end
end

View File

@ -5,5 +5,6 @@ FactoryGirl.define do
factory :device do
name { Faker::Internet.user_name }
uuid { SecureRandom.uuid }
webcam_url { Faker::Company.logo }
end
end

View File

@ -0,0 +1,17 @@
require 'spec_helper'
describe Devices::Update do
let(:user) { FactoryGirl.create(:user) }
let(:device) { user.device }
it 'updates an existing device' do
previous_name = device.name
previous_webcam_url = device.webcam_url
p = { webcam_url: "http://myfeed.com/feed.jpeg", name: "Feedy McFeedington" }
Devices::Update.run!({device: device}, p)
device.reload
expect(device.name).to eq(p[:name])
expect(device.webcam_url).to eq(p[:webcam_url])
end
end