Commit ab1c0ca6 by John Doe

finished migration to jbuilder, added controller specs for all db objects

parent 35643f4b
...@@ -36,6 +36,7 @@ gem 'httparty' ...@@ -36,6 +36,7 @@ gem 'httparty'
gem 'rack-cors' gem 'rack-cors'
gem 'devise_token_auth' gem 'devise_token_auth'
gem 'omniauth' gem 'omniauth'
gem 'oj' # fast json
group :development, :test do group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console # Call 'byebug' anywhere in the code to stop execution and get a debugger console
......
...@@ -168,6 +168,7 @@ GEM ...@@ -168,6 +168,7 @@ GEM
notiffany (0.1.1) notiffany (0.1.1)
nenv (~> 0.1) nenv (~> 0.1)
shellany (~> 0.0) shellany (~> 0.0)
oj (2.18.1)
omniauth (1.3.2) omniauth (1.3.2)
hashie (>= 1.2, < 4) hashie (>= 1.2, < 4)
rack (>= 1.0, < 3) rack (>= 1.0, < 3)
...@@ -327,6 +328,7 @@ DEPENDENCIES ...@@ -327,6 +328,7 @@ DEPENDENCIES
httparty httparty
jbuilder (~> 2.0) jbuilder (~> 2.0)
jquery-rails jquery-rails
oj
omniauth omniauth
rack-cors rack-cors
rails (= 5.0.1) rails (= 5.0.1)
......
...@@ -26,7 +26,7 @@ class DbFoldersController < ApplicationController ...@@ -26,7 +26,7 @@ class DbFoldersController < ApplicationController
end end
def set_folder def set_folder
@db_folder = DbFolder.find(params[:id]) @db_folder = DbFolder.includes(:db_streams).find(params[:id])
@db = @db_folder.db @db = @db_folder.db
@nilm = @db.nilm @nilm = @db.nilm
end end
......
...@@ -24,7 +24,7 @@ class DbStreamsController < ApplicationController ...@@ -24,7 +24,7 @@ class DbStreamsController < ApplicationController
end end
def set_stream def set_stream
@db_stream = DbStream.find(params[:id]) @db_stream = DbStream.includes(:db_elements).find(params[:id])
@db = @db_stream.db @db = @db_stream.db
@nilm = @db.nilm @nilm = @db.nilm
end end
......
...@@ -7,12 +7,9 @@ class DbsController < ApplicationController ...@@ -7,12 +7,9 @@ class DbsController < ApplicationController
before_action :authorize_viewer, only: [:show] before_action :authorize_viewer, only: [:show]
before_action :authorize_owner, only: [:update] before_action :authorize_owner, only: [:update]
# GET /dbs
# GET /dbs.json # GET /dbs.json
def show def show; end
end
# PATCH/PUT /dbs/1
# PATCH/PUT /dbs/1.json # PATCH/PUT /dbs/1.json
def update def update
@service = StubService.new @service = StubService.new
...@@ -37,20 +34,23 @@ class DbsController < ApplicationController ...@@ -37,20 +34,23 @@ class DbsController < ApplicationController
def refresh def refresh
adapter = DbAdapter.new(@db.url) adapter = DbAdapter.new(@db.url)
service = UpdateDb.new(db: @db) service = UpdateDb.new(db: @db)
return service.run(adapter.dbinfo, adapter.schema) service.run(adapter.dbinfo, adapter.schema)
end end
def db_params def db_params
params.permit(:url, :max_points_per_plot) params.permit(:url, :max_points_per_plot)
end end
def set_db def set_db
@db = Db.find(params[:id]) @db = Db.find(params[:id])
@nilm = @db.nilm @nilm = @db.nilm
end end
#authorization based on nilms
# authorization based on nilms
def authorize_owner def authorize_owner
head :unauthorized unless current_user.owns_nilm?(@nilm) head :unauthorized unless current_user.owns_nilm?(@nilm)
end end
def authorize_viewer def authorize_viewer
head :unauthorized unless current_user.views_nilm?(@nilm) head :unauthorized unless current_user.views_nilm?(@nilm)
end end
......
...@@ -22,9 +22,8 @@ class Db < ApplicationRecord ...@@ -22,9 +22,8 @@ class Db < ApplicationRecord
"#{nilm.url}/nilmdb" "#{nilm.url}/nilmdb"
end end
# def as_json(options = {}) def self.json_keys
# db = super(except: [:created_at, :updated_at]) [:id, :url, :size_total, :size_db, :available,
# db[:contents] = root_folder.as_json({shallow: false}) :size_other, :version, :max_points_per_plot]
# db end
# end
end end
...@@ -7,7 +7,7 @@ class DbElement < ApplicationRecord ...@@ -7,7 +7,7 @@ class DbElement < ApplicationRecord
validates :name, presence: true validates :name, presence: true
validates :name, uniqueness: { scope: :db_stream_id, validates :name, uniqueness: { scope: :db_stream_id,
message: ' is already used in this stream'} message: ' is already used in this stream' }
validates :scale_factor, presence: true, numericality: true validates :scale_factor, presence: true, numericality: true
validates :scale_factor, presence: true, numericality: true validates :scale_factor, presence: true, numericality: true
...@@ -17,15 +17,16 @@ class DbElement < ApplicationRecord ...@@ -17,15 +17,16 @@ class DbElement < ApplicationRecord
# force set any validated params to acceptable # force set any validated params to acceptable
# default values this allows us to process corrupt databases # default values this allows us to process corrupt databases
def use_default_attributes def use_default_attributes
self.name = "element#{self.column}" self.name = "element#{column}"
self.units = "" self.units = ''
self.default_min = nil self.default_min = nil
self.default_max = nil self.default_max = nil
self.scale_factor = 1.0 self.scale_factor = 1.0
self.offset = 0.0 self.offset = 0.0
end end
def as_json(_options = {}) def self.json_keys
super(except: [:created_at, :updated_at]) [:id, :name, :units, :column, :default_max, :discrete,
:default_min, :scale_factor, :offset, :plottable]
end end
end end
...@@ -47,12 +47,8 @@ class DbFolder < ApplicationRecord ...@@ -47,12 +47,8 @@ class DbFolder < ApplicationRecord
self.description = '' self.description = ''
end end
def as_json(options = {shallow: true}) def self.json_keys
folder = super(except: [:created_at, :updated_at]) [:id, :name, :description, :path, :hidden,
if(options[:shallow]== false) :start_time, :end_time, :size_on_disk]
folder[:subfolders] = subfolders.map(&:as_json)
folder[:streams] = db_streams.includes(:db_elements,:db_decimations).map(&:as_json)
end
folder
end end
end end
...@@ -52,11 +52,10 @@ class DbStream < ApplicationRecord ...@@ -52,11 +52,10 @@ class DbStream < ApplicationRecord
self.description = '' self.description = ''
end end
def self.json_keys
def as_json(_options = {}) [:id, :name, :description, :path, :start_time,
stream = super(except: [:created_at, :updated_at]) :end_time, :size_on_disk, :total_rows, :total_time,
stream[:elements] = db_elements.map(&:as_json) :data_type, :name_abbrev, :delete_locked, :hidden]
stream[:decimations] = db_decimations.map(&:as_json)
stream
end end
end end
json.extract! db_folder, :id, :name, :description, :path, :hidden, json.extract! db_folder, *DbFolder.json_keys
:start_time, :end_time, :size_on_disk
json.shallow shallow json.subfolders(db_folder.subfolders) do |folder|
unless(shallow) json.extract! folder, *DbFolder.json_keys
json.subfolders do end
json.array! db_folder.subfolders, partial: 'db_folders/db_folder',
as: :db_folder, shallow: true json.streams(db_folder.db_streams.includes(:db_elements)) do |stream|
end json.extract! stream, *DbStream.json_keys
json.streams do json.elements(stream.db_elements) do |element|
json.array! db_folder.db_streams, partial: 'db_streams/db_stream', json.extract! element, *DbElement.json_keys
as: :db_stream
end end
end end
json.partial! "db_folders/db_folder", db_folder: @db_folder, shallow: false # frozen_string_literal: true
json.partial! 'db_folders/db_folder',
db_folder: @db_folder
json.data do json.data do
json.partial! "db_folders/db_folder", db_folder: @db_folder, shallow: false json.partial! "db_folders/db_folder", db_folder: @db_folder
end end
json.partial! "helpers/messages", service: @service json.partial! "helpers/messages", service: @service
json.extract! db_element, :id, :name, :units, :column, :default_max,
:default_min, :scale_factor, :offset, :plottable,
:discrete
# frozen_string_literal: true # frozen_string_literal: true
json.extract! db_stream, :id, :name, :description, :path, :start_time, json.extract! db_stream, *DbStream.json_keys
:end_time, :size_on_disk, :total_rows, :total_time,
:data_type, :name_abbrev, :delete_locked, :hidden
json.elements do json.elements(db_stream.db_elements) do |element|
json.array! db_stream.db_elements, json.extract! element, *DbElement.json_keys
partial: 'db_streams/db_element',
as: :db_element
end end
json.extract! db, :id, :url, :size_total, :size_db, json.extract! db, *Db.json_keys
:size_other, :version, :max_points_per_plot,
:available
json.contents do json.contents do
json.partial! "db_folders/db_folder", db_folder: @db.root_folder, root = db.root_folder
shallow: false json.extract! root, *DbFolder.json_keys
json.subfolders(root.subfolders) do |folder|
json.extract! folder, *DbFolder.json_keys
end
json.streams(root.db_streams) do |stream|
json.extract! stream, *DbStream.json_keys
json.elements(stream.db_elements) do |element|
json.extract! element, *DbElement.json_keys
end
end
end end
json.extract! nilm, :id, :name, :description, :url json.extract! nilm, :id, :name, :description, :url
json.db_id nilm.db.id
json.available nilm.db.available json.available nilm.db.available
...@@ -35,8 +35,6 @@ RSpec.describe DbFoldersController, type: :request do ...@@ -35,8 +35,6 @@ RSpec.describe DbFoldersController, type: :request do
expect(response.header['Content-Type']).to include('application/json') expect(response.header['Content-Type']).to include('application/json')
body = JSON.parse(response.body) body = JSON.parse(response.body)
expect(body['name']).to eq(folder.name) expect(body['name']).to eq(folder.name)
# contains rendered streams & subfolders (empty, but present)
expect(body['shallow']).to be(false)
end end
end end
end end
......
...@@ -97,7 +97,7 @@ RSpec.describe DbsController, type: :request do ...@@ -97,7 +97,7 @@ RSpec.describe DbsController, type: :request do
expect(john_nilm.db.max_points_per_plot).to eq(num_points+10) expect(john_nilm.db.max_points_per_plot).to eq(num_points+10)
end end
end end
context 'without admin permissions' do context 'without owner permissions' do
it 'returns unauthorized' do it 'returns unauthorized' do
@auth_headers = john.create_new_auth_token @auth_headers = john.create_new_auth_token
num_points = pete_nilm.db.max_points_per_plot num_points = pete_nilm.db.max_points_per_plot
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment