Class: Thredded::UserPreferencesForm

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/forms/thredded/user_preferences_form.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user:, messageboard: nil, messageboards: nil, params: {}) ⇒ UserPreferencesForm

Returns a new instance of UserPreferencesForm.

Parameters:



27
28
29
30
31
32
# File 'app/forms/thredded/user_preferences_form.rb', line 27

def initialize(user:, messageboard: nil, messageboards: nil, params: {})
  @user = user
  @messageboard = messageboard
  @messageboards = messageboards
  super(params)
end

Instance Attribute Details

#messageboardThredded::Messageboard? (readonly)

Returns:



8
9
10
# File 'app/forms/thredded/user_preferences_form.rb', line 8

def messageboard
  @messageboard
end

Instance Method Details

#for_every_notifier(prefs) ⇒ Object



73
74
75
76
77
# File 'app/forms/thredded/user_preferences_form.rb', line 73

def for_every_notifier(prefs)
  Thredded.notifiers.map do |notifier|
    prefs.find { |n| n.notifier_key == notifier.key } || prefs.build(notifier_key: notifier.key)
  end
end

#messageboard_groupsObject



52
53
54
55
56
57
58
# File 'app/forms/thredded/user_preferences_form.rb', line 52

def messageboard_groups
  @messageboard_groups ||= Thredded::MessageboardGroupView.grouped(
    @messageboards,
    user: @user,
    with_unread_topics_counts: false
  )
end

#messageboard_notifications_for_followed_topicsObject



68
69
70
71
# File 'app/forms/thredded/user_preferences_form.rb', line 68

def messageboard_notifications_for_followed_topics
  return nil unless messageboard
  for_every_notifier(user_preference.messageboard_notifications_for_followed_topics.for_messageboard(messageboard))
end

#notifications_for_followed_topicsObject



64
65
66
# File 'app/forms/thredded/user_preferences_form.rb', line 64

def notifications_for_followed_topics
  for_every_notifier(user_preference.notifications_for_followed_topics)
end

#notifications_for_private_topicsObject



60
61
62
# File 'app/forms/thredded/user_preferences_form.rb', line 60

def notifications_for_private_topics
  for_every_notifier(user_preference.notifications_for_private_topics)
end

#saveBoolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/forms/thredded/user_preferences_form.rb', line 35

def save
  return false unless valid?
  Thredded::UserPreference.transaction do
    user_preference.save!

    # Update all of the messageboards' auto_follow_topics if the global preference has changed.
    if user_preference.previous_changes.include?('auto_follow_topics')
      Thredded::UserMessageboardPreference.where(user: @user)
        .update_all(auto_follow_topics: user_preference.auto_follow_topics)
      user_messageboard_preference.auto_follow_topics_will_change! if messageboard
    end

    user_messageboard_preference.save! if messageboard
  end
  true
end

#update_pathObject



79
80
81
82
83
84
85
# File 'app/forms/thredded/user_preferences_form.rb', line 79

def update_path
  if @messageboard
    Thredded::UrlsHelper.messageboard_preferences_path(@messageboard)
  else
    Thredded::UrlsHelper.global_preferences_path
  end
end