Commit a1988d29 by John Doe

added home views

parent 5d4a5f51
...@@ -7,6 +7,16 @@ class DataViewsController < ApplicationController ...@@ -7,6 +7,16 @@ class DataViewsController < ApplicationController
@data_views = DataView.find_viewable(current_user) @data_views = DataView.find_viewable(current_user)
end end
# GET /data_views/home.json
def home
# return the user's home view if present
@data_view = current_user.home_data_view
if @data_view.nil?
head :not_found
return
end
end
# POST /data_views.json # POST /data_views.json
def create def create
@service = CreateDataView.new() @service = CreateDataView.new()
......
...@@ -32,6 +32,16 @@ class DbElementsController < ApplicationController ...@@ -32,6 +32,16 @@ class DbElementsController < ApplicationController
@service.run(req_elements, start_time, end_time) @service.run(req_elements, start_time, end_time)
@start_time = @service.start_time @start_time = @service.start_time
@end_time = @service.end_time @end_time = @service.end_time
# update the user's home view if a data view instance is provided
if params[:redux_json]!=nil
service = CreateDataView.new()
service.run({redux_json: params[:redux_json]},
nil, current_user, home_view=true)
unless service.success?
Rails.logger.warn("error creating home view for #{current_user.id}")
end
end
render status: @service.success? ? :ok : :unprocessable_entity render status: @service.success? ? :ok : :unprocessable_entity
end end
end end
...@@ -11,6 +11,8 @@ class User < ActiveRecord::Base ...@@ -11,6 +11,8 @@ class User < ActiveRecord::Base
has_many :memberships has_many :memberships
has_many :user_groups, through: :memberships has_many :user_groups, through: :memberships
has_many :data_views has_many :data_views
belongs_to :home_data_view, class_name: "DataView",
foreign_key: "home_data_view_id", dependent: :destroy
#---Validations----- #---Validations-----
validates :first_name, :last_name, :email, :presence => true validates :first_name, :last_name, :email, :presence => true
......
...@@ -5,9 +5,21 @@ class CreateDataView ...@@ -5,9 +5,21 @@ class CreateDataView
include ServiceStatus include ServiceStatus
attr_reader :data_view attr_reader :data_view
def run(data_view_params, stream_ids, user) def run(data_view_params, stream_ids, user, home_view=false)
#retrieve nilms for every stream if home_view
@data_view = DataView.create(
data_view_params.merge(
{name: 'home_view', visibility: 'hidden', owner: user}))
user.home_data_view.destroy unless user.home_data_view.nil?
user.update(home_data_view: @data_view)
return self
end
# normal data view
@data_view = DataView.new(data_view_params.merge({owner: user}))
# build nilm associations for permissions
begin begin
#retrieve nilms for every stream in this view
db_ids = DbStream db_ids = DbStream
.find(stream_ids) .find(stream_ids)
.pluck(:db_id) .pluck(:db_id)
...@@ -19,14 +31,11 @@ class CreateDataView ...@@ -19,14 +31,11 @@ class CreateDataView
self.add_errors(['invalid stream_ids']) self.add_errors(['invalid stream_ids'])
return self return self
end end
@data_view = DataView.create(data_view_params)
@data_view.nilm_ids = nilm_ids
@data_view.owner = user
unless @data_view.save unless @data_view.save
self.add_errors(@data_view.errors.full_messages) self.add_errors(@data_view.errors.full_messages)
return self return self
end end
@data_view.nilm_ids = nilm_ids
self.set_notice('created data view') self.set_notice('created data view')
self self
end end
......
json.extract! @data_view, *DataView.json_keys
json.owner current_user==@data_view.owner
...@@ -5,7 +5,11 @@ Rails.application.routes.draw do ...@@ -5,7 +5,11 @@ Rails.application.routes.draw do
put 'refresh' put 'refresh'
end end
end end
resources :data_views resources :data_views do
collection do
get 'home' #retrieve a user's home data view
end
end
resources :dbs, only: [:show, :update] resources :dbs, only: [:show, :update]
resources :db_folders, only: [:show, :update] resources :db_folders, only: [:show, :update]
resources :db_streams, only: [:update] do resources :db_streams, only: [:update] do
......
class AddHomeDataViewToUser < ActiveRecord::Migration[5.1]
def change
add_column :users, :home_data_view_id, :number
end
end
class ChangeKeyToInteger < ActiveRecord::Migration[5.1]
def change
change_column :users, :home_data_view_id, :integer
end
end
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170524005258) do ActiveRecord::Schema.define(version: 20170525005625) do
create_table "data_views", force: :cascade do |t| create_table "data_views", force: :cascade do |t|
t.integer "user_id" t.integer "user_id"
...@@ -166,6 +166,7 @@ ActiveRecord::Schema.define(version: 20170524005258) do ...@@ -166,6 +166,7 @@ ActiveRecord::Schema.define(version: 20170524005258) do
t.integer "invited_by_id" t.integer "invited_by_id"
t.string "invited_by_type" t.string "invited_by_type"
t.string "invitation_url" t.string "invitation_url"
t.integer "home_data_view_id"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true t.index ["email"], name: "index_users_on_email", unique: true
t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true
......
...@@ -45,6 +45,37 @@ RSpec.describe DataViewsController, type: :request do ...@@ -45,6 +45,37 @@ RSpec.describe DataViewsController, type: :request do
end end
end end
describe 'GET home' do
context 'with authenticated user' do
it 'returns user home data view' do
user1 = create(:user)
dv1 = create(:data_view)
user1.update(home_data_view: dv1)
user2 = create(:user)
dv2 = create(:data_view)
user2.update(home_data_view: dv2)
#user1 gets his view
@auth_headers = user1.create_new_auth_token
get "/data_views/home.json", headers: @auth_headers
expect(response).to have_http_status(:ok)
body = JSON.parse(response.body)
expect(body["id"]).to eq dv1.id
#user2 gets his view
@auth_headers = user2.create_new_auth_token
get "/data_views/home.json", headers: @auth_headers
expect(response).to have_http_status(:ok)
body = JSON.parse(response.body)
expect(body["id"]).to eq dv2.id
end
it 'returns 404:not_found if data view is unset' do
user = create(:user)
@auth_headers = user.create_new_auth_token
get "/data_views/home.json", headers: @auth_headers
expect(response).to have_http_status(:not_found)
expect(response.body).to be_empty
end
end
end
describe 'POST create' do describe 'POST create' do
context 'with authenticated user' do context 'with authenticated user' do
it 'creates a dataview' do it 'creates a dataview' do
......
...@@ -17,7 +17,7 @@ RSpec.describe DbElementsController, type: :request do ...@@ -17,7 +17,7 @@ RSpec.describe DbElementsController, type: :request do
stream.db_elements << @elem1 stream.db_elements << @elem1
stream.db_elements << @elem2 stream.db_elements << @elem2
end end
it 'returns elements with data' do it "returns elements with data and updates the user's home view" do
@service_data = [{ id: @elem1.id, data: 'mock1' }, @service_data = [{ id: @elem1.id, data: 'mock1' },
...@@ -33,12 +33,14 @@ RSpec.describe DbElementsController, type: :request do ...@@ -33,12 +33,14 @@ RSpec.describe DbElementsController, type: :request do
@auth_headers = user1.create_new_auth_token @auth_headers = user1.create_new_auth_token
get '/db_elements/data.json', get '/db_elements/data.json',
params: { elements: [@elem1.id, @elem2.id].to_json, params: { elements: [@elem1.id, @elem2.id].to_json,
start_time: 0, end_time: 100 }, start_time: 0, end_time: 100, redux_json: 'new home view' },
headers: @auth_headers headers: @auth_headers
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
# check to make sure JSON renders the elements # check to make sure JSON renders the elements
body = JSON.parse(response.body) body = JSON.parse(response.body)
expect(body['data'].count).to eq(2) expect(body['data'].count).to eq(2)
# check the user home view
expect(user1.reload.home_data_view.redux_json).to eq('new home view')
end end
it 'returns error if time bounds are invalid' do it 'returns error if time bounds are invalid' do
@auth_headers = user1.create_new_auth_token @auth_headers = user1.create_new_auth_token
......
...@@ -7,6 +7,7 @@ RSpec.describe User, type: :model do ...@@ -7,6 +7,7 @@ RSpec.describe User, type: :model do
specify { expect(user).to respond_to(:first_name) } specify { expect(user).to respond_to(:first_name) }
specify { expect(user).to respond_to(:last_name) } specify { expect(user).to respond_to(:last_name) }
specify { expect(user).to respond_to(:email) } specify { expect(user).to respond_to(:email) }
specify { expect(user).to respond_to(:home_data_view)}
end end
it 'responds to name' do it 'responds to name' do
......
...@@ -10,22 +10,41 @@ describe 'CreateDataView service' do ...@@ -10,22 +10,41 @@ describe 'CreateDataView service' do
create(:db_stream, db: db), create(:db_stream, db: db),
create(:db_stream, db: db)]} create(:db_stream, db: db)]}
it 'creates a dataview' do describe 'successfully' do
params = { before do
@params = {
name: 'test', name: 'test',
visibility: 'public', visibility: 'public',
description: '', description: '',
image: '', image: '',
redux_json: ''} redux_json: ''}
stream_ids = viewed_streams.map {|x| x.id} @stream_ids = viewed_streams.map {|x| x.id}
service = CreateDataView.new @service = CreateDataView.new
service.run(params, stream_ids, viewer) end
expect(service.success?).to be true it 'creates normal data views' do
@service.run(@params, @stream_ids, viewer)
expect(@service.success?).to be true
expect(DataView.count).to eq(1) expect(DataView.count).to eq(1)
expect(nilm.data_views.count).to eq(1) expect(nilm.data_views.count).to eq(1)
expect(viewer.data_views.count).to eq(1) expect(viewer.data_views.count).to eq(1)
end end
it 'creates user home views' do
#new home view replaces the previous one
viewer.update(home_data_view: create(:data_view))
expect(DataView.count).to eq(1)
#now add the new one
@service.run(@params, @stream_ids, viewer, home_view=true)
expect(@service.success?).to be true
#previous view should be deleted
expect(DataView.count).to eq(1)
view = DataView.first
expect(view.visibility).to eq 'hidden'
expect(viewer.home_data_view).to eq(view)
end
end
it 'returns error if dataview is not valid' do it 'returns error if dataview is not valid' do
params = {description: 'missing name'} params = {description: 'missing name'}
stream_ids = viewed_streams.map {|x| x.id} stream_ids = viewed_streams.map {|x| x.id}
......
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