preferences.rb 927 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::User::CalendarSubscription::Preferences < Service::Base
  3. attr_reader :user
  4. def initialize(user)
  5. super()
  6. @user = user
  7. end
  8. def execute
  9. default_preferences
  10. .merge(user_preferences)
  11. .tap { |elem| elem[:tickets][:alarm] = !!elem.dig(:tickets, :alarm) } # ensure alarm is set to false if it's not set
  12. end
  13. private
  14. def default_settings
  15. Setting
  16. .where(area: 'Defaults::CalendarSubscriptions')
  17. .where("name LIKE 'defaults_calendar_subscriptions_%'")
  18. end
  19. def default_preferences
  20. default_settings
  21. .to_h do |elem|
  22. [
  23. elem.name.delete_prefix('defaults_calendar_subscriptions_'),
  24. elem.state_current[:value]
  25. ]
  26. end
  27. .with_indifferent_access
  28. end
  29. def user_preferences
  30. user.preferences[:calendar_subscriptions] || {}
  31. end
  32. end