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

Remove dismissed_state from Callout model

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