From e77ccf46e385df33d24d6bbcf266466b893100a4 Mon Sep 17 00:00:00 2001 From: Jarrett Lusso Date: Mon, 12 Feb 2024 09:25:50 -0500 Subject: [PATCH] Added `session_id` to Query since it was not scalable to store the AR ids in the cookie session. --- app/controllers/application_controller.rb | 9 +-------- app/controllers/queries_controller.rb | 11 +++++------ app/models/query.rb | 1 + app/views/queries/show.html.erb | 2 +- db/migrate/20240212142204_add_session_id_to_query.rb | 5 +++++ db/schema.rb | 3 ++- test/fixtures/queries.yml | 1 + test/models/query_test.rb | 1 + 8 files changed, 17 insertions(+), 16 deletions(-) create mode 100644 db/migrate/20240212142204_add_session_id_to_query.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0b1fb4b..dfc8cee 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,13 +1,6 @@ class ApplicationController < ActionController::Base # Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has. - # allow_browser versions: :modern + allow_browser versions: :modern default_form_builder ApplicationFormBuilder - - helper_method :my_queries - - def my_queries - Query.where(id: session[:queries]).order(created_at: :desc) - end - end diff --git a/app/controllers/queries_controller.rb b/app/controllers/queries_controller.rb index fbc6bb5..e335447 100644 --- a/app/controllers/queries_controller.rb +++ b/app/controllers/queries_controller.rb @@ -3,10 +3,10 @@ class QueriesController < ApplicationController def index @queries = if params[:filter] == 'all' - Query.all.order(created_at: :desc) + Query.all else - my_queries - end + Query.where(session_id: session.id.to_s) + end.order(created_at: :desc) end def show @@ -14,7 +14,7 @@ def show def create @query = Query.new(query_params) - session[:last_query_type] = @query.type + @query.session_id = session.id.to_s if @query.valid? dns_lookup = DNSLookup.new(@query.domain, @query.type, @query.server_ip) @@ -22,8 +22,7 @@ def create @query.duration = dns_lookup.duration @query.save - session[:queries] ||= [] - session[:queries].push(@query.id) + session[:last_query_type] = @query.type redirect_to @query else diff --git a/app/models/query.rb b/app/models/query.rb index f55c803..38883af 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -11,6 +11,7 @@ # type :string # created_at :datetime not null # updated_at :datetime not null +# session_id :string # class Query < ApplicationRecord self.inheritance_column = nil diff --git a/app/views/queries/show.html.erb b/app/views/queries/show.html.erb index e158168..40bb098 100644 --- a/app/views/queries/show.html.erb +++ b/app/views/queries/show.html.erb @@ -33,7 +33,7 @@
- <% if my_queries.include?(@query) %> + <% if @query.session_id == session.id.to_s %> <%= button_to "Remove", query_path(@query), method: :delete, form: { data: { turbo_confirm: "Are you sure?" } }, class: 'hover:underline text-neutral-600 transition hover:text-white' %> diff --git a/db/migrate/20240212142204_add_session_id_to_query.rb b/db/migrate/20240212142204_add_session_id_to_query.rb new file mode 100644 index 0000000..f2743f9 --- /dev/null +++ b/db/migrate/20240212142204_add_session_id_to_query.rb @@ -0,0 +1,5 @@ +class AddSessionIdToQuery < ActiveRecord::Migration[7.2] + def change + add_column :queries, :session_id, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index e9d1ff3..c45927f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_02_10_182547) do +ActiveRecord::Schema[7.2].define(version: 2024_02_12_142204) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -23,6 +23,7 @@ t.integer "duration" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "session_id" end end diff --git a/test/fixtures/queries.yml b/test/fixtures/queries.yml index 407579a..8e804e7 100644 --- a/test/fixtures/queries.yml +++ b/test/fixtures/queries.yml @@ -11,6 +11,7 @@ # type :string # created_at :datetime not null # updated_at :datetime not null +# session_id :string # one: diff --git a/test/models/query_test.rb b/test/models/query_test.rb index 4334012..c2644d5 100644 --- a/test/models/query_test.rb +++ b/test/models/query_test.rb @@ -11,6 +11,7 @@ # type :string # created_at :datetime not null # updated_at :datetime not null +# session_id :string # require "test_helper"