Module: OneExtension

Included in:
ApplicationController
Defined in:
lib/one-extension.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



13
14
15
16
# File 'lib/one-extension.rb', line 13

def self.included(base)
  base.send(:before_filter, :one_ext_check)
  base.extend(ClassMethods)
end

Instance Method Details

#one_ext_checkObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/one-extension.rb', line 18

def one_ext_check
  # only run on get request for format.html
  is_get_request = request.env["REQUEST_METHOD"] == "GET"
  is_html_request = [nil, 'html'].include? request.filtered_parameters["format"]
  if is_get_request && is_html_request
    # check if the .html extension is set
    has_extension = request.env["PATH_INFO"][-5..-1] == ".html"
    # get the one_extension_type
    type = self.class.one_extension_type
    if type == :exclusion && has_extension
      # strip the .html
      url = request.env["PATH_INFO"][0...-5]
    elsif type == :inclusion && !has_extension
      # add the .html
      url = "#{request.env["PATH_INFO"]}.html"
    end
    if url.present?
      # put the query string back into the URL if present
      if request.env["QUERY_STRING"].length > 0
        url = "#{url}?#{request.env["QUERY_STRING"]}"
      end
      redirect_to url, :status => 301
    end
  end
end