From f09c4caf9aa6077a922542981ddc142b7d3b418d Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Wed, 4 Nov 2026 12:48:41 -0500 Subject: [PATCH] Keep calendar toggles per user; bring back New Customer on appointments - Hide Past Weeks and Show Customer Appointments Only are saved in each user's preferences instead of the session, so they stick across logins and devices. Show Customer Appointments Only is on by default. - The new appointment form's New Customer button had no icon since Redmine 6's SVG icons, so it didn't show. It has its icon and label again, for users who may add customers. Co-Authored-By: Claude Opus 5.5 --- app/views/common/_calendar.html.erb | 2 +- .../customer_appointments/_form.html.erb | 8 +++- .../patches/calendars_controller_patch.rb | 40 +++++++++++++++---- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/app/views/common/_calendar.html.erb b/app/views/common/_calendar.html.erb index 778a5c2..037f9eb 100644 --- a/app/views/common/_calendar.html.erb +++ b/app/views/common/_calendar.html.erb @@ -1,7 +1,7 @@ <%= javascript_include_tag("calendar", plugin: :redmine_qbo_calendar) %> <% - # Toggles: kept in the session by CalendarsControllerPatch, so month links keep them + # Toggles: each user's own, kept in their preferences by CalendarsControllerPatch (always in params here) only_appointments = params[:only_appointments] == '1' hide_past_weeks = params[:hide_past_weeks] == '1' diff --git a/app/views/customer_appointments/_form.html.erb b/app/views/customer_appointments/_form.html.erb index 48979be..69b5626 100644 --- a/app/views/customer_appointments/_form.html.erb +++ b/app/views/customer_appointments/_form.html.erb @@ -24,8 +24,12 @@

<%= f.text_field :customer, label: :label_customer, class: "customer-name", autocomplete: "off", data: { autocomplete_url: "/customers/autocomplete" } %> <%= f.hidden_field :customer_id, id: "customer_id" %> - - + <%# Creates the customer in a dialog without leaving the appointment (customer-modal.js), for users who + may add customers %> + <% if User.current.allowed_to?(:add_customers, nil, global: true) %> + <%= link_to sprite_icon('add', l(:label_new_customer)), '#', id: 'inline-create-customer-btn', + class: 'icon icon-add', title: l(:label_new_customer) %> + <% end %>

diff --git a/lib/redmine_qbo_calendar/patches/calendars_controller_patch.rb b/lib/redmine_qbo_calendar/patches/calendars_controller_patch.rb index e4f4b14..8845c5d 100644 --- a/lib/redmine_qbo_calendar/patches/calendars_controller_patch.rb +++ b/lib/redmine_qbo_calendar/patches/calendars_controller_patch.rb @@ -24,14 +24,14 @@ module RedmineQboCalendar params[:month] ||= session[:qbo_calendar_month] end - # The "Hide past weeks" and "Show customer appointments only" toggles stick for the session, so - # month links (which don't carry them) keep them without a second page load - %w[only_appointments hide_past_weeks].each do |name| - key = :"qbo_calendar_#{name}" + # The "Hide past weeks" and "Show customer appointments only" toggles are each user's own setting, + # kept in their preferences (the session for anonymous users), so they stick across logins, + # browsers and month links (which don't carry them) + CALENDAR_TOGGLES.each do |name, default| if params.key?(name) - session[key] = params[name] == '1' - elsif session[key] - params[name] = '1' + save_calendar_toggle(name, params[name] == '1') + else + params[name] = calendar_toggle(name, default) ? '1' : '0' end end @@ -84,8 +84,34 @@ module RedmineQboCalendar @calendar.events = events end + # Toggle => its value until the user changes it: customer appointments only, past weeks shown + CALENDAR_TOGGLES = { 'only_appointments' => true, 'hide_past_weeks' => false }.freeze + private + def calendar_toggle_key(name) + :"qbo_calendar_#{name}" + end + + def calendar_toggle(name, default) + key = calendar_toggle_key(name) + stored = User.current.logged? ? User.current.pref[key] : session[key] + stored.nil? ? default : ActiveModel::Type::Boolean.new.cast(stored) + end + + def save_calendar_toggle(name, value) + key = calendar_toggle_key(name) + if User.current.logged? + pref = User.current.pref + return if pref[key] == (value ? '1' : '0') + + pref[key] = value ? '1' : '0' + pref.save + else + session[key] = value + end + end + def log(msg) Rails.logger.info "[CalendarsControllerPatch] #{msg}" end