Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guest mode #154

Merged
merged 15 commits into from
Sep 3, 2024
Merged
1 change: 1 addition & 0 deletions app/avo/resources/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def fields
field :title, as: :text, sortable: true, link_to_record: true
field :slug, as: :text, hide_on: :new
field :description, as: :trix
field :public, as: :boolean
field :starts_at, as: :date_time,
help: "The datetime field will use your browser's current timezone.", sortable: true,
format: "FFFF"
Expand Down
5 changes: 5 additions & 0 deletions app/constraints/authenticated_constraint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AuthenticatedConstraint
def matches?(request)
request.session[:user_id].present?
end
end
5 changes: 5 additions & 0 deletions app/constraints/unauthenticated_constraint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class UnauthenticatedConstraint
def matches?(request)
request.session[:user_id].nil?
end
end
1 change: 1 addition & 0 deletions app/controllers/abouts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class AboutsController < ApplicationController
allow_unauthenticated_access
def show
end
end
1 change: 1 addition & 0 deletions app/controllers/concerns/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def authenticate_user!
authenticate_user

if !user_signed_in?
flash[:notice] = I18n.t("authentication.unauthenticated")
redirect_to new_user_session_path
end
end
Expand Down
7 changes: 5 additions & 2 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class SessionsController < ApplicationController
allow_unauthenticated_access

def index
@user_session_ids = current_user.sessions.pluck(:id)
@user_session_ids = current_user&.sessions&.pluck(:id)
@sessions = SessionQuery.new(
relation: sessions.joins(:location).distinct,
params: filter_params
Expand All @@ -9,6 +11,7 @@ def index

def show
@session = sessions.friendly.find(params[:id])
raise ActiveRecord::RecordNotFound if @session.private? && !user_signed_in?
davilajose23 marked this conversation as resolved.
Show resolved Hide resolved
end

private
Expand All @@ -18,6 +21,6 @@ def sessions
end

def filter_params
params.permit(:starts_at, :live, :past, :starting_soon)
params.permit(:starts_at, :live, :past, :starting_soon).merge(show_private: user_signed_in?)
end
end
1 change: 1 addition & 0 deletions app/controllers/speakers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class SpeakersController < ApplicationController
allow_unauthenticated_access only: [:show]
def show
@speaker = current_conference.speakers.friendly.find(params[:id])
@profile = @speaker.profile.presence || Profile.new
Expand Down
17 changes: 15 additions & 2 deletions app/helpers/navigation_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,21 @@ def show_header?
end

def show_bottom_navbar?
user_signed_in? &&
!current_page?(coming_soon_path)
current_page?(sessions_path) ||
(user_signed_in? || !current_page?(unauthenticated_root_path)) &&
!excluded_paths.any? { |path| current_page?(path) }
end

private

def excluded_paths
[
new_user_session_path,
new_registration_path,
new_password_reset_path,
edit_password_reset_path,
post_submit_password_reset_path
]
end

def title(title)
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/session_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ def current_agenda_session
end

def current_schedule_session
@current_schedule_session ||= current_user.sessions.live_or_upcoming_today.first
@current_schedule_session ||= current_user&.sessions&.live_or_upcoming_today&.first
end
end
7 changes: 7 additions & 0 deletions app/models/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#
# id :integer not null, primary key
# ends_at :datetime not null
# public :boolean default(TRUE), not null
# sent_reminders :json
# slug :string
# starts_at :datetime not null
Expand Down Expand Up @@ -49,6 +50,8 @@ class Session < ApplicationRecord
scope :starting_soon, -> { where("starts_at BETWEEN ? and ?", Time.current, 1.hour.from_now) }
scope :upcoming_today, -> { where("date(starts_at) = ? and starts_at > ?", Date.current, Time.current) }
scope :live_or_upcoming_today, -> { live.or(upcoming_today) }
scope :publics, -> { where(public: true) }
scope :privates, -> { where(public: false) }
andresag4 marked this conversation as resolved.
Show resolved Hide resolved

def self.ransackable_attributes(_auth_object = nil)
%w[title]
Expand All @@ -67,4 +70,8 @@ def starting_soon?
def past?
ends_at < Time.current
end

def private?
!public?
end
end
7 changes: 7 additions & 0 deletions app/queries/session_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize(relation: Session.all, params: {})
def call
filter_by_date
filter_by_status
filter_privates

relation
end
Expand All @@ -27,6 +28,12 @@ def filter_by_status
self.relation = relation.send_chain_or(status_scopes)
end

def filter_privates
return if params[:show_private].present?

self.relation = relation.publics
end

def starts_at
@_starts_at ||= params[:starts_at]&.to_date
rescue Date::Error
Expand Down
8 changes: 4 additions & 4 deletions app/views/layouts/_bottom_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<%= link_to(
schedule_path(
starts_at: current_starts_at_filter,
anchor: session_anchor(current_user.sessions.live_or_upcoming_today.first)
anchor: session_anchor(current_schedule_session)
),
class: [
nav_text_class_for([schedule_path]),
Expand All @@ -30,16 +30,16 @@
<% end %>

<%= link_to(
profile_path(current_profile&.uuid),
user_signed_in? ? profile_path(current_profile&.uuid) : new_user_session_path,
class: [
nav_text_class_for(["/profiles"]),
"flex flex-col items-center justify-center"
]
) do %>
) do %>
<%= inline_svg_tag(
"icons/avatar_no_fill.svg",
class: nav_icon_class_for(["/profiles"])
) %>
) %>
Profile
<% end %>

Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<%= yield %>

<% if show_bottom_navbar? %>
<%= render partial: "layouts/bottom_navbar", locals: { unread_notifications: current_user.notifications.unread } %>
<%= render partial: "layouts/bottom_navbar", locals: { unread_notifications: current_user&.notifications&.unread } %>
<% end %>
</main>
</body>
Expand Down
2 changes: 1 addition & 1 deletion app/views/sessions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
partial: 'sessions/card',
locals: {
session: session,
user_is_an_attendee: @user_session_ids.include?(session.id)
user_is_an_attendee: @user_session_ids&.include?(session.id)
}
) %>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/speakers/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
partial: "sessions/card",
locals: {
session: session,
user_is_an_attendee: Current.user.sessions.include?(session)
user_is_an_attendee: Current.user&.sessions&.include?(session)
}
) %>
<% end %>
Expand Down
9 changes: 6 additions & 3 deletions app/views/user_sessions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
</div>

<div class="flex flex-col">
<%= form.label :password, class: "text-white italic font-bold mb-2" %>
<div class="flex flex-row justify-between">
<%= form.label :password, class: "text-white italic font-bold mb-2" %>
<%= link_to "Forgot password?", new_password_reset_path, class: "font-black italic underline text-white" %>
</div>
<%= form.password_field :password, required: true, placeholder: "Password", data: { test_id: "password_field" } %>
</div>
</div>

<div class="flex flex-col items-center mb-10">
<%= link_to "Forgot password?", new_password_reset_path, class: "font-black italic underline text-white mb-10" %>
<%= link_to "Sign up", new_registration_path, class: "font-black italic underline text-white" %>
<%= link_to "View as guest", sessions_path, class: "font-black italic underline text-white mb-10" %>
<%= link_to "Sign up", new_registration_path, class: "font-black italic underline text-white mb-10" %>
</div>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ en:
live: "Live"
starting_soon: "Starting soon"
submit: "Apply"
authentication:
unauthenticated: "You need to sign in or sign up before continuing."
authorization:
unauthorized: "You are not authorized to access this page."
session_mailer:
Expand Down
8 changes: 7 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
Rails.application.routes.draw do
root "sessions#index"
constraints AuthenticatedConstraint.new do
root "sessions#index"
end

constraints UnauthenticatedConstraint.new do
root "user_sessions#new", as: :unauthenticated_root
end
andresag4 marked this conversation as resolved.
Show resolved Hide resolved

get "up" => "rails/health#show", :as => :rails_health_check
get "/service-worker.js" => "service_worker#service_worker"
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240829165208_add_public_to_sessions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPublicToSessions < ActiveRecord::Migration[7.2]
def change
add_column :sessions, :public, :boolean, default: true, null: false
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 spec/factories/sessions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#
# id :integer not null, primary key
# ends_at :datetime not null
# public :boolean default(TRUE), not null
# sent_reminders :json
# slug :string
# starts_at :datetime not null
Expand Down
1 change: 1 addition & 0 deletions spec/models/session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#
# id :integer not null, primary key
# ends_at :datetime not null
# public :boolean default(TRUE), not null
# sent_reminders :json
# slug :string
# starts_at :datetime not null
Expand Down