Guest User

Untitled

a guest
Mar 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. module ActionController::Renderers
  2. add :ssj_collection do |resources, options|
  3. self.content_type = Mime::SSJ
  4. json = {
  5. :href => request.url,
  6. :items => render(:ssj => resources)
  7. }
  8. json = ActiveSupport::JSON.encode(json)
  9. self.response_body = json
  10. end
  11.  
  12. add :ssj do |resource, options|
  13. options[:partial] = resource
  14.  
  15. view_context = ActionView::Base.for_controller(self)
  16. output = SsjPartialRenderer.new(view_context, options, nil).render
  17.  
  18. if resource.respond_to?(:each)
  19. # we're probably rendering a collection to be used in a collection document
  20. return output
  21. else
  22. self.content_type = Mime::SSJ
  23. self.response_body = ActiveSupport::JSON.encode(output)
  24. end
  25. end
  26.  
  27. class SsjPartialRenderer < ActionView::Partials::PartialRenderer
  28.  
  29. def render_collection
  30. @template = template = find_template
  31.  
  32. return nil if @collection.blank?
  33.  
  34. result = template ? collection_with_template(template) : collection_without_template
  35. # This forces the results into a string, so don't do it!
  36. #result.join(spacer).html_safe!
  37. end
  38.  
  39. def _find_template(path)
  40. if controller = @view.controller
  41. prefix = controller.controller_path unless path.include?(?/)
  42. end
  43.  
  44. #@view.find(path, {:formats => @view.formats}, prefix, true)
  45. # Pretend we're not a partial, so we find the non-underscored template
  46. @view.find(path, {:formats => @view.formats}, prefix, false)
  47. end
  48.  
  49. end
  50.  
  51. end
Add Comment
Please, Sign In to add comment