Skip to content

Commit

Permalink
Merge pull request #154 from TelosLabs/guest_mode
Browse files Browse the repository at this point in the history
Add guest mode
  • Loading branch information
andresag4 authored Sep 3, 2024
2 parents afc223b + 109dfd5 commit bbb1e86
Show file tree
Hide file tree
Showing 22 changed files with 85 additions and 19 deletions.
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
12 changes: 9 additions & 3 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,7 +11,11 @@ def index

def show
@user_session_ids = current_user.sessions.pluck(:id)
@session = sessions.friendly.includes(:location, :tags, speakers: [profile: :image_attachment]).find(params[:id])
@session = if user_signed_in?
sessions.friendly.includes(:location, :tags, speakers: [profile: :image_attachment]).find(params[:id])
else
sessions.publics.friendly.includes(:location, :tags, speakers: [profile: :image_attachment]).find(params[:id])
end
end

private
Expand All @@ -19,6 +25,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 show_back_button?
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 @@ -51,6 +52,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) }

def self.ransackable_attributes(_auth_object = nil)
%w[title]
Expand All @@ -69,4 +72,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 @@ -50,7 +50,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
11 changes: 7 additions & 4 deletions app/views/user_sessions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
</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>
<div class="relative" data-controller="password-visibility">
<%= form.password_field :password, required: true, class: "peer w-full", placeholder: "Password", data: { test_id: "password_field", "password-visibility-target": "input" } %>
<button class="absolute right-3 top-1/2 -translate-y-1/2 text-gray-300 hover:text-white peer-focus:text-gray-400 peer-focus:hover:text-gray-600" type="button" data-action="password-visibility#toggle">
<button class="absolute text-gray-300 -translate-y-1/2 right-3 top-1/2 hover:text-white peer-focus:text-gray-400 peer-focus:hover:text-gray-600" type="button" data-action="password-visibility#toggle">
<%= inline_svg_tag("icons/eye-empty.svg", size: "22", data: { "password-visibility-target": "icon" }) %>
<%= inline_svg_tag("icons/eye-off.svg", size: "22", class: "hidden", data: { "password-visibility-target": "icon" }) %>
</button>
Expand All @@ -27,8 +30,8 @@
</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 @@ -45,6 +45,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."
invalid_auth_token: "Your request has expired. Please try again."
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

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

0 comments on commit bbb1e86

Please sign in to comment.