From 2765e1e19140ea4d7a2d3529993d736fc35b78b0 Mon Sep 17 00:00:00 2001 From: Jose Fernando Davila Date: Thu, 29 Aug 2024 16:09:40 -0600 Subject: [PATCH] add guest mode --- app/avo/resources/session.rb | 1 + app/constraints/authenticated_constraint.rb | 5 +++ app/constraints/unauthenticated_constraint.rb | 5 +++ app/controllers/abouts_controller.rb | 1 + app/controllers/concerns/authentication.rb | 1 + app/controllers/sessions_controller.rb | 7 ++- app/controllers/speakers_controller.rb | 1 + app/helpers/navigation_helper.rb | 8 +++- app/models/session.rb | 7 +++ app/queries/session_query.rb | 7 +++ app/views/layouts/_bottom_navbar.html.erb | 43 +++++++++++++------ app/views/layouts/application.html.erb | 2 +- app/views/sessions/index.html.erb | 2 +- app/views/speakers/show.html.erb | 2 +- app/views/user_sessions/new.html.erb | 3 +- config/routes.rb | 9 +++- .../20240829165208_add_public_to_sessions.rb | 5 +++ db/schema.rb | 3 +- spec/factories/sessions.rb | 1 + spec/models/session_spec.rb | 1 + 20 files changed, 92 insertions(+), 22 deletions(-) create mode 100644 app/constraints/authenticated_constraint.rb create mode 100644 app/constraints/unauthenticated_constraint.rb create mode 100644 db/migrate/20240829165208_add_public_to_sessions.rb diff --git a/app/avo/resources/session.rb b/app/avo/resources/session.rb index 0e20798e..2f092693 100644 --- a/app/avo/resources/session.rb +++ b/app/avo/resources/session.rb @@ -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" diff --git a/app/constraints/authenticated_constraint.rb b/app/constraints/authenticated_constraint.rb new file mode 100644 index 00000000..5c59d059 --- /dev/null +++ b/app/constraints/authenticated_constraint.rb @@ -0,0 +1,5 @@ +class AuthenticatedConstraint + def matches?(request) + request.session[:user_id].present? + end +end \ No newline at end of file diff --git a/app/constraints/unauthenticated_constraint.rb b/app/constraints/unauthenticated_constraint.rb new file mode 100644 index 00000000..87f9aca4 --- /dev/null +++ b/app/constraints/unauthenticated_constraint.rb @@ -0,0 +1,5 @@ +class UnauthenticatedConstraint + def matches?(request) + request.session[:user_id].nil? + end +end \ No newline at end of file diff --git a/app/controllers/abouts_controller.rb b/app/controllers/abouts_controller.rb index e17cc4de..2e3b1c48 100644 --- a/app/controllers/abouts_controller.rb +++ b/app/controllers/abouts_controller.rb @@ -1,4 +1,5 @@ class AboutsController < ApplicationController + skip_before_action :authenticate_user! def show end end diff --git a/app/controllers/concerns/authentication.rb b/app/controllers/concerns/authentication.rb index 0fa41334..e784cdb5 100644 --- a/app/controllers/concerns/authentication.rb +++ b/app/controllers/concerns/authentication.rb @@ -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 diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 40ce9583..816c8fb9 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -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 @@ -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 @@ -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 diff --git a/app/controllers/speakers_controller.rb b/app/controllers/speakers_controller.rb index 7bb1a30c..889238cb 100644 --- a/app/controllers/speakers_controller.rb +++ b/app/controllers/speakers_controller.rb @@ -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 diff --git a/app/helpers/navigation_helper.rb b/app/helpers/navigation_helper.rb index 1da7657a..58922915 100644 --- a/app/helpers/navigation_helper.rb +++ b/app/helpers/navigation_helper.rb @@ -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 diff --git a/app/models/session.rb b/app/models/session.rb index eabb3c2d..92f3525b 100644 --- a/app/models/session.rb +++ b/app/models/session.rb @@ -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 @@ -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] @@ -67,4 +70,8 @@ def starting_soon? def past? ends_at < Time.current end + + def private? + !public? + end end diff --git a/app/queries/session_query.rb b/app/queries/session_query.rb index 7807e336..88a79a5d 100644 --- a/app/queries/session_query.rb +++ b/app/queries/session_query.rb @@ -9,6 +9,7 @@ def initialize(relation: Session.all, params: {}) def call filter_by_date filter_by_status + filter_privates relation end @@ -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 diff --git a/app/views/layouts/_bottom_navbar.html.erb b/app/views/layouts/_bottom_navbar.html.erb index 1f7b5b69..ac026ce4 100644 --- a/app/views/layouts/_bottom_navbar.html.erb +++ b/app/views/layouts/_bottom_navbar.html.erb @@ -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]), @@ -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: [ diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index c97a000d..ec992782 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -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 %> diff --git a/app/views/sessions/index.html.erb b/app/views/sessions/index.html.erb index 546d1d87..97c61298 100644 --- a/app/views/sessions/index.html.erb +++ b/app/views/sessions/index.html.erb @@ -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 %> diff --git a/app/views/speakers/show.html.erb b/app/views/speakers/show.html.erb index d4e5febc..9602778f 100644 --- a/app/views/speakers/show.html.erb +++ b/app/views/speakers/show.html.erb @@ -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 %> diff --git a/app/views/user_sessions/new.html.erb b/app/views/user_sessions/new.html.erb index cd294413..07f9ff4c 100644 --- a/app/views/user_sessions/new.html.erb +++ b/app/views/user_sessions/new.html.erb @@ -22,7 +22,8 @@
<%= 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" %>
diff --git a/config/routes.rb b/config/routes.rb index 798c0ab1..1f110f05 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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" diff --git a/db/migrate/20240829165208_add_public_to_sessions.rb b/db/migrate/20240829165208_add_public_to_sessions.rb new file mode 100644 index 00000000..94d7c37e --- /dev/null +++ b/db/migrate/20240829165208_add_public_to_sessions.rb @@ -0,0 +1,5 @@ +class AddPublicToSessions < ActiveRecord::Migration[7.2] + def change + add_column :sessions, :public, :boolean, default: true, null: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 4f079492..f1d97823 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_08_26_205135) do +ActiveRecord::Schema[7.2].define(version: 2024_08_29_165208) do create_table "action_text_rich_texts", force: :cascade do |t| t.string "name", null: false t.text "body" @@ -117,6 +117,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "slug" + t.boolean "public", default: true, null: false t.index ["conference_id"], name: "index_sessions_on_conference_id" t.index ["location_id"], name: "index_sessions_on_location_id" t.index ["slug"], name: "index_sessions_on_slug", unique: true diff --git a/spec/factories/sessions.rb b/spec/factories/sessions.rb index 426cfe3f..6595a8e5 100644 --- a/spec/factories/sessions.rb +++ b/spec/factories/sessions.rb @@ -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 diff --git a/spec/models/session_spec.rb b/spec/models/session_spec.rb index a3232aec..85df6b89 100644 --- a/spec/models/session_spec.rb +++ b/spec/models/session_spec.rb @@ -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