Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
333a697b
Commit
333a697b
authored
Mar 04, 2021
by
Matija Čupić
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move callout initialization to User model
Moves callout initialization to the User model.
parent
95c919d9
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
47 additions
and
17 deletions
+47
-17
app/controllers/user_callouts_controller.rb
app/controllers/user_callouts_controller.rb
+2
-8
app/graphql/mutations/user_callouts/create.rb
app/graphql/mutations/user_callouts/create.rb
+1
-7
app/models/user.rb
app/models/user.rb
+4
-0
spec/models/user_spec.rb
spec/models/user_spec.rb
+39
-0
spec/requests/api/graphql/mutations/user_callouts/create_spec.rb
...quests/api/graphql/mutations/user_callouts/create_spec.rb
+1
-2
No files found.
app/controllers/user_callouts_controller.rb
View file @
333a697b
...
...
@@ -4,10 +4,10 @@ class UserCalloutsController < ApplicationController
feature_category
:navigation
def
create
callout
=
ensure_callout
callout
=
current_user
.
find_or_initialize_callout
(
feature_name
)
callout
.
update
(
dismissed_at:
Time
.
current
)
if
callout
.
valid?
if
callout
.
persisted?
callout
.
update
(
dismissed_at:
Time
.
current
)
respond_to
do
|
format
|
format
.
json
{
head
:ok
}
end
...
...
@@ -20,12 +20,6 @@ class UserCalloutsController < ApplicationController
private
# rubocop: disable CodeReuse/ActiveRecord
def
ensure_callout
current_user
.
callouts
.
find_or_create_by
(
feature_name:
UserCallout
.
feature_names
[
feature_name
])
end
# rubocop: enable CodeReuse/ActiveRecord
def
feature_name
params
.
require
(
:feature_name
)
end
...
...
app/graphql/mutations/user_callouts/create.rb
View file @
333a697b
...
...
@@ -15,7 +15,7 @@ module Mutations
description:
'The user callout dismissed.'
def
resolve
(
feature_name
:)
user_callout
=
find
_callout
(
feature_name
)
user_callout
=
current_user
.
find_or_initialize
_callout
(
feature_name
)
user_callout
.
update
(
dismissed_at:
Time
.
current
)
if
user_callout
.
valid?
errors
=
errors_on_object
(
user_callout
)
...
...
@@ -25,12 +25,6 @@ module Mutations
errors:
errors
}
end
private
def
find_callout
(
feature_name
)
current_user
.
callouts
.
find_or_initialize_by
(
feature_name:
::
UserCallout
.
feature_names
[
feature_name
])
# rubocop:disable CodeReuse/ActiveRecord
end
end
end
end
app/models/user.rb
View file @
333a697b
...
...
@@ -1854,6 +1854,10 @@ class User < ApplicationRecord
created_at
>
Devise
.
confirm_within
.
ago
end
def
find_or_initialize_callout
(
feature_name
)
callouts
.
find_or_initialize_by
(
feature_name:
::
UserCallout
.
feature_names
[
feature_name
])
end
protected
# override, from Devise::Validatable
...
...
spec/models/user_spec.rb
View file @
333a697b
...
...
@@ -5482,4 +5482,43 @@ RSpec.describe User do
end
end
end
describe
'#find_or_initialize_callout'
do
subject
(
:find_or_initialize_callout
)
{
user
.
find_or_initialize_callout
(
feature_name
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:feature_name
)
{
UserCallout
.
feature_names
.
keys
.
first
}
context
'when callout exists'
do
let!
(
:callout
)
{
create
(
:user_callout
,
user:
user
,
feature_name:
feature_name
)
}
it
'returns existing callout'
do
expect
(
find_or_initialize_callout
).
to
eq
(
callout
)
end
end
context
'when callout does not exist'
do
context
'when feature name is valid'
do
it
'initializes a new callout'
do
expect
(
find_or_initialize_callout
).
to
be_a_new
(
UserCallout
)
end
it
'is valid'
do
expect
(
find_or_initialize_callout
).
to
be_valid
end
end
context
'when feature name is not valid'
do
let
(
:feature_name
)
{
'notvalid'
}
it
'initializes a new callout'
do
expect
(
find_or_initialize_callout
).
to
be_a_new
(
UserCallout
)
end
it
'is not valid'
do
expect
(
find_or_initialize_callout
).
not_to
be_valid
end
end
end
end
end
spec/requests/api/graphql/mutations/user_callouts/create_spec.rb
View file @
333a697b
# frozen_string_literal: true
require
'spec_helper'
...
...
@@ -7,7 +6,7 @@ RSpec.describe 'Create a user callout' do
include
GraphqlHelpers
let_it_be
(
:current_user
)
{
create
(
:user
)
}
let
(
:feature_name
)
{
::
UserCallout
.
feature_names
.
keys
.
first
}
let
(
:feature_name
)
{
::
UserCallout
.
feature_names
.
each_key
.
first
}
let
(
:input
)
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