Commit 333a697b authored by Matija Čupić's avatar Matija Čupić

Move callout initialization to User model

Moves callout initialization to the User model.
parent 95c919d9
......@@ -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
......
......@@ -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
......@@ -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
......
......@@ -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
# 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
{
......
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 to comment