Няма описание http://j1x-huginn.herokuapp.com

agents_controller.rb 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. respond_to do |format|
  23. format.html { redirect_back "Agent run queued for '#{@agent.name}'" }
  24. format.json { head :ok }
  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. :default_schedule => agent.default_schedule,
  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. respond_to do |format|
  50. format.html { redirect_back "All emitted events removed for '#{@agent.name}'" }
  51. format.json { head :ok }
  52. end
  53. end
  54. def propagate
  55. details = Agent.receive! # Eventually this should probably be scoped to the current_user.
  56. respond_to do |format|
  57. format.html { redirect_back "Queued propagation calls for #{details[:event_count]} event(s) on #{details[:agent_count]} agent(s)" }
  58. format.json { head :ok }
  59. end
  60. end
  61. def show
  62. @agent = current_user.agents.find(params[:id])
  63. respond_to do |format|
  64. format.html
  65. format.json { render json: @agent }
  66. end
  67. end
  68. def new
  69. agents = current_user.agents
  70. if id = params[:id]
  71. @agent = agents.build_clone(agents.find(id))
  72. else
  73. @agent = agents.build
  74. end
  75. respond_to do |format|
  76. format.html
  77. format.json { render json: @agent }
  78. end
  79. end
  80. def edit
  81. @agent = current_user.agents.find(params[:id])
  82. end
  83. def diagram
  84. @agents = if params[:scenario_id].present?
  85. current_user.scenarios.find(params[:scenario_id]).agents.includes(:receivers)
  86. else
  87. current_user.agents.includes(:receivers)
  88. end
  89. end
  90. def create
  91. @agent = Agent.build_for_type(params[:agent].delete(:type),
  92. current_user,
  93. params[:agent])
  94. respond_to do |format|
  95. if @agent.save
  96. format.html { redirect_back "'#{@agent.name}' was successfully created." }
  97. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  98. else
  99. format.html { render action: "new" }
  100. format.json { render json: @agent.errors, status: :unprocessable_entity }
  101. end
  102. end
  103. end
  104. def update
  105. @agent = current_user.agents.find(params[:id])
  106. respond_to do |format|
  107. if @agent.update_attributes(params[:agent])
  108. format.html { redirect_back "'#{@agent.name}' was successfully updated." }
  109. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  110. else
  111. format.html { render action: "edit" }
  112. format.json { render json: @agent.errors, status: :unprocessable_entity }
  113. end
  114. end
  115. end
  116. def leave_scenario
  117. @agent = current_user.agents.find(params[:id])
  118. @scenario = current_user.scenarios.find(params[:scenario_id])
  119. @agent.scenarios.destroy(@scenario)
  120. respond_to do |format|
  121. format.html { redirect_back "'#{@agent.name}' removed from '#{@scenario.name}'" }
  122. format.json { head :no_content }
  123. end
  124. end
  125. def destroy
  126. @agent = current_user.agents.find(params[:id])
  127. @agent.destroy
  128. respond_to do |format|
  129. format.html { redirect_back "'#{@agent.name}' deleted" }
  130. format.json { head :no_content }
  131. end
  132. end
  133. protected
  134. # Sanitize params[:return] to prevent open redirect attacks, a common security issue.
  135. def redirect_back(message)
  136. if params[:return] == "show" && @agent
  137. path = agent_path(@agent)
  138. elsif params[:return] =~ /\A#{Regexp::escape scenarios_path}\/\d+\Z/
  139. path = params[:return]
  140. else
  141. path = agents_path
  142. end
  143. redirect_to path, notice: message
  144. end
  145. end