Module: HtmlNamespacing::Plugin::Rails

Defined in:
lib/html_namespacing/plugin/rails.rb

Defined Under Namespace

Modules: Helpers

Class Method Summary collapse

Class Method Details

.handle_exception(e, template, view) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/html_namespacing/plugin/rails.rb', line 25

def self.handle_exception(e, template, view)
  if func = @options[:handle_exception_callback]
    func.call(e, template, view)
  else
    raise(e)
  end
end

.install(options = {}) ⇒ Object



11
12
13
14
# File 'lib/html_namespacing/plugin/rails.rb', line 11

def self.install(options = {})
  @options = options
  install_rails_2_3(options)
end

.install_rails_2_3(options = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/html_namespacing/plugin/rails.rb', line 94

def self.install_rails_2_3(options = {})
  if options[:javascript]
    ::ActionView::Base.class_eval do
      attr_writer(:html_namespacing_rendered_paths)
      def html_namespacing_rendered_paths
        @html_namespacing_rendered_paths ||= []
      end
    end
  end

  ::ActionView::Template.class_eval do
    def render_with_html_namespacing(view, local_assigns = {})
      html = render_without_html_namespacing(view, local_assigns)

      view.html_namespacing_rendered_paths << path_without_format_and_extension if view.respond_to?(:html_namespacing_rendered_paths)

      if HtmlNamespacing::Plugin::Rails.template_formats.include?(format)
        add_namespace_to_html(html, view)
      else
        html
      end
    end
    alias_method_chain :render, :html_namespacing

    private

    def add_namespace_to_html(html, view)
      HtmlNamespacing::add_namespace_to_html(html, html_namespace)
    rescue ArgumentError => e
      HtmlNamespacing::Plugin::Rails.handle_exception(e, self, view)
      html # unless handle_exception() raised something
    end

    def html_namespace
      HtmlNamespacing::Plugin::Rails.path_to_namespace(path_without_format_and_extension)
    end
  end
end

.javascript_optional_suffixObject



37
38
39
# File 'lib/html_namespacing/plugin/rails.rb', line 37

def self.javascript_optional_suffix
  @options[:javascript_optional_suffix]
end

.javascript_rootObject



33
34
35
# File 'lib/html_namespacing/plugin/rails.rb', line 33

def self.javascript_root
  @options[:javascript_root] || RAILS_ROOT + '/app/javascripts/views'
end

.path_to_namespace(relative_path) ⇒ Object

Called by ActionView



17
18
19
20
21
22
23
# File 'lib/html_namespacing/plugin/rails.rb', line 17

def self.path_to_namespace(relative_path)
  if func = @options[:path_to_namespace_callback]
    func.call(relative_path)
  else
    HtmlNamespacing::Plugin.default_relative_path_to_namespace(relative_path)
  end
end

.template_formatsObject



41
42
43
# File 'lib/html_namespacing/plugin/rails.rb', line 41

def self.template_formats
  @formats ||= Set.new(@options[:template_formats] || ['html'])
end