Skip to content

Commit

Permalink
Added session_id to Query since it was not scalable to store the AR…
Browse files Browse the repository at this point in the history
… ids in the cookie session.
  • Loading branch information
jclusso committed Feb 12, 2024
1 parent cbd3c59 commit e77ccf4
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 16 deletions.
9 changes: 1 addition & 8 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 5 additions & 6 deletions app/controllers/queries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@ 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
end

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)
@query.results = dns_lookup.run
@query.duration = dns_lookup.duration
@query.save

session[:queries] ||= []
session[:queries].push(@query.id)
session[:last_query_type] = @query.type

redirect_to @query
else
Expand Down
1 change: 1 addition & 0 deletions app/models/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/queries/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</div>
</div>
<div class="flex p-4 text-xs text-neutral-600 justify-between">
<% 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' %>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240212142204_add_session_id_to_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSessionIdToQuery < ActiveRecord::Migration[7.2]
def change
add_column :queries, :session_id, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixtures/queries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# type :string
# created_at :datetime not null
# updated_at :datetime not null
# session_id :string
#

one:
Expand Down
1 change: 1 addition & 0 deletions test/models/query_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# type :string
# created_at :datetime not null
# updated_at :datetime not null
# session_id :string
#
require "test_helper"

Expand Down

0 comments on commit e77ccf4

Please sign in to comment.