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
skip_before_action :authenticate_user!
davilajose23 marked this conversation as resolved.
Show resolved Hide resolved
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
skip_before_action :authenticate_user!
davilajose23 marked this conversation as resolved.
Show resolved Hide resolved

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
skip_before_action :authenticate_user!, only: [:show]
davilajose23 marked this conversation as resolved.
Show resolved Hide resolved
def show
@speaker = current_conference.speakers.friendly.find(params[:id])
@profile = @speaker.profile.presence || Profile.new
Expand Down
10 changes: 8 additions & 2 deletions app/helpers/navigation_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ 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)) &&
!current_page?(new_registration_path) &&
!current_page?(new_user_session_path) &&
!current_page?(new_password_reset_path) &&
!current_page?(edit_password_reset_path) &&
!current_page?(post_submit_password_reset_path) &&
!current_page?(coming_soon_path)
davilajose23 marked this conversation as resolved.
Show resolved Hide resolved
end

def title(title)
Expand Down
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?
davilajose23 marked this conversation as resolved.
Show resolved Hide resolved

self.relation = relation.publics
end

def starts_at
@_starts_at ||= params[:starts_at]&.to_date
rescue Date::Error
Expand Down
42 changes: 29 additions & 13 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_user&.sessions&.live_or_upcoming_today&.first)
davilajose23 marked this conversation as resolved.
Show resolved Hide resolved
),
class: [
nav_text_class_for([schedule_path]),
Expand All @@ -29,18 +29,34 @@
My Schedule
<% end %>

<%= link_to(
profile_path(current_profile&.uuid),
class: [
nav_text_class_for(["/profiles"]),
"flex flex-col items-center justify-center"
]
) do %>
<%= inline_svg_tag(
"icons/avatar_no_fill.svg",
class: nav_icon_class_for(["/profiles"])
) %>
Profile
<% if user_signed_in? %>
<%= link_to(
profile_path(current_profile&.uuid),
davilajose23 marked this conversation as resolved.
Show resolved Hide resolved
class: [
nav_text_class_for(["/profiles"]),
"flex flex-col items-center justify-center"
]
) do %>
<%= inline_svg_tag(
"icons/avatar_no_fill.svg",
class: nav_icon_class_for(["/profiles"])
) %>
Profile
<% end %>
<% else %>
<%= link_to(
new_user_session_path,
class: [
nav_text_class_for(["/profiles"]),
"flex flex-col items-center justify-center"
]
) do %>
<%= inline_svg_tag(
"icons/avatar_no_fill.svg",
class: nav_icon_class_for(["/profiles"])
) %>
Profile
<% end %>
<% end %>

<%= link_to(
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
3 changes: 2 additions & 1 deletion app/views/user_sessions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

<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 "Sign up", new_registration_path, class: "font-black italic underline text-white mb-10" %>
<%= link_to "Continue as guest", sessions_path, class: "font-black italic underline text-white" %>
andresag4 marked this conversation as resolved.
Show resolved Hide resolved
</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