Sha256: fd38304170b0b15cf695bd0012b5eddea8ae60f2847d519c591ddedcb18a7dc9

Contents?: true

Size: 1.81 KB

Versions: 9

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true
module Thredded
  class UserPreferencesForm
    include ActiveModel::Model

    # @return [Thredded::Messageboard, nil]
    attr_reader :messageboard

    validate :validate_children

    delegate :notify_on_mention, :notify_on_mention=,
             :notify_on_message, :notify_on_message=,
             to: :user_preference

    delegate :notify_on_mention, :notify_on_mention=,
             to: :user_messageboard_preference,
             prefix: :messageboard

    # @param user [Thredded.user_class]
    # @param messageboard [Thredded::Messageboard, nil]
    def initialize(user:, messageboard: nil, params: {})
      @user = user
      @messageboard = messageboard
      super(params)
    end

    # @return [Boolean]
    def save
      return false unless valid?
      Thredded::UserPreference.transaction do
        user_preference.save!
        user_messageboard_preference.save! if messageboard
      end
      true
    end

    private

    # @return [Thredded::UserPreference]
    def user_preference
      @user_preference ||= @user.thredded_user_preference
    end

    # @return [Thredded::UserMessageboardPreference, nil]
    def user_messageboard_preference
      return nil unless @messageboard
      @user_messageboard_preference ||=
        user_preference.messageboard_preferences.find_or_initialize_by(messageboard_id: @messageboard.id)
    end

    def validate_children
      promote_errors(user_preference.errors) if user_preference.invalid?
      if messageboard && user_messageboard_preference.invalid?
        promote_errors(user_messageboard_preference.errors, :messageboard)
      end
    end

    def promote_errors(child_errors, prefix = nil)
      child_errors.each do |attribute, message|
        errors.add([prefix, attribute].compact.join('_'), message)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
thredded-0.7.0 app/forms/thredded/user_preferences_form.rb
thredded-0.6.3 app/forms/thredded/user_preferences_form.rb
thredded-0.6.2 app/forms/thredded/user_preferences_form.rb
thredded-0.6.1 app/forms/thredded/user_preferences_form.rb
thredded-0.6.0 app/forms/thredded/user_preferences_form.rb
thredded-0.5.1 app/forms/thredded/user_preferences_form.rb
thredded-0.5.0 app/forms/thredded/user_preferences_form.rb
thredded-0.4.0 app/forms/thredded/user_preferences_form.rb
thredded-0.3.2 app/forms/thredded/user_preferences_form.rb