agents_controller.rb 3.1KB

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