agents_controller.rb 3.4KB

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