Class: Testimonials::TestimonialsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/testimonials/testimonials_controller.rb

Constant Summary collapse

PER_PAGE =
50

Instance Method Summary collapse

Instance Method Details

#createObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/testimonials/testimonials_controller.rb', line 49

def create
  testimonial = build_testimonial

  error = attach_video(testimonial) || attach_avatar(testimonial)
  return render json: { errors: [error] }, status: :unprocessable_entity if error

  if testimonial.save
    # Purge only after a valid save, so a failed update can't strand a
    # review with its video already gone.
    if remove_video? && testimonial.video_attached?
      testimonial.video_file.purge
      testimonial.poster.purge if testimonial.poster_attached?
    end
    record_submission
    notify_host(testimonial)
    head :created
  else
    render json: { errors: testimonial.errors.full_messages }, status: :unprocessable_entity
  end
end

#destroyObject



44
45
46
47
# File 'app/controllers/testimonials/testimonials_controller.rb', line 44

def destroy
  @testimonial.destroy!
  redirect_to root_path, status: :see_other
end

#indexObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/testimonials/testimonials_controller.rb', line 22

def index
  @status = Testimonial::STATUSES.include?(params[:status]) ? params[:status] : 'pending'
  @kind = Testimonial::KINDS.include?(params[:kind]) ? params[:kind] : nil
  @query = params[:q].to_s.strip.presence
  @counts = tenant_scope.group(:status).count

  scope = tenant_scope.where(status: @status)
  scope = scope.where(kind: @kind) if @kind
  scope = search(scope) if @query
  @page = [params[:page].to_i, 1].max
  @testimonials = scope.newest_first.offset((@page - 1) * PER_PAGE).limit(PER_PAGE + 1).to_a
  @more = @testimonials.size > PER_PAGE
  @testimonials = @testimonials.first(PER_PAGE)
end

#showObject



37
# File 'app/controllers/testimonials/testimonials_controller.rb', line 37

def show; end

#updateObject



39
40
41
42
# File 'app/controllers/testimonials/testimonials_controller.rb', line 39

def update
  @testimonial.update!(admin_params)
  redirect_back fallback_location: testimonial_path(@testimonial), status: :see_other
end