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
ff791f4b
authored
Feb 26, 2017
by
John Doe
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
adjusted json rendering for user groups
parent
8779108b
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
125 additions
and
12 deletions
app/adapters/db_adapter.rb
app/controllers/user_groups_controller.rb
app/views/user_groups/index.json.jbuilder
db/seeds.rb
spec/controllers/user_groups_controller_spec.rb
spec/factories/nilms.rb
spec/factories/user_groups.rb
spec/factories/users.rb
app/adapters/db_adapter.rb
View file @
ff791f4b
...
...
@@ -3,6 +3,7 @@
# Wrapper around NilmDB HTTP service
class
DbAdapter
include
HTTParty
default_timeout
10
def
initialize
(
url
)
@url
=
url
...
...
app/controllers/user_groups_controller.rb
View file @
ff791f4b
...
...
@@ -5,7 +5,10 @@ class UserGroupsController < ApplicationController
# GET /user_groups.json
def
index
@user_groups
=
UserGroup
.
all
@owned_groups
=
UserGroup
.
where
(
owner:
current_user
)
@member_groups
=
current_user
.
user_groups
my_groups
=
@member_groups
+
@owned_groups
@other_groups
=
UserGroup
.
where
.
not
(
id:
my_groups
.
pluck
(
:id
))
end
# POST /user_groups.json
...
...
app/views/user_groups/index.json.jbuilder
View file @
ff791f4b
json.array! @user_groups do |group|
json.extract! group, *UserGroup.json_keys
json.owner do
json.array! @owned_groups do |group|
json.extract! group, *UserGroup.json_keys
json.members group.users do |user|
json.extract! user, *User.json_keys
end
end
end
json.member do
json.array! @member_groups do |group|
json.extract! group, *UserGroup.json_keys
end
end
json.other do
json.array! @other_groups do |group|
json.extract! group, *UserGroup.json_keys
end
end
db/seeds.rb
View file @
ff791f4b
# frozen_string_literal: true
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
...
...
@@ -5,3 +6,49 @@
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
require
'factory_girl_rails'
# config.include FactoryGirl::Syntax::Methods
DatabaseCleaner
.
strategy
=
:truncation
DatabaseCleaner
.
clean
# create named users
def
create_user
(
first_name
,
last_name
)
FactoryGirl
.
create
(
:confirmed_user
,
first_name:
first_name
,
last_name:
last_name
)
end
john
=
create_user
(
'John'
,
'Donnal'
)
john
.
email
=
'jdonnal@gmail.com'
john
.
password
=
'changeme'
john
.
save!
nicky
=
create_user
(
'Nicky'
,
'Donnal'
)
steve
=
create_user
(
'Steve'
,
'Leeb'
)
pete
=
create_user
(
'Pete'
,
'Lindahl'
)
greg
=
create_user
(
'Greg'
,
'Bredariol'
)
# create named groups
donnals
=
FactoryGirl
.
create
(
:user_group
,
name:
'Donnals'
,
owner:
john
,
members:
[
nicky
])
lab
=
FactoryGirl
.
create
(
:user_group
,
name:
'Lab'
,
owner:
john
,
members:
[
steve
,
pete
,
greg
])
# create real nilms
nc
=
CreateNilm
.
new
nc
.
run
(
name:
'Local'
,
url:
'http://localhost:8080'
)
home
=
nc
.
nilm
FactoryGirl
.
create
(
:permission
,
nilm:
home
,
user:
john
,
role:
'admin'
)
FactoryGirl
.
create
(
:permission
,
nilm:
home
,
user_group:
donnals
,
role:
'owner'
)
FactoryGirl
.
create
(
:permission
,
nilm:
home
,
user:
steve
,
role:
'viewer'
)
# create fake nilms
3
.
times
{
FactoryGirl
.
create
(
:nilm
,
admins:
[
john
])
}
5
.
times
{
FactoryGirl
.
create
(
:nilm
,
owners:
[
john
])
}
10
.
times
{
FactoryGirl
.
create
(
:nilm
,
viewers:
[
john
])}
# create other groups
5
.
times
do
g
=
FactoryGirl
.
create
(
:test_user_group
,
size:
rand
(
1
..
8
))
g
.
users
<<
john
#john joins every group!
end
spec/controllers/user_groups_controller_spec.rb
View file @
ff791f4b
...
...
@@ -4,22 +4,51 @@ require 'rails_helper'
RSpec
.
describe
UserGroupsController
,
type: :request
do
let
(
:grp1
)
{
create
(
:user_group
,
name:
'Group1'
)
}
let
(
:grp2
)
{
create
(
:user_group
,
name:
'Group2'
)
}
let
(
:donnals
)
{
create
(
:user_group
,
name:
'Donnals'
,
owner:
john
,
members:
[
nicky
])}
let
(
:john
)
{
create
(
:user
,
first_name:
'Jonh'
)
}
let
(
:nicky
)
{
create
(
:user
,
first_name:
'Nicky'
)}
let
(
:steve
)
{
create
(
:user
,
first_name:
'Steve'
)}
describe
'GET index'
do
before
{
john
.
confirm
}
before
do
john
.
confirm
# force lazy evaluation of let to build groups
grp1
;
grp2
;
donnals
;
end
context
'with authenticated user'
do
it
'returns user groups'
do
# force lazy evaluation of let to build groups
grp1
;
grp2
;
context
'with john'
do
it
'returns 1 owner, 0 members, 2 others'
do
@auth_headers
=
john
.
create_new_auth_token
get
"/user_groups.json"
,
headers:
@auth_headers
expect
(
response
.
header
[
'Content-Type'
]).
to
include
(
'application/json'
)
body
=
JSON
.
parse
(
response
.
body
)
expect
(
body
[
0
][
"id"
]).
to
eq
(
grp1
.
id
)
expect
(
body
[
1
][
"id"
]).
to
eq
(
grp2
.
id
)
expect
(
body
[
"owner"
].
length
).
to
eq
(
1
)
expect
(
body
[
"owner"
][
0
][
"members"
].
length
).
to
eq
(
1
)
expect
(
body
[
"member"
].
length
).
to
eq
(
0
)
expect
(
body
[
"other"
].
length
).
to
eq
(
2
)
end
end
context
'with nicky'
do
it
'returns 0 owners, 1 member, 2 others'
do
@auth_headers
=
nicky
.
create_new_auth_token
get
"/user_groups.json"
,
headers:
@auth_headers
expect
(
response
.
header
[
'Content-Type'
]).
to
include
(
'application/json'
)
body
=
JSON
.
parse
(
response
.
body
)
expect
(
body
[
"owner"
].
length
).
to
eq
(
0
)
expect
(
body
[
"member"
].
length
).
to
eq
(
1
)
expect
(
body
[
"other"
].
length
).
to
eq
(
2
)
end
end
context
'with steve'
do
it
'returns 0 owners, 0 members, 3 others'
do
@auth_headers
=
steve
.
create_new_auth_token
get
"/user_groups.json"
,
headers:
@auth_headers
expect
(
response
.
header
[
'Content-Type'
]).
to
include
(
'application/json'
)
body
=
JSON
.
parse
(
response
.
body
)
expect
(
body
[
"owner"
].
length
).
to
eq
(
0
)
expect
(
body
[
"member"
].
length
).
to
eq
(
0
)
expect
(
body
[
"other"
].
length
).
to
eq
(
3
)
end
end
context
'without sign-in'
do
...
...
spec/factories/nilms.rb
View file @
ff791f4b
...
...
@@ -5,7 +5,7 @@ FactoryGirl.define do
factory
:nilm
do
db
name
"test_nilm"
name
{
Faker
::
StarWars
.
unique
.
planet
}
description
{
Faker
::
Lorem
.
sentence
}
transient
do
admins
[]
...
...
spec/factories/user_groups.rb
View file @
ff791f4b
...
...
@@ -8,5 +8,17 @@ FactoryGirl.define do
description
{
Faker
::
Lorem
.
sentence
}
owner
{
users
.
empty?
?
create
(:
user
):
users
.
first
}
users
{
members
.
empty?
?
[
create
(:
user
)]
:
members
}
factory
:test_user_group
do
transient
do
size
[]
end
name
{
Faker
::
Company
.
unique
.
name
}
description
{
Faker
::
ChuckNorris
.
fact
}
owner
{
create
(
:confirmed_user
)
}
users
{
size
.
times
.
map
{
create
(
:confirmed_user
)
}
}
end
end
end
spec/factories/users.rb
View file @
ff791f4b
...
...
@@ -4,5 +4,11 @@ FactoryGirl.define do
last_name
{
Faker
::
Name
.
first_name
}
email
{
Faker
::
Internet
.
unique
.
email
}
password
{
Faker
::
Lorem
.
characters
(
10
)}
factory
:confirmed_user
do
confirmed_at
{
Time
.
now
}
end
end
end
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