From 9acfc1f8ede8d1fa68dd8f51f3359d7322f43f8f Mon Sep 17 00:00:00 2001 From: Dean Lofts Date: Sun, 25 Aug 2024 00:42:24 +1000 Subject: [PATCH] add theme support --- app/controllers/links_controller.rb | 29 ++++++++++++++++++++++++++--- config/routes.rb | 19 +++++++++++++------ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/app/controllers/links_controller.rb b/app/controllers/links_controller.rb index 071cac1..4e1ec9a 100644 --- a/app/controllers/links_controller.rb +++ b/app/controllers/links_controller.rb @@ -1,5 +1,6 @@ class LinksController < ApplicationController before_action :authenticate_user!, except: [:index, :show, :user_links, :track_click] + before_action :set_theme, only: [:user_links] # Add this line def index @links = Link.order(:position) @@ -43,10 +44,27 @@ def destroy def user_links @user = User.find_by(username: params[:username]) - @links = @user.links.where(visible: true).order(:position) - @pinned_links = @user.links.where(visible: true, pinned: true).order(:position) - @achievements = @user.achievements.order(date: :desc) + return redirect_to root_path, alert: "User not found" if @user.nil? + + @links = @user.links + @pinned_links = @user.links.where(pinned: true) + @achievements = @user.achievements @user.tags = JSON.parse(@user.tags) if @user.tags.is_a?(String) + + # Add debugging + Rails.logger.debug "Theme: #{@theme.inspect}" + + # Render the appropriate template based on the theme + case @theme + when 'retro' + render 'user_links_retro' + when 'win95' + render 'user_links_win95' + when 'win98' + render 'user_links_win98' + else + render 'user_links' + end end def track_click @@ -68,4 +86,9 @@ def track_click def link_params params.require(:link).permit(:url, :title, :description, :position, :icon, :visible, :pinned) end + + def set_theme + # Determine the theme either from a query parameter or route segment + @theme = params[:theme] || 'default' + end end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index e04ae32..bd7d7bd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,26 +1,33 @@ Rails.application.routes.draw do get 'analytics/index' - - # Use the custom registrations controller in all environments + + # Devise routes for user registration devise_for :users, controllers: { registrations: 'users/registrations' } + # Routes for links with standard RESTful actions resources :links do member do get :track_click end end + # Sidekiq monitoring require 'sidekiq/web' require 'sidekiq-scheduler/web' mount Sidekiq::Web => '/sidekiq' - # Other routes + # Custom routes for user-specific views and analytics get '/:username/analytics', to: 'analytics#index', as: :user_analytics - get "up" => "rails/health#show", as: :rails_health_check + get '/:username(/:theme)', to: 'links#user_links', as: :user_links, constraints: { theme: /retro|win95|win98/ } + + # Health check route + get 'up' => 'rails/health#show', as: :rails_health_check + + # Root route root to: 'pages#home' - resources :links, only: [:index, :show, :new, :create, :edit, :update, :destroy] + + # Additional resources resources :achievements, only: [:index, :show, :new, :create, :edit, :update, :destroy] - get '/:username', to: 'links#user_links', as: 'user_links' end \ No newline at end of file