agents_controller.rb 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. class AgentsController < ApplicationController
  2. def index
  3. @agents = current_user.agents.page(params[:page])
  4. respond_to do |format|
  5. format.html
  6. format.json { render json: @agents }
  7. end
  8. end
  9. def run
  10. @agent = current_user.agents.find(params[:id])
  11. @agent.async_check
  12. redirect_to agents_path, notice: "Agent run queued"
  13. end
  14. def type_details
  15. agent = Agent.build_for_type(params[:type], current_user, {})
  16. render :json => {
  17. :can_be_scheduled => agent.can_be_scheduled?,
  18. :can_receive_events => agent.can_receive_events?,
  19. :options => agent.default_options,
  20. :description_html => agent.html_description
  21. }
  22. end
  23. def event_descriptions
  24. html = current_user.agents.find(params[:ids].split(",")).group_by(&:type).map { |type, agents|
  25. agents.map(&:html_event_description).uniq.map { |desc|
  26. "<p><strong>#{type}</strong><br />" + desc + "</p>"
  27. }
  28. }.flatten.join()
  29. render :json => { :description_html => html }
  30. end
  31. def remove_events
  32. @agent = current_user.agents.find(params[:id])
  33. @agent.events.delete_all
  34. redirect_to agents_path, notice: "All events removed"
  35. end
  36. def propagate
  37. details = Agent.receive!
  38. redirect_to agents_path, notice: "Queued propagation calls for #{details[:event_count]} event(s) on #{details[:agent_count]} agent(s)"
  39. end
  40. def show
  41. @agent = current_user.agents.find(params[:id])
  42. respond_to do |format|
  43. format.html
  44. format.json { render json: @agent }
  45. end
  46. end
  47. def new
  48. @agent = current_user.agents.build
  49. respond_to do |format|
  50. format.html
  51. format.json { render json: @agent }
  52. end
  53. end
  54. def edit
  55. @agent = current_user.agents.find(params[:id])
  56. end
  57. def diagram
  58. @agents = current_user.agents.includes(:receivers)
  59. end
  60. def create
  61. @agent = Agent.build_for_type(params[:agent].delete(:type),
  62. current_user,
  63. params[:agent])
  64. respond_to do |format|
  65. if @agent.save
  66. format.html { redirect_to agents_path, notice: 'Your Agent was successfully created.' }
  67. format.json { render json: @agent, status: :created, location: @agent }
  68. else
  69. format.html { render action: "new" }
  70. format.json { render json: @agent.errors, status: :unprocessable_entity }
  71. end
  72. end
  73. end
  74. def update
  75. @agent = current_user.agents.find(params[:id])
  76. respond_to do |format|
  77. if @agent.update_attributes(params[:agent])
  78. format.html { redirect_to agents_path, notice: 'Your Agent was successfully updated.' }
  79. format.json { head :no_content }
  80. else
  81. format.html { render action: "edit" }
  82. format.json { render json: @agent.errors, status: :unprocessable_entity }
  83. end
  84. end
  85. end
  86. def destroy
  87. @agent = current_user.agents.find(params[:id])
  88. @agent.destroy
  89. respond_to do |format|
  90. format.html { redirect_to agents_path }
  91. format.json { head :no_content }
  92. end
  93. end
  94. end