one-extension 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/one-extension.rb +29 -3
- metadata +1 -1
data/lib/one-extension.rb
CHANGED
@@ -1,12 +1,38 @@
|
|
1
1
|
module OneExtension
|
2
|
+
module ClassMethods
|
3
|
+
attr_accessor :one_extension_type
|
4
|
+
def one_extension(symbol)
|
5
|
+
expecting = [:inclusion, :exclusion]
|
6
|
+
unless expecting.include? symbol
|
7
|
+
raise PathError, "expecting :inclusion or :exclusion: got `:#{symbol}`"
|
8
|
+
end
|
9
|
+
self.one_extension_type = symbol
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
2
13
|
def self.included(base)
|
3
14
|
base.send(:before_filter, :one_ext_check)
|
15
|
+
base.extend(ClassMethods)
|
4
16
|
end
|
17
|
+
|
5
18
|
def one_ext_check
|
6
|
-
|
7
|
-
|
8
|
-
|
19
|
+
# only run on get request for format.html
|
20
|
+
is_get_request = request.env["REQUEST_METHOD"] == "GET"
|
21
|
+
is_html_request = [nil, 'html'].include? request.filtered_parameters["format"]
|
22
|
+
if is_get_request && is_html_request
|
23
|
+
# check if the .html extension is set
|
24
|
+
has_extension = request.env["PATH_INFO"][-5..-1] == ".html"
|
25
|
+
# get the one_extension_type
|
26
|
+
type = self.class.one_extension_type || :exclusion
|
27
|
+
if type == :exclusion && has_extension
|
28
|
+
# strip the .html
|
9
29
|
url = request.env["PATH_INFO"][0...-5]
|
30
|
+
elsif type == :inclusion && !has_extension
|
31
|
+
# add the .html
|
32
|
+
url = "#{request.env["PATH_INFO"]}.html"
|
33
|
+
end
|
34
|
+
if url.present?
|
35
|
+
# put the query string back into the URL if present
|
10
36
|
if request.env["QUERY_STRING"].length > 0
|
11
37
|
url = "#{url}?#{request.env["QUERY_STRING"]}"
|
12
38
|
end
|