agents_controller.rb 3.0KB

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