agents_controller.rb 3.6 KB

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