Finished device creation and device controlller

pull/45/head
rick carlino 2014-05-19 06:56:41 -07:00
parent 3feb95db9a
commit 3a10634863
5 changed files with 50 additions and 45 deletions

View File

@ -2,13 +2,17 @@ app = angular.module('FarmBot')
controller = ($scope, Restangular, Device) ->
$scope.devices = Restangular.all('devices').getList().$object
$scope.refreshDeviceList = ->
devices.getList().then (data) ->
$scope.devices = data
$scope.device = {}
$scope.removeDevice = (device) ->
device.remove().then ->
#update the $scope var once the repsonse is OK
$scope.devices = _.without($scope.products, product);
$scope.devices = _.without($scope.devices, device);
$scope.createDevice = ->
$scope.devices.post($scope.device).then (data) ->
$scope.devices.push(data)
app.controller "DeviceController", [

View File

@ -1,49 +1,50 @@
class Api::DevicesController < ApplicationController
respond_to :json
before_action :ensure_logged_in
before_action :set_device, only: [:show, :edit, :update, :destroy]
# GET /api/devices
def index
@devices = Device.where(user_id: current_user.id)
render json: @devices
end
# GET /api/devices/1
def show
end
# POST /api/devices
def create
@device = Device.new(device_params)
@device.user = current_user
if @device.save
render json: @device
end
end
# PATCH/PUT /api/devices/1
def update
if @device.update(device_params)
render json: @device
end
end
# DELETE /api/devices/1
def destroy
@device = Device.find(params[:id])
ensure_device_ownership
if @device.destroy
render nothing: true, status: :unauthorized
else
render @device.errors, status: :unprocessable_entity
end
end
# def create # Not yet implemented - sit tight. Coming soon!
# @device = Device.new(device_params)
# if @device.save
# render json: @device, status: 201
# else
# render json: @device, status: 400
# end
# end
private
# def device_params
# params.require(:person).permit(:name, :age)
# end
# Handles unauthorized / unauthenticated API requests.
def ensure_logged_in
unless current_user
render nothing: true, :status => :unauthorized and return
end
end
def ensure_device_ownership
if @device.user == current_user
render nothing: true, :status => :unauthorized and return
@device.destroy
render nothing: true, status: 204
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_device
@device = Device.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def device_params
params.permit([:name, :uuid, :token])
end
end

View File

@ -1,20 +1,20 @@
.row
.large-12.columns
%h1 Manage FarmBot Device Settings {{ status }}
%form
%form{ng_submit: 'createDevice()'}
%fieldset
%legend FarmBot Settings
.row
.large-12.columns
%label Friendly Name
%input{placeholder: "Franny the FarmBot", type: "text"}/
%input{placeholder: "Franny the FarmBot", type: "text", ng_model: 'device.name'}/
.row
.large-6.columns
%label Skynet UUID
%input{placeholder: "ad698900-2546-11e3-87fb-c560cb0ca47b", type: "text"}/
%input{placeholder: "ad698900-2546-11e3-87fb-c560cb0ca47b", type: "text", ng_model: 'device.uuid'}/
.large-6.columns
%label Skynet Security Token
%input{placeholder: "4bbd2jm242dl5wmimbwz4rvlu77m0a4i", type: "text"}/
%input{placeholder: "4bbd2jm242dl5wmimbwz4rvlu77m0a4i", type: "text", ng_model: 'device.token'}/
.row
.large-12.columns
%button.button

View File

@ -29,10 +29,10 @@
- if current_user
- if current_user.devices.count > 0
%li
%a.button{href: "http://foundation.zurb.com/docs"} Send Data to Farmbot
%a.button{href: "/dashboard#/devices"} Send Data to Farmbot
- else
%li
%a.button{href: "http://foundation.zurb.com/docs"} Pair with Device
%a.button{href: '/dashboard#/devices'} Pair with Device
%li.has-dropdown.not-click
%a{href: "#"}
= "Welcome #{current_user.name}!"

View File

@ -1,7 +1,7 @@
Dss::Application.routes.draw do
namespace :api do
resources :devices, only: [:index, :destroy]
resources :devices, only: [:index, :destroy, :create]
end
devise_for :users, :controllers => {:registrations => "registrations"}