Class: Bible270::ProfilesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/bible270/profiles_controller.rb

Overview

A reader editing their own name — the one thing about themselves they can change. Everything else about a reader comes from how they signed in.

Instance Method Summary collapse

Instance Method Details

#editObject



9
10
11
12
13
# File 'app/controllers/bible270/profiles_controller.rb', line 9

def edit
  @names = current_reader.suggested_names
  load_comment_notification_state
  load_daily_reminder_state
end

#remove_avatarObject



53
54
55
56
# File 'app/controllers/bible270/profiles_controller.rb', line 53

def remove_avatar
  current_reader.remove_avatar!
  redirect_to profile_path, notice: 'Your picture has been removed.'
end

#updateObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/bible270/profiles_controller.rb', line 15

def update
  @comment_notifications_available = comment_notifications_available?
  @notify_on_mention = requested_comment_notifications
  @daily_reminders_available = daily_reminders_available?
  problems = []
  problems << 'both a first and last name' unless current_reader.update_names(params[:first_name],
                                                                              params[:last_name])
  if params[:bible_version].present? && !current_reader.update_bible_version(params[:bible_version])
    problems << 'a translation from the list'
  end
  source = params[:passage_source].presence || Reader::DEFAULT_PASSAGE_SOURCE
  problems << 'a reading-link source from the list' unless current_reader.update_passage_source(source)
  if params[:avatar].present? && !current_reader.attach_avatar(params[:avatar])
    problems << (current_reader.errors[:avatar].first || 'a valid image')
  end

  requested_daily_reminders = params[:daily_reminders] == '1'
  requested_daily_reminder_time = daily_reminder_time_param
  if @daily_reminders_available && !Reader.valid_daily_reminder_time?(requested_daily_reminder_time)
    problems << 'a reminder time in 24-hour HH:MM format'
  end

  if problems.empty?
    current_reader.update!(notify_on_mention: @notify_on_mention) if @comment_notifications_available
    if @daily_reminders_available
      current_reader.update!(daily_reminders: requested_daily_reminders,
                             daily_reminder_time: requested_daily_reminder_time)
    end
    redirect_to profile_path, notice: 'Your profile has been updated.'
  else
    @names = { first_name: params[:first_name], last_name: params[:last_name] }
    @daily_reminders = requested_daily_reminders
    @daily_reminder_time = requested_daily_reminder_time
    flash.now[:alert] = "Please give #{problems.to_sentence}."
    render :edit, status: :unprocessable_entity
  end
end