view_paths.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. require 'action_view/base'
  2. module AbstractController
  3. module ViewPaths
  4. extend ActiveSupport::Concern
  5. included do
  6. class_attribute :_view_paths
  7. self._view_paths = ActionView::PathSet.new
  8. self._view_paths.freeze
  9. end
  10. delegate :template_exists?, :view_paths, :formats, :formats=,
  11. :locale, :locale=, :to => :lookup_context
  12. module ClassMethods
  13. def parent_prefixes
  14. @parent_prefixes ||= begin
  15. parent_controller = superclass
  16. prefixes = []
  17. until parent_controller.abstract?
  18. prefixes << parent_controller.controller_path
  19. parent_controller = parent_controller.superclass
  20. end
  21. prefixes
  22. end
  23. end
  24. end
  25. # The prefixes used in render "foo" shortcuts.
  26. def _prefixes
  27. @_prefixes ||= begin
  28. parent_prefixes = self.class.parent_prefixes
  29. parent_prefixes.dup.unshift(controller_path)
  30. end
  31. end
  32. # LookupContext is the object responsible to hold all information required to lookup
  33. # templates, i.e. view paths and details. Check ActionView::LookupContext for more
  34. # information.
  35. def lookup_context
  36. @_lookup_context ||=
  37. ActionView::LookupContext.new(self.class._view_paths, details_for_lookup, _prefixes)
  38. end
  39. def details_for_lookup
  40. { }
  41. end
  42. def append_view_path(path)
  43. lookup_context.view_paths.push(*path)
  44. end
  45. def prepend_view_path(path)
  46. lookup_context.view_paths.unshift(*path)
  47. end
  48. module ClassMethods
  49. # Append a path to the list of view paths for this controller.
  50. #
  51. # ==== Parameters
  52. # * <tt>path</tt> - If a String is provided, it gets converted into
  53. # the default view path. You may also provide a custom view path
  54. # (see ActionView::PathSet for more information)
  55. def append_view_path(path)
  56. self._view_paths = view_paths + Array(path)
  57. end
  58. # Prepend a path to the list of view paths for this controller.
  59. #
  60. # ==== Parameters
  61. # * <tt>path</tt> - If a String is provided, it gets converted into
  62. # the default view path. You may also provide a custom view path
  63. # (see ActionView::PathSet for more information)
  64. def prepend_view_path(path)
  65. self._view_paths = ActionView::PathSet.new(Array(path) + view_paths)
  66. end
  67. # A list of all of the default view paths for this controller.
  68. def view_paths
  69. _view_paths
  70. end
  71. # Set the view paths.
  72. #
  73. # ==== Parameters
  74. # * <tt>paths</tt> - If a PathSet is provided, use that;
  75. # otherwise, process the parameter into a PathSet.
  76. def view_paths=(paths)
  77. self._view_paths = ActionView::PathSet.new(Array.wrap(paths))
  78. end
  79. end
  80. end
  81. end