Commit ab1c0ca6 by John Doe

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

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