|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Pwb |
| 4 | + # Concern for managing theme availability on websites. |
| 5 | + # |
| 6 | + # Provides methods to determine which themes are available for a website, |
| 7 | + # based on: |
| 8 | + # 1. Website-specific available_themes (if set) |
| 9 | + # 2. Tenant default themes (from TenantSettings) |
| 10 | + # 3. The "default" theme is always available |
| 11 | + # |
| 12 | + # Usage: |
| 13 | + # website.accessible_themes # => [Theme, Theme, ...] |
| 14 | + # website.theme_accessible?('bologna') # => true/false |
| 15 | + # website.accessible_theme_names # => ['default', 'brisbane'] |
| 16 | + # |
| 17 | + module WebsiteThemeable |
| 18 | + extend ActiveSupport::Concern |
| 19 | + |
| 20 | + # The default theme name - always available |
| 21 | + DEFAULT_THEME = 'default'.freeze |
| 22 | + |
| 23 | + included do |
| 24 | + # Validate that theme_name is accessible to this website |
| 25 | + validate :theme_must_be_accessible, if: :theme_name_changed? |
| 26 | + end |
| 27 | + |
| 28 | + # Get all themes accessible to this website |
| 29 | + # Returns enabled themes filtered by website/tenant availability |
| 30 | + # |
| 31 | + # @return [Array<Theme>] |
| 32 | + # |
| 33 | + def accessible_themes |
| 34 | + theme_names = accessible_theme_names |
| 35 | + Theme.enabled.select { |theme| theme_names.include?(theme.name) } |
| 36 | + end |
| 37 | + |
| 38 | + # Get names of themes accessible to this website |
| 39 | + # Uses website-specific list if set, otherwise tenant defaults |
| 40 | + # |
| 41 | + # @return [Array<String>] |
| 42 | + # |
| 43 | + def accessible_theme_names |
| 44 | + themes = if available_themes.present? |
| 45 | + available_themes |
| 46 | + else |
| 47 | + TenantSettings.instance.effective_default_themes |
| 48 | + end |
| 49 | + |
| 50 | + # Always include default theme and ensure uniqueness |
| 51 | + ([DEFAULT_THEME] + themes).uniq |
| 52 | + end |
| 53 | + |
| 54 | + # Check if a specific theme is accessible to this website |
| 55 | + # |
| 56 | + # @param theme_name [String] The theme name to check |
| 57 | + # @return [Boolean] |
| 58 | + # |
| 59 | + def theme_accessible?(theme_name) |
| 60 | + return true if theme_name.to_s == DEFAULT_THEME |
| 61 | + |
| 62 | + accessible_theme_names.include?(theme_name.to_s) |
| 63 | + end |
| 64 | + |
| 65 | + # Check if this website has custom theme availability set |
| 66 | + # |
| 67 | + # @return [Boolean] |
| 68 | + # |
| 69 | + def custom_theme_availability? |
| 70 | + available_themes.present? |
| 71 | + end |
| 72 | + |
| 73 | + # Update the available themes for this website |
| 74 | + # |
| 75 | + # @param themes [Array<String>] Array of theme names |
| 76 | + # @return [Boolean] true if saved successfully |
| 77 | + # |
| 78 | + def update_available_themes(themes) |
| 79 | + update(available_themes: Array(themes).reject(&:blank?)) |
| 80 | + end |
| 81 | + |
| 82 | + # Reset to use tenant default themes |
| 83 | + # |
| 84 | + # @return [Boolean] true if saved successfully |
| 85 | + # |
| 86 | + def reset_to_default_themes |
| 87 | + update(available_themes: nil) |
| 88 | + end |
| 89 | + |
| 90 | + private |
| 91 | + |
| 92 | + def theme_must_be_accessible |
| 93 | + return if theme_name.blank? |
| 94 | + return if theme_accessible?(theme_name) |
| 95 | + |
| 96 | + errors.add(:theme_name, "is not available for this website") |
| 97 | + end |
| 98 | + end |
| 99 | +end |
0 commit comments