Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
wattsworth
/
lumen-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
a1988d29
authored
May 25, 2017
by
John Doe
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
added home views
parent
5d4a5f51
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
126 additions
and
25 deletions
app/controllers/data_views_controller.rb
app/controllers/db_elements_controller.rb
app/models/user.rb
app/services/data_view/create_data_view.rb
app/views/data_views/home.json.jbuilder
config/routes.rb
db/migrate/20170525004328_add_home_data_view_to_user.rb
db/migrate/20170525005625_change_key_to_integer.rb
db/schema.rb
spec/controllers/data_views_controller_spec.rb
spec/controllers/db_elements_controller_spec.rb
spec/models/user_spec.rb
spec/services/data_view/create_data_view_spec.rb
app/controllers/data_views_controller.rb
View file @
a1988d29
...
...
@@ -7,6 +7,16 @@ class DataViewsController < ApplicationController
@data_views
=
DataView
.
find_viewable
(
current_user
)
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
def
create
@service
=
CreateDataView
.
new
()
...
...
app/controllers/db_elements_controller.rb
View file @
a1988d29
...
...
@@ -32,6 +32,16 @@ class DbElementsController < ApplicationController
@service
.
run
(
req_elements
,
start_time
,
end_time
)
@start_time
=
@service
.
start_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
end
end
app/models/user.rb
View file @
a1988d29
...
...
@@ -11,7 +11,9 @@ class User < ActiveRecord::Base
has_many
:memberships
has_many
:user_groups
,
through: :memberships
has_many
:data_views
belongs_to
:home_data_view
,
class_name:
"DataView"
,
foreign_key:
"home_data_view_id"
,
dependent: :destroy
#---Validations-----
validates
:first_name
,
:last_name
,
:email
,
:presence
=>
true
validates
:email
,
:uniqueness
=>
true
...
...
app/services/data_view/create_data_view.rb
View file @
a1988d29
...
...
@@ -5,9 +5,21 @@ class CreateDataView
include
ServiceStatus
attr_reader
:data_view
def
run
(
data_view_params
,
stream_ids
,
user
)
#retrieve nilms for every stream
def
run
(
data_view_params
,
stream_ids
,
user
,
home_view
=
false
)
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
#retrieve nilms for every stream in this view
db_ids
=
DbStream
.
find
(
stream_ids
)
.
pluck
(
:db_id
)
...
...
@@ -19,14 +31,11 @@ class CreateDataView
self
.
add_errors
([
'invalid stream_ids'
])
return
self
end
@data_view
=
DataView
.
create
(
data_view_params
)
@data_view
.
nilm_ids
=
nilm_ids
@data_view
.
owner
=
user
unless
@data_view
.
save
self
.
add_errors
(
@data_view
.
errors
.
full_messages
)
return
self
end
@data_view
.
nilm_ids
=
nilm_ids
self
.
set_notice
(
'created data view'
)
self
end
...
...
app/views/data_views/home.json.jbuilder
0 → 100644
View file @
a1988d29
json.extract! @data_view, *DataView.json_keys
json.owner current_user==@data_view.owner
config/routes.rb
View file @
a1988d29
...
...
@@ -5,7 +5,11 @@ Rails.application.routes.draw do
put
'refresh'
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
:db_folders
,
only:
[
:show
,
:update
]
resources
:db_streams
,
only:
[
:update
]
do
...
...
db/migrate/20170525004328_add_home_data_view_to_user.rb
0 → 100644
View file @
a1988d29
class
AddHomeDataViewToUser
<
ActiveRecord
::
Migration
[
5.1
]
def
change
add_column
:users
,
:home_data_view_id
,
:number
end
end
db/migrate/20170525005625_change_key_to_integer.rb
0 → 100644
View file @
a1988d29
class
ChangeKeyToInteger
<
ActiveRecord
::
Migration
[
5.1
]
def
change
change_column
:users
,
:home_data_view_id
,
:integer
end
end
db/schema.rb
View file @
a1988d29
...
...
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
2017052
4005258
)
do
ActiveRecord
::
Schema
.
define
(
version:
2017052
5005625
)
do
create_table
"data_views"
,
force: :cascade
do
|
t
|
t
.
integer
"user_id"
...
...
@@ -166,6 +166,7 @@ ActiveRecord::Schema.define(version: 20170524005258) do
t
.
integer
"invited_by_id"
t
.
string
"invited_by_type"
t
.
string
"invitation_url"
t
.
integer
"home_data_view_id"
t
.
index
[
"confirmation_token"
],
name:
"index_users_on_confirmation_token"
,
unique:
true
t
.
index
[
"email"
],
name:
"index_users_on_email"
,
unique:
true
t
.
index
[
"invitation_token"
],
name:
"index_users_on_invitation_token"
,
unique:
true
...
...
spec/controllers/data_views_controller_spec.rb
View file @
a1988d29
...
...
@@ -45,6 +45,37 @@ RSpec.describe DataViewsController, type: :request do
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
context
'with authenticated user'
do
it
'creates a dataview'
do
...
...
spec/controllers/db_elements_controller_spec.rb
View file @
a1988d29
...
...
@@ -17,7 +17,7 @@ RSpec.describe DbElementsController, type: :request do
stream
.
db_elements
<<
@elem1
stream
.
db_elements
<<
@elem2
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'
},
...
...
@@ -33,12 +33,14 @@ RSpec.describe DbElementsController, type: :request do
@auth_headers
=
user1
.
create_new_auth_token
get
'/db_elements/data.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
expect
(
response
).
to
have_http_status
(
:ok
)
# check to make sure JSON renders the elements
body
=
JSON
.
parse
(
response
.
body
)
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
it
'returns error if time bounds are invalid'
do
@auth_headers
=
user1
.
create_new_auth_token
...
...
spec/models/user_spec.rb
View file @
a1988d29
...
...
@@ -7,6 +7,7 @@ RSpec.describe User, type: :model do
specify
{
expect
(
user
).
to
respond_to
(
:first_name
)
}
specify
{
expect
(
user
).
to
respond_to
(
:last_name
)
}
specify
{
expect
(
user
).
to
respond_to
(
:email
)
}
specify
{
expect
(
user
).
to
respond_to
(
:home_data_view
)}
end
it
'responds to name'
do
...
...
spec/services/data_view/create_data_view_spec.rb
View file @
a1988d29
...
...
@@ -10,20 +10,39 @@ describe 'CreateDataView service' do
create
(
:db_stream
,
db:
db
),
create
(
:db_stream
,
db:
db
)]}
it
'creates a dataview'
do
params
=
{
name:
'test'
,
visibility:
'public'
,
description:
''
,
image:
''
,
redux_json:
''
}
stream_ids
=
viewed_streams
.
map
{
|
x
|
x
.
id
}
service
=
CreateDataView
.
new
service
.
run
(
params
,
stream_ids
,
viewer
)
expect
(
service
.
success?
).
to
be
true
expect
(
DataView
.
count
).
to
eq
(
1
)
expect
(
nilm
.
data_views
.
count
).
to
eq
(
1
)
expect
(
viewer
.
data_views
.
count
).
to
eq
(
1
)
describe
'successfully'
do
before
do
@params
=
{
name:
'test'
,
visibility:
'public'
,
description:
''
,
image:
''
,
redux_json:
''
}
@stream_ids
=
viewed_streams
.
map
{
|
x
|
x
.
id
}
@service
=
CreateDataView
.
new
end
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
(
nilm
.
data_views
.
count
).
to
eq
(
1
)
expect
(
viewer
.
data_views
.
count
).
to
eq
(
1
)
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment