scenarios_controller.rb 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. class ScenariosController < ApplicationController
  2. skip_before_filter :authenticate_user!, :only => :export
  3. def index
  4. @scenarios = current_user.scenarios.page(params[:page])
  5. respond_to do |format|
  6. format.html
  7. format.json { render json: @scenarios }
  8. end
  9. end
  10. def new
  11. @scenario = current_user.scenarios.build
  12. respond_to do |format|
  13. format.html
  14. format.json { render json: @scenario }
  15. end
  16. end
  17. def show
  18. @scenario = current_user.scenarios.find(params[:id])
  19. @agents = @scenario.agents.preload(:scenarios).page(params[:page])
  20. respond_to do |format|
  21. format.html
  22. format.json { render json: @scenario }
  23. end
  24. end
  25. def share
  26. @scenario = current_user.scenarios.find(params[:id])
  27. respond_to do |format|
  28. format.html
  29. format.json { render json: @scenario }
  30. end
  31. end
  32. def export
  33. @scenario = Scenario.find(params[:id])
  34. raise ActiveRecord::RecordNotFound unless @scenario.public? || (current_user && current_user.id == @scenario.user_id)
  35. @exporter = AgentsExporter.new(:name => @scenario.name,
  36. :description => @scenario.description,
  37. :guid => @scenario.guid,
  38. :source_url => @scenario.public? && export_scenario_url(@scenario),
  39. :agents => @scenario.agents)
  40. response.headers['Content-Disposition'] = 'attachment; filename="' + @exporter.filename + '"'
  41. render :json => JSON.pretty_generate(@exporter.as_json)
  42. end
  43. def edit
  44. @scenario = current_user.scenarios.find(params[:id])
  45. respond_to do |format|
  46. format.html
  47. format.json { render json: @scenario }
  48. end
  49. end
  50. def create
  51. @scenario = current_user.scenarios.build(params[:scenario])
  52. respond_to do |format|
  53. if @scenario.save
  54. format.html { redirect_to @scenario, notice: 'This Scenario was successfully created.' }
  55. format.json { render json: @scenario, status: :created, location: @scenario }
  56. else
  57. format.html { render action: "new" }
  58. format.json { render json: @scenario.errors, status: :unprocessable_entity }
  59. end
  60. end
  61. end
  62. def update
  63. @scenario = current_user.scenarios.find(params[:id])
  64. respond_to do |format|
  65. if @scenario.update_attributes(params[:scenario])
  66. format.html { redirect_to @scenario, notice: 'This Scenario was successfully updated.' }
  67. format.json { head :no_content }
  68. else
  69. format.html { render action: "edit" }
  70. format.json { render json: @scenario.errors, status: :unprocessable_entity }
  71. end
  72. end
  73. end
  74. def destroy
  75. @scenario = current_user.scenarios.find(params[:id])
  76. @scenario.destroy
  77. respond_to do |format|
  78. format.html { redirect_to scenarios_path }
  79. format.json { head :no_content }
  80. end
  81. end
  82. end