Commit ad38e120 authored by Matija Čupić's avatar Matija Čupić

Remove dismissed_state from Callout model

parent 4ff0cfe5
class CalloutsController < ApplicationController
before_action :callout, only: [:dismiss]
def dismiss
respond_to do |format|
format.json do
if @callout
@callout.update(dismissed_state: true)
if ensure_callout
respond_to { |format| format.json { head :ok } }
else
Callout.create(feature_name: callout_param, dismissed_state: true, user: current_user)
end
head :ok
end
respond_to { |format| format.json { head :bad_request } }
end
end
private
def callout
@callout = Callout.find_by(user: current_user, feature_name: callout_param)
def ensure_callout
current_user.callouts.find_or_create_by(feature_name: callout_param)
end
def callout_param
......
......@@ -15,6 +15,6 @@ module CalloutsHelper
private
def user_dismissed?(feature_name)
Callout.find_by(user: current_user, feature_name: feature_name)&.dismissed_state?
current_user&.callouts&.find_by(feature_name: feature_name)
end
end
class Callout < ActiveRecord::Base
belongs_to :user
validates :user, presence: true
validates :feature_name, presence: true, uniqueness: { scope: :user_id }
end
......@@ -8,12 +8,11 @@ class CreateCallouts < ActiveRecord::Migration
def change
create_table :callouts do |t|
t.string :feature_name, null: false
t.boolean :dismissed_state, null: false
t.references :user, index: true, foreign_key: { on_delete: :cascade }, null: false
t.timestamps_with_timezone null: false
end
add_index :callouts, :feature_name, unique: true
add_index :callouts, [:user_id, :feature_name], unique: true
end
end
......@@ -205,13 +205,12 @@ ActiveRecord::Schema.define(version: 20180125214301) do
create_table "callouts", force: :cascade do |t|
t.string "feature_name", null: false
t.boolean "dismissed_state", null: false
t.integer "user_id", null: false
t.datetime_with_timezone "created_at", null: false
t.datetime_with_timezone "updated_at", null: false
end
add_index "callouts", ["feature_name"], name: "index_callouts_on_feature_name", unique: true, using: :btree
add_index "callouts", ["user_id", "feature_name"], name: "index_callouts_on_user_id_and_feature_name", unique: true, using: :btree
add_index "callouts", ["user_id"], name: "index_callouts_on_user_id", using: :btree
create_table "chat_names", force: :cascade do |t|
......
......@@ -25,10 +25,6 @@ describe CalloutsController do
context 'when callout entry already exists' do
let!(:callout) { create(:callout, feature_name: 'feature_name', user: user) }
it 'should update it with a dismissed state' do
expect { subject }.to change { callout.reload.dismissed_state }.from(false).to(true)
end
it 'should return success' do
subject
......
FactoryBot.define do
factory :callout do
feature_name 'test_callout'
dismissed_state false
user
end
......
require 'rails_helper'
describe Callout do
let(:callout) { create(:callout) }
let!(:callout) { create(:callout) }
describe 'relationships' do
it { is_expected.to belong_to(:user) }
end
describe 'validations' do
it { is_expected.to validate_presence_of(:user) }
it { is_expected.to validate_presence_of(:feature_name) }
it { is_expected.to validate_uniqueness_of(:feature_name).scoped_to(:user_id) }
end
end
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