checks_controller.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. module Blazer
  2. class ChecksController < BaseController
  3. before_action :set_check, only: [:edit, :update, :destroy, :run]
  4. def index
  5. state_order = [nil, "disabled", "error", "timed out", "failing", "passing"]
  6. @checks = Blazer::Check.joins(:query).includes(:query).order("blazer_queries.name, blazer_checks.id").to_a.sort_by { |q| state_order.index(q.state) || 99 }
  7. @checks.select! { |c| "#{c.query.name} #{c.emails}".downcase.include?(params[:q]) } if params[:q]
  8. end
  9. def new
  10. @check = Blazer::Check.new(query_id: params[:query_id])
  11. end
  12. def create
  13. @check = Blazer::Check.new(check_params)
  14. # use creator_id instead of creator
  15. # since we setup association without checking if column exists
  16. @check.creator = blazer_user if @check.respond_to?(:creator_id=) && blazer_user
  17. if @check.save
  18. redirect_to query_path(@check.query)
  19. else
  20. render_errors @check
  21. end
  22. end
  23. def update
  24. if @check.update(check_params)
  25. redirect_to query_path(@check.query)
  26. else
  27. render_errors @check
  28. end
  29. end
  30. def destroy
  31. @check.destroy
  32. redirect_to checks_path
  33. end
  34. def run
  35. @query = @check.query
  36. redirect_to query_path(@query)
  37. end
  38. private
  39. def check_params
  40. params.require(:check).permit(:query_id, :emails, :invert, :check_type, :schedule)
  41. end
  42. def set_check
  43. @check = Blazer::Check.find(params[:id])
  44. end
  45. end
  46. end