Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. # app/hyperloop/operations/controller_op.rb
  2.  
  3. # make sure to also require 'operations/controller_op'
  4. # at the *END* of config/initializers/hyperloop.rb
  5.  
  6. module Hyperloop
  7.  
  8. # monkey patch HyperloopController to pass controller instance from controller method to ServerOp
  9.  
  10. Engine.routes.append do
  11.  
  12. class HyperloopController < ::ApplicationController
  13. def execute_remote
  14. parsed_params = JSON.parse(params[:json]).symbolize_keys
  15. render ServerOp.run_from_client(
  16. :acting_user,
  17. self, # this gets added
  18. parsed_params[:operation],
  19. parsed_params[:params].merge(acting_user: acting_user)
  20. )
  21. end
  22.  
  23. def execute_remote_api
  24. params.require(:params).permit!
  25. parsed_params = params[:params].to_h.symbolize_keys
  26. raise AccessViolation unless parsed_params[:authorization]
  27. # likewise add self here as second param
  28. render ServerOp.run_from_client(:authorization, self, params[:operation], parsed_params)
  29. end
  30. end
  31. end unless RUBY_ENGINE == 'opal'
  32.  
  33. # monkey patch run_from_client so it accepts controller
  34.  
  35. class ServerOp < Operation
  36. def self.run_from_client(security_param, controller, operation, params)
  37. operation.constantize.class_eval do
  38. # if the params expect a controller then merge it in
  39. # and if we are passing in a controller then we can ignore the security param
  40. if _Railway.params_wrapper.method_defined?(:controller)
  41. params[:controller] = controller
  42. # but if we are not passing a controller, then we must have th security param defined
  43. elsif !_Railway.params_wrapper.method_defined?(security_param)
  44. raise AccessViolation
  45. end
  46. run(params)
  47. .then { |r| return { json: { response: serialize_response(r) } } }
  48. .fail { |e| return { json: { error: e}, status: 500 } }
  49. end
  50. rescue Exception => e
  51. { json: {error: e}, status: 500 }
  52. end
  53. end unless RUBY_ENGINE == 'opal'
  54.  
  55. # at this point you can add a controller param to a ServerOp and you will be able to use it
  56. # but to make things nice we will define a subclass that already takes the controller
  57. # and then make the Operation act like a Controller delegate
  58.  
  59. class ControllerOp < ServerOp; end
  60. class ControllerOp < ServerOp
  61. param :controller
  62. alias pre_controller_op_method_missing method_missing
  63. def method_missing(name, *args, &block)
  64. if params.controller.respond_to? name
  65. params.controller.send(name, *args, &block)
  66. else
  67. pre_controller_op_method_missing(name, *args, &block)
  68. end
  69. end
  70. end unless RUBY_ENGINE == 'opal'
  71. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement