Module: Course::Settings::EmailSettingsConcern

Extended by:
ActiveSupport::Concern
Included in:
AnnouncementsComponent, ForumsComponent, SurveyComponent, UsersComponent, VideosComponent
Defined in:
app/models/concerns/course/settings/email_settings_concern.rb

Overview

This concern provides common defaults for querying and persisting email notification settings for a course component.

The emails settings for the given component is assumed to be stored in the following shape in course.settings:

{ course_component_key: { emails: { item_opening: { enabled: true }, item_closing: { enabled: false }, ... } } }

To use this concern:

  • Include the concern in the settings model for the component.
  • Implement .email_setting_items and .component_class.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#email_settingsArray<Hash>

A list of concrete email settings for the component. This is used by Notifications for the notifications settings page. See Notifications#email_settings for details of the hash shape.

Returns:

  • (Array<Hash>)

    Array of setting items


72
73
74
75
76
77
78
79
# File 'app/models/concerns/course/settings/email_settings_concern.rb', line 72

def email_settings
  email_setting_items.map do |setting_key, defaults|
    user_setting = settings.settings(:emails, setting_key).enabled
    enabled = user_setting.nil? ? defaults[:enabled_by_default] : user_setting
    setting = { component: key, key: setting_key, enabled: enabled }
    respond_to?(:title) && title ? setting.merge(component_title: title) : setting
  end
end

#update_email_setting(attributes) ⇒ Object

Updates an email notification setting.

Parameters:

  • attributes (Hash)

    New setting represented by a hash with 'key' and 'enabled' keys, e.g. { 'key' => 'item_opening', 'enabled' => true }

Raises:

  • (ArgumentError)

85
86
87
88
89
90
# File 'app/models/concerns/course/settings/email_settings_concern.rb', line 85

def update_email_setting(attributes)
  setting_key = attributes['key'].to_sym
  raise ArgumentError, 'Invalid email key' unless valid_email_setting_key?(setting_key)
  settings.settings(:emails, setting_key).enabled = ActiveRecord::Type::Boolean.new.cast(attributes['enabled'])
  true
end