Class: Bible270::AdminController

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

Overview

Small administrative panel: remove readers, adjust their completions, and move them to a given day of the plan.

Access is controlled by Bible270.config.admin_emails / admin_resolver. With neither set the panel is unreachable, and every action 404s rather than 403s so its existence isn't advertised.

Instance Method Summary collapse

Instance Method Details

#commentsObject

Everything is visible by default; this lists it all so anything unsuitable can be taken down.



46
47
48
49
50
51
52
53
# File 'app/controllers/bible270/admin_controller.rb', line 46

def comments
  scope = Comment.includes(:reader).order(created_at: :desc)
  @filter = params[:filter].to_s
  scope = scope.hidden if @filter == 'hidden'
  scope = scope.approved if @filter == 'visible'
  @comments = scope.limit(200)
  @hidden_count = Comment.hidden.count
end

#complete_throughObject



122
123
124
125
126
127
128
129
130
# File 'app/controllers/bible270/admin_controller.rb', line 122

def complete_through
  day = params[:day].to_i
  if @reader.mark_through!(day)
    redirect_to admin_reader_path(@reader),
                notice: day.zero? ? 'All check-offs cleared.' : "Marked days 1-#{day} complete."
  else
    redirect_to admin_reader_path(@reader), alert: 'Give a day between 0 and 270.'
  end
end

#destroyObject



71
72
73
74
75
# File 'app/controllers/bible270/admin_controller.rb', line 71

def destroy
  name = @reader.display_name
  @reader.destroy
  redirect_to admin_path, notice: "Removed #{name} and all their check-offs and reflections."
end

#destroy_commentObject



65
66
67
68
69
# File 'app/controllers/bible270/admin_controller.rb', line 65

def destroy_comment
  day = @comment.day
  @comment.destroy
  redirect_back_to_comments "Deleted the reflection on day #{day}."
end

#hide_commentObject



55
56
57
58
# File 'app/controllers/bible270/admin_controller.rb', line 55

def hide_comment
  @comment.hide!
  redirect_back_to_comments "Hidden #{@comment.reader.display_name}'s reflection on day #{@comment.day}."
end

#indexObject



28
29
30
31
32
33
34
35
# File 'app/controllers/bible270/admin_controller.rb', line 28

def index
  @enrollment_closed = Setting.enrollment_closed?
  @enrollment_closed_at = Setting.enrollment_closed_at
  @readers = Reader.all.to_a.sort_by(&:sort_name)
  counts = Checkoff.group(:reader_id, :day).count
  @days_completed = Hash.new(0)
  counts.each { |(rid, day), n| @days_completed[rid] += 1 if n >= Plan.required_track_count(day) }
end

#remove_avatarObject



117
118
119
120
# File 'app/controllers/bible270/admin_controller.rb', line 117

def remove_avatar
  @reader.remove_avatar!
  redirect_to admin_reader_path(@reader), notice: "Removed #{@reader.display_name}'s picture."
end

#showObject



37
38
39
40
# File 'app/controllers/bible270/admin_controller.rb', line 37

def show
  @days_completed = @reader.days_completed
  @comments = @reader.comments.order(created_at: :desc).limit(50)
end

#toggle_dayObject



132
133
134
135
136
137
138
139
# File 'app/controllers/bible270/admin_controller.rb', line 132

def toggle_day
  day = params[:day].to_i
  if @reader.toggle_day!(day)
    redirect_to admin_reader_path(@reader, anchor: "day-#{day}")
  else
    redirect_to admin_reader_path(@reader), alert: 'That day is outside the plan.'
  end
end

#unhide_commentObject



60
61
62
63
# File 'app/controllers/bible270/admin_controller.rb', line 60

def unhide_comment
  @comment.unhide!
  redirect_back_to_comments "Restored #{@comment.reader.display_name}'s reflection on day #{@comment.day}."
end

#update_enrollmentObject

---- enrolment --------------------------------------------------------



18
19
20
21
22
23
24
25
26
# File 'app/controllers/bible270/admin_controller.rb', line 18

def update_enrollment
  if params[:state] == 'closed'
    Setting.close_enrollment!
    redirect_to admin_path, notice: 'Closed to new readers. Existing readers can still sign in.'
  else
    Setting.open_enrollment!
    redirect_to admin_path, notice: 'Open to new readers.'
  end
end

#update_profileObject

Name and picture together, mirroring what a reader can change about themselves on their own profile.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/bible270/admin_controller.rb', line 100

def update_profile
  problems = []
  if (params[:first_name].present? || params[:last_name].present?) && !@reader.update_names(params[:first_name],
                                                                                            params[:last_name])
    problems << 'both a first and last name'
  end
  if params[:avatar].present? && !@reader.attach_avatar(params[:avatar])
    problems << (@reader.errors[:avatar].first || 'a valid image')
  end

  if problems.empty?
    redirect_to admin_reader_path(@reader), notice: 'Updated.'
  else
    redirect_to admin_reader_path(@reader), alert: "Needs #{problems.to_sentence}."
  end
end

#update_startObject

Put the reader on a given day as of today, or set an explicit start date.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/controllers/bible270/admin_controller.rb', line 78

def update_start
  if params[:start_date].present?
    # set_start_date!, not update_start_date!: an admin is not subject to
    # config.allow_reader_start_date, which governs what readers may do to
    # their own dates.
    if @reader.set_start_date!(params[:start_date])
      redirect_to admin_reader_path(@reader),
                  notice: "Start date set to #{@reader.started_on.strftime('%B %-d, %Y')}."
    else
      redirect_to admin_reader_path(@reader),
                  alert: "Couldn't read #{params[:start_date].inspect} as a date."
    end
  elsif params[:day].present? && @reader.restart_on!(day: params[:day])
    redirect_to admin_reader_path(@reader),
                notice: "#{@reader.display_name} is now on day #{params[:day]}."
  else
    redirect_to admin_reader_path(@reader), alert: 'Give a start date or a day between 1 and 270.'
  end
end