Skip to content

Commit

Permalink
add guest mode
Browse files Browse the repository at this point in the history
  • Loading branch information
davilajose23 committed Aug 29, 2024
1 parent 9c20cbb commit 2765e1e
Show file tree
Hide file tree
Showing 20 changed files with 92 additions and 22 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
skip_before_action :authenticate_user!
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] = "You need to sign in or sign up before continuing."
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!

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?
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]
def show
@speaker = current_conference.speakers.friendly.find(params[:id])
@profile = @speaker.profile.presence || Profile.new
Expand Down
8 changes: 7 additions & 1 deletion app/helpers/navigation_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ def show_header?
end

def show_bottom_navbar?
user_signed_in? &&
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)
end

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) }

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
43 changes: 30 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)
),
class: [
nav_text_class_for([schedule_path]),
Expand All @@ -29,20 +29,37 @@
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),
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(
notifications_path,
class: [
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" %>
</div>
</div>
</div>
Expand Down
9 changes: 8 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
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 2765e1e

Please sign in to comment.