Sha256: 9896b4ed7d74aff90689388bacb695cb0cc849bdd07547f1e1ed614b218d508b

Contents?: true

Size: 1.65 KB

Versions: 17

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module Decidim
  # The form object that handles the data behind updating a user's
  # notifications settings in her profile page.
  class NotificationsSettingsForm < Form
    mimic :user

    attribute :email_on_notification, Boolean
    attribute :email_on_moderations, Boolean
    attribute :newsletter_notifications, Boolean
    attribute :notifications_from_followed, Boolean
    attribute :notifications_from_own_activity, Boolean
    attribute :allow_public_contact, Boolean

    def map_model(user)
      self.newsletter_notifications = user.newsletter_notifications_at.present?
      self.notifications_from_followed = ["all", "followed-only"].include? user.notification_types
      self.notifications_from_own_activity = ["all", "own-only"].include? user.notification_types
      self.allow_public_contact = user.direct_message_types == "all"
    end

    def newsletter_notifications_at
      return nil unless newsletter_notifications

      Time.current
    end

    def notification_types
      if notifications_from_followed && notifications_from_own_activity
        "all"
      elsif notifications_from_followed
        "followed-only"
      elsif notifications_from_own_activity
        "own-only"
      else
        "none"
      end
    end

    def direct_message_types
      allow_public_contact ? "all" : "followed-only"
    end

    def user_is_moderator?(user)
      Decidim.participatory_space_manifests.map do |manifest|
        participatory_space_type = manifest.model_class_name.constantize
        return true if participatory_space_type.moderators(user.organization).exists?(id: user.id)
      end
      false
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
decidim-core-0.26.8 app/forms/decidim/notifications_settings_form.rb
decidim-core-0.26.7 app/forms/decidim/notifications_settings_form.rb
decidim-core-0.26.5 app/forms/decidim/notifications_settings_form.rb
decidim-core-0.26.4 app/forms/decidim/notifications_settings_form.rb
decidim-core-0.26.3 app/forms/decidim/notifications_settings_form.rb
decidim-core-0.26.2 app/forms/decidim/notifications_settings_form.rb
decidim-core-0.26.1 app/forms/decidim/notifications_settings_form.rb
decidim-core-0.26.0 app/forms/decidim/notifications_settings_form.rb
decidim-core-0.26.0.rc2 app/forms/decidim/notifications_settings_form.rb
decidim-core-0.26.0.rc1 app/forms/decidim/notifications_settings_form.rb
decidim-core-0.25.2 app/forms/decidim/notifications_settings_form.rb
decidim-core-0.25.1 app/forms/decidim/notifications_settings_form.rb
decidim-core-0.25.0 app/forms/decidim/notifications_settings_form.rb
decidim-core-0.25.0.rc4 app/forms/decidim/notifications_settings_form.rb
decidim-core-0.25.0.rc3 app/forms/decidim/notifications_settings_form.rb
decidim-core-0.25.0.rc2 app/forms/decidim/notifications_settings_form.rb
decidim-core-0.25.0.rc1 app/forms/decidim/notifications_settings_form.rb