Skip to content

Commit

Permalink
Merge pull request #115 from fnf-org/kig/fix-redirect
Browse files Browse the repository at this point in the history
Fixing login/registration redirect loops;
  • Loading branch information
beingmattlevy authored May 5, 2024
2 parents 395a6e4 + 77b2590 commit dec9508
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 95 deletions.
5 changes: 3 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ def stripe_secret_api_key
def after_sign_in_path_for(resource)
if redirect_to_param.present?
store_location_for(resource, redirect_to_param)
elsif request.referer == Routing.routes.new_user_session_url
elsif [Routing.routes.new_user_registration_url,
Routing.routes.new_user_session_url].include?(request.referer)
super
else
stored_location_for(resource) || request.referer || root_path
end
end

def redirect_path
@redirect_path = redirect_to_param || request.path
@redirect_path = redirect_to_param
end

def redirect_to_param
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def index
elsif (ticket_request = TicketRequest.where(user: current_user)
.where(event: most_recent_event).last)

redirect_to event_ticket_request_path(event_id: most_recent_event.id, id: ticket_request.id)
redirect_to event_ticket_request_path(event_id: most_recent_event.to_param, id: ticket_request.id)

else
redirect_to new_event_ticket_request_path(event_id: most_recent_event.id)
redirect_to new_event_ticket_request_path(event_id: most_recent_event.to_param)
end
else
render
Expand Down
28 changes: 10 additions & 18 deletions app/controllers/ticket_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,20 @@ def show
end

def new
unless @event.ticket_sales_open?
flash.now[:error] = @event.errors.messages.values.join('. ')
return redirect_to root_path, error: flash.now[:error]
end

if signed_in?
existing_request = TicketRequest.where(user_id: current_user, event_id: @event).order(:created_at).first
@ticket_request = TicketRequest.new(event_id: @event.id)

return redirect_to event_ticket_request_path(@event, existing_request) if existing_request
unless @event.ticket_requests_open?
flash.now[:error] = @event.errors.full_messages.join('. ')
return render action: 'new'
end

@user = current_user if signed_in?
@ticket_request = TicketRequest.new
if signed_in?
@user = current_user

if @user
last_ticket_request = TicketRequest.where(user_id: @user&.id).order(:created_at).last
if last_ticket_request
%w[address_line1 address_line2 city state zip_code country_code].each do |field|
@ticket_request.send(:"#{field}=", last_ticket_request.send(field))
end
existing_request = TicketRequest.where(user_id: @user.id, event_id: @event).order(:created_at).first
if existing_request
redirect_to event_ticket_request_path(event_id: @event.id, id: existing_request.id)
end
else
@ticket_request = TicketRequest.new(event_id: @event.id)
end
end

Expand Down Expand Up @@ -160,6 +151,7 @@ def create
render_flash(flash)
end
end

# rubocop: enable Metrics/AbcSize

def update
Expand Down
10 changes: 5 additions & 5 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def resource_class
devise_mapping.to
end

def log_in_options
redirect_path = instance_variable_get(:@redirect_path)

redirect_path ? { redirect_to: redirect_path } : {}
end
# def log_in_options
# redirect_path = instance_variable_get(:@redirect_path)
#
# redirect_path ? { redirect_to: redirect_path } : {}
# end
end
16 changes: 11 additions & 5 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,24 @@ def ticket_sales_open?
elsif ticket_sales_end_time && Time.current.after?(ticket_sales_end_time)
errors.add(:ticket_sales_end_time, 'Tickets are no longer on sale for this event.')
elsif Time.current.after?(end_time)
errors.add(:end_time, 'This event has ended, so no ticket requests are accepted anymore.')
errors.add(:end_time, 'This event has ended and ticket sales are closed.')
else
return true
end
Rails.logger.error("ticket_sales_open? -> false, reason: #{errors.full_messages.join('; ')}".colorize(:red))
false
end

def ticket_requests_open?
return false if Time.zone.now >= end_time
return true unless ticket_requests_end_time

ticket_requests_end_time > Time.zone.now
if ticket_requests_end_time && Time.current.after?(ticket_requests_end_time)
errors.add(:ticket_requests_end_time, 'Ticket requests are no longer accepted.')
elsif Time.current.after?(end_time)
errors.add(:end_time, 'This event has ended and ticket sales are closed.')
else
return true
end
Rails.logger.error("ticket_requests_open? -> false, reason: #{errors.full_messages.join('; ')}".colorize(:red))
false
end

def eald?
Expand Down
36 changes: 36 additions & 0 deletions app/views/events/_event_summary.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
%h4 Event Summary
%dl.dl-horizontal
%dt Start Time
%dd= TimeHelper.for_display(@event.start_time.localtime)
%dt End Time
%dd= TimeHelper.for_display(@event.end_time.localtime)

- if @event.ticket_sales_start_time
%dt Tickets On Sale At
%dd= TimeHelper.for_display(@event.ticket_sales_start_time)

- if @event.ticket_sales_end_time
%dt Tickets Sales Stop At
%dd= TimeHelper.for_display(@event.ticket_sales_end_time)

%dt Adult Ticket Price
%dd
= number_to_currency(@event.adult_ticket_price)
- if @event.max_adult_tickets_per_request
%span.muted
(max #{@event.max_adult_tickets_per_request} tickets per request)
%dt Kid Ticket Price
%dd
- if @event.price_rules.any?
%ul
- @event.price_rules.order(:created_at).each do |price_rule|
%li= price_rule.rule_text
%li
Otherwise:
= number_to_currency(@event.kid_ticket_price)
\/ kid
- else
= number_to_currency(@event.kid_ticket_price)
- if @event.max_kid_tickets_per_request
%span.muted
(max #{@event.max_kid_tickets_per_request} kid tickets per request)
44 changes: 1 addition & 43 deletions app/views/events/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,7 @@
.col-sm-12.col-md-12.col-lg-6
.card.p-2.border-1
.card-body
%h4 Event Summary
%dl.dl-horizontal
%dt Start Time
%dd= TimeHelper.for_display(@event.start_time.localtime)
%dt End Time
%dd= TimeHelper.for_display(@event.end_time.localtime)

- if @event.ticket_sales_start_time
%dt Tickets On Sale At
%dd= TimeHelper.for_display(@event.ticket_sales_start_time)

- if @event.ticket_sales_end_time
%dt Tickets Sales Stop At
%dd= TimeHelper.for_display(@event.ticket_sales_end_time)

%dt Adult Ticket Price
%dd
= number_to_currency(@event.adult_ticket_price)
- if @event.max_adult_tickets_per_request
%span.muted
(max #{@event.max_adult_tickets_per_request} tickets per request)
%dt Kid Ticket Price
%dd
- if @event.price_rules.any?
%ul
- @event.price_rules.order(:created_at).each do |price_rule|
%li= price_rule.rule_text
%li
Otherwise:
= number_to_currency(@event.kid_ticket_price)
\/ kid
- else
= number_to_currency(@event.kid_ticket_price)
- if @event.max_kid_tickets_per_request
%span.muted
(max #{@event.max_kid_tickets_per_request} kid tickets per request)
%dt Cabin Price
%dd
= number_to_currency(@event.cabin_price)
- if @event.max_cabins_per_request
%span.muted
(max #{@event.max_cabins_per_request} cabins per request)

= render partial: "events/event_summary"
.card.mt-4
.card-header
%h4 Ticket Request Link
Expand Down
17 changes: 4 additions & 13 deletions app/views/ticket_requests/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,7 @@

.card-body
- if @event.ticket_requests_open?
%em Event Starts:
%br
%strong
= TimeHelper.for_display(@event.start_time.localtime)

%br
.vertical-20
%em Event Ends:
%br
%strong
= TimeHelper.for_display(@event.end_time.localtime)
= render partial: "events/event_summary"

- else
%h5 Ticket sales for #{@event.name} have closed
Expand All @@ -34,6 +24,7 @@
%p
If your request is approved, you'll receive an email with further instructions.

.col-sm-12.col-md-12.col-lg-7.col-xl-7
= render 'form'
- if @event.ticket_requests_open?
.col-sm-12.col-md-12.col-lg-7.col-xl-7
= render 'form'

2 changes: 1 addition & 1 deletion config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,5 +309,5 @@

# When set to false, does not sign a user in automatically after their password is
# changed. Defaults to true, so a user is signed in automatically after changing a password.
# config.sign_in_after_change_password = true
config.sign_in_after_change_password = true
end
12 changes: 10 additions & 2 deletions config/initializers/lograge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@
# noinspection RubyResolve
Rails.application.configure do
config.lograge.enabled = true

config.lograge.keep_original_rails_log = Rails.env.test?

config.lograge.logger = ActiveSupport::Logger.new(Rails.env.test? ? nil : $stdout)
config.lograge.custom_options = lambda do |event|
request = event.payload[:request]
stacktrace = Rails.backtrace_cleaner.clean(event.payload[:exception_object].backtrace) if event.payload[:exception_object]

{
params: request.filtered_parameters.except(:controller, :action),
request_id: request.request_id,
exception: event.payload[:exception],
stacktrace:
}
end
config.lograge.custom_payload do |controller|
{
host: controller.request.host,
Expand Down
6 changes: 6 additions & 0 deletions config/initializers/notifications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

ActiveSupport::Notifications.subscribe 'redirect_to.action_controller' do |event|
Rails.logger.warn "NOTIFICATIONS: redirect -> [#{event.payload[:location]}] , status: [#{event.payload[:status]}]".colorize(:yellow)
Rails.logger.warn "NOTIFICATIONS: event: -> [#{event.inspect}".colorize(:magenta)
end
8 changes: 4 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

get 'oops', controller: :home, action: :oops

devise_scope :user do
# Redirests signing out users back to sign-in
get 'users', to: 'devise/sessions#new'
end
# devise_scope :user do
# # Redirects signing out users back to sign-in
# get 'users', to: 'devise/sessions#new'
# end

devise_for :users

Expand Down

0 comments on commit dec9508

Please sign in to comment.