agents_controller.rb 3.5KB

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