From 72e512426c12af70da047cd7f67648a0ecc16974 Mon Sep 17 00:00:00 2001 From: Josh Brody Date: Wed, 23 Aug 2023 09:31:09 -0500 Subject: [PATCH 1/2] fix comparison link component --- .../comparison_link_component.html.erb | 17 +++++++++++++++++ .../ahoy_captain/comparison_link_component.rb | 16 ++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 app/components/ahoy_captain/comparison_link_component.html.erb diff --git a/app/components/ahoy_captain/comparison_link_component.html.erb b/app/components/ahoy_captain/comparison_link_component.html.erb new file mode 100644 index 0000000..67d9d84 --- /dev/null +++ b/app/components/ahoy_captain/comparison_link_component.html.erb @@ -0,0 +1,17 @@ + diff --git a/app/components/ahoy_captain/comparison_link_component.rb b/app/components/ahoy_captain/comparison_link_component.rb index 5792839..9a665bb 100644 --- a/app/components/ahoy_captain/comparison_link_component.rb +++ b/app/components/ahoy_captain/comparison_link_component.rb @@ -1,17 +1,21 @@ # frozen_string_literal: true -class AhoyCaptain::ComparisonLinkComponent < AhoyCaptain::DropdownLinkComponent +class AhoyCaptain::ComparisonLinkComponent < ViewComponent::Base include ::AhoyCaptain::CompareMode include ::AhoyCaptain::RangeOptions include ::AhoyCaptain::Rangeable + renders_many :links + renders_one :header + + attr_reader :title, :classes def initialize(title: "", classes: "btn btn-sm btn-base-100 no-underline hover:bg-base-100") @classes = classes end # cheating def title - self.with_option_content(options_for_option) + self.with_link_content(options_for_option) comparison_mode.label end @@ -22,12 +26,12 @@ def render? def options_for_option [ - (link_to "Disable Comparison", AhoyCaptain::Engine.routes.url_helpers.root_path(**helpers.search_params.merge(comparison: false))), - (link_to "Previous period", AhoyCaptain::Engine.routes.url_helpers.root_path(**helpers.search_params.merge(comparison: :previous)), class: selected(:previous, :true)), - (link_to "Year-over-year", AhoyCaptain::Engine.routes.url_helpers.root_path(**helpers.search_params.merge(comparison: :year)), class: selected(:year)), (link_to "Custom period", "javascript:customComparisonModal.showModal()", class: selected(:custom)), + (link_to "Year-over-year", AhoyCaptain::Engine.routes.url_helpers.root_path(**helpers.search_params.merge(comparison: :year)), class: selected(:year)), + (link_to "Previous period", AhoyCaptain::Engine.routes.url_helpers.root_path(**helpers.search_params.merge(comparison: :previous)), class: selected(:previous, :true)), + (link_to "Disable Comparison", AhoyCaptain::Engine.routes.url_helpers.root_path(**helpers.search_params.merge(comparison: false))), - ].join.html_safe + ].reverse.join.html_safe end private From 44e517d147a95d5e3cc2fcaccc08337a826675ce Mon Sep 17 00:00:00 2001 From: Josh Brody Date: Wed, 23 Aug 2023 10:22:27 -0500 Subject: [PATCH 2/2] wip --- .../ahoy_captain/application_controller.rb | 29 +++++++++++++++++++ app/models/ahoy_captain/i18n_config.rb | 23 +++++++++++++++ app/views/ahoy_captain/roots/show.html.erb | 2 +- app/views/ahoy_captain/stats/show.html.erb | 4 +-- config/locales/ahoy_captain/en.yml | 10 +++++++ lib/ahoy_captain/configuration.rb | 3 +- 6 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 app/models/ahoy_captain/i18n_config.rb create mode 100644 config/locales/ahoy_captain/en.yml diff --git a/app/controllers/ahoy_captain/application_controller.rb b/app/controllers/ahoy_captain/application_controller.rb index c186154..bf5923a 100644 --- a/app/controllers/ahoy_captain/application_controller.rb +++ b/app/controllers/ahoy_captain/application_controller.rb @@ -5,6 +5,8 @@ class ApplicationController < ActionController::Base include RangeOptions include Rangeable + around_action :use_ahoy_captain_locale + layout 'ahoy_captain/layouts/application' def period @@ -23,6 +25,33 @@ def period private + def use_ahoy_captain_locale(&action) + @original_i18n_config = I18n.config + I18n.config = ::AhoyCaptain::I18nConfig.new + I18n.with_locale(current_locale, &action) + ensure + I18n.config = @original_i18n_config + @original_i18n_config = nil + end + + def use_original_locale + prev_config = I18n.config + I18n.config = @original_i18n_config if @original_i18n_config + yield + ensure + I18n.config = prev_config + end + + def current_locale + if request.GET['locale'] + request.GET['locale'] + elsif params[:locale] + params[:locale] + else + I18n.default_locale + end + end + def use_details_frame if request.headers['Turbo-Frame'] == 'details' request.variant = :details diff --git a/app/models/ahoy_captain/i18n_config.rb b/app/models/ahoy_captain/i18n_config.rb new file mode 100644 index 0000000..f656578 --- /dev/null +++ b/app/models/ahoy_captain/i18n_config.rb @@ -0,0 +1,23 @@ +module AhoyCaptain + class I18nConfig < ::I18n::Config + BACKEND = I18n::Backend::Simple.new + AVAILABLE_LOCALES = AhoyCaptain::Engine.root.join("config/locales/ahoy_captain").glob("*.yml").map { |path| File.basename(path, ".yml").to_sym }.uniq + AVAILABLE_LOCALES_SET = AVAILABLE_LOCALES.inject(Set.new) { |set, locale| set << locale.to_s << locale.to_sym } + + def backend + BACKEND + end + + def available_locales + AVAILABLE_LOCALES + end + + def available_locales_set + AVAILABLE_LOCALES_SET + end + + def default_locale + AhoyCaptain.config.locale + end + end +end diff --git a/app/views/ahoy_captain/roots/show.html.erb b/app/views/ahoy_captain/roots/show.html.erb index 470321e..73f77f7 100644 --- a/app/views/ahoy_captain/roots/show.html.erb +++ b/app/views/ahoy_captain/roots/show.html.erb @@ -42,7 +42,7 @@ <% end %> - <%= render AhoyCaptain::TileComponent.new(title: 'Top Sources') do |component| %> + <%= render AhoyCaptain::TileComponent.new(title: t('ahoy_captain.tiles.titles.top_sources')) do |component| %> <% component.with_display_links do %>
diff --git a/app/views/ahoy_captain/stats/show.html.erb b/app/views/ahoy_captain/stats/show.html.erb index 0f6ef2b..bd717f0 100644 --- a/app/views/ahoy_captain/stats/show.html.erb +++ b/app/views/ahoy_captain/stats/show.html.erb @@ -1,10 +1,10 @@ <%= turbo_frame_tag :stats, data: { controller: "active-frame-link" } do %>
<% if @presenter.send(:range).realtime? %> - <%= render stats_container(@presenter.unique_visitors, stats_unique_visitors_url(search_params), "Unique Visits (30 min)", :number_with_delimiter, true) %> + <%= render stats_container(@presenter.unique_visitors, stats_unique_visitors_url(search_params), "#{t('ahoy_captain.stats.titles.unique_visits')} (30 min)", :number_with_delimiter, true) %> <%= render stats_container(@presenter.total_pageviews, stats_total_pageviews_path(search_params), "Total Pageviews (30 min)", :number_with_delimiter) %> <% else %> - <%= render stats_container(@presenter.unique_visitors, stats_unique_visitors_url(search_params), "Unique Visits", :number_with_delimiter, true) %> + <%= render stats_container(@presenter.unique_visitors, stats_unique_visitors_url(search_params), t('ahoy_captain.stats.titles.unique_visits'), :number_with_delimiter, true) %> <%= render stats_container(@presenter.total_visits, stats_total_visits_path(search_params), "Total Visits", :number_with_delimiter) %> <%= render stats_container(@presenter.total_pageviews, stats_total_pageviews_path(search_params), "Total Pageviews", :number_with_delimiter) %> <%= render stats_container(@presenter.views_per_visit, stats_views_per_visits_path(search_params), "Views per Visit", :number_with_delimiter) %> diff --git a/config/locales/ahoy_captain/en.yml b/config/locales/ahoy_captain/en.yml new file mode 100644 index 0000000..8327622 --- /dev/null +++ b/config/locales/ahoy_captain/en.yml @@ -0,0 +1,10 @@ +--- +en: + ahoy_captain: + tiles: + titles: + top_sources: Top Sources + stats: + titles: + unique_visits: Unique Visits + diff --git a/lib/ahoy_captain/configuration.rb b/lib/ahoy_captain/configuration.rb index c94be69..c8368d0 100644 --- a/lib/ahoy_captain/configuration.rb +++ b/lib/ahoy_captain/configuration.rb @@ -3,7 +3,7 @@ module AhoyCaptain class Configuration - attr_accessor :view_name, :theme, :realtime_interval + attr_accessor :view_name, :theme, :realtime_interval, :locale attr_reader :goals, :funnels, :cache, :ranges, :disabled_widgets, :event, :models, :filters, :predicate_labels def initialize @goals = GoalCollection.new @@ -35,6 +35,7 @@ def initialize @realtime_interval = 30.seconds @disabled_widgets = [] + @locale = :en end def goal(id, &block)