From 354b42a70c3f20358e8edb5abfc93c014bff9774 Mon Sep 17 00:00:00 2001 From: leenasn Date: Wed, 18 Nov 2015 15:15:31 +0530 Subject: [PATCH] Add API installation status update --- app/admin/app.rb | 17 ++++++++++++ app/controllers/installations_controller.rb | 14 ++++++++++ app/models/app.rb | 2 ++ app/models/device.rb | 2 ++ config/routes.rb | 2 ++ .../installations_controller_spec.rb | 27 +++++++++++++++++++ 6 files changed, 64 insertions(+) create mode 100644 app/admin/app.rb create mode 100644 app/controllers/installations_controller.rb create mode 100644 spec/controllers/installations_controller_spec.rb diff --git a/app/admin/app.rb b/app/admin/app.rb new file mode 100644 index 0000000..7ab4724 --- /dev/null +++ b/app/admin/app.rb @@ -0,0 +1,17 @@ +ActiveAdmin.register App do + permit_params :name, :package_name +# See permitted parameters documentation: +# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters +# +# permit_params :list, :of, :attributes, :on, :model +# +# or +# +# permit_params do +# permitted = [:permitted, :attributes] +# permitted << :other if resource.something? +# permitted +# end + + +end diff --git a/app/controllers/installations_controller.rb b/app/controllers/installations_controller.rb new file mode 100644 index 0000000..65937c3 --- /dev/null +++ b/app/controllers/installations_controller.rb @@ -0,0 +1,14 @@ +class InstallationsController < ApplicationController + before_action :authenticate_device + respond_to :json + + def downloaded + Installation.find(params[:id]).downloaded! + render json:{}, status: :ok + end + + def installed + Installation.find(params[:id]).installed! + render json:{}, status: :ok + end +end diff --git a/app/models/app.rb b/app/models/app.rb index abf5c39..440a469 100644 --- a/app/models/app.rb +++ b/app/models/app.rb @@ -1,5 +1,7 @@ class App < ActiveRecord::Base + default_scope {order("name")} + has_many :batch_installations, dependent: :destroy validates :name, :package_name, presence: true diff --git a/app/models/device.rb b/app/models/device.rb index c0b9cb6..36b8d7c 100644 --- a/app/models/device.rb +++ b/app/models/device.rb @@ -1,4 +1,6 @@ class Device < ActiveRecord::Base + default_scope {order ('id desc')} + enum status: [:active,:missing,:dead] attr_accessor :status diff --git a/config/routes.rb b/config/routes.rb index db1b9f9..cc71ba6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,8 @@ Rails.application.routes.draw do devise_for :admin_users, ActiveAdmin::Devise.config post 'heartbeats', to: 'heartbeats#create', :defaults => { :format => :json } + post '/installations/downloaded', :defaults => { :format => :json } + post '/installations/installed', :defaults => { :format => :json } ActiveAdmin.routes(self) # The priority is based upon order of creation: first created -> highest priority. diff --git a/spec/controllers/installations_controller_spec.rb b/spec/controllers/installations_controller_spec.rb new file mode 100644 index 0000000..3027599 --- /dev/null +++ b/spec/controllers/installations_controller_spec.rb @@ -0,0 +1,27 @@ +require 'rails_helper' + +RSpec.describe InstallationsController, type: :controller do + let(:installation) {FactoryGirl.create(:installation)} + let(:device){installation.device} + before(:each)do + request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(device.access_token) + end + + describe "POST #downloaded" do + + it "upate installation with download status" do + post :downloaded, :id => installation.id , format: :json + expect(response).to have_http_status(:ok) + expect(Installation.last.downloaded?).to be true + end + end + + describe "POST #installed" do + + it "update installation with installed status" do + post :installed, :id => installation.id , format: :json + expect(response).to have_http_status(:ok) + expect(Installation.last.installed?).to be true + end + end +end