codelation_pages 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/codelation_pages/version.rb +1 -1
- data/lib/extensions/action_dispatch/routing/mapper.rb +89 -60
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dadaf83a31dbb1dd37151f3772267d31dabc3e3d
|
4
|
+
data.tar.gz: 8c79d9b25c9c668dbcaeea7acd614cfcefd2e330
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53449bac2a18119f1073aef8ecf2a9660d0e6b2245911341e566df2c3cca6afa9044d6b97ecd5f414c5169149272729d87c78d43c8c6cd26e57285254f10690b
|
7
|
+
data.tar.gz: 0c879d254920a6facd6e97ad1770f8869406cf8f648d4f4093499070ea7eb4383978b94c6eed533a4c02dc9f2eb8406535d8daa0d0872c606f719cfdd98d8dc4
|
data/README.md
CHANGED
@@ -40,7 +40,7 @@ The link helper will be available as `features_overview_path`.
|
|
40
40
|
|
41
41
|
Files within subfolders will not be served by the `PagesController`. They will
|
42
42
|
be served by a generated controller matching the name of the folder. In the case of our
|
43
|
-
"Features > Overview" page, the controller would be the `FeaturesController`.
|
43
|
+
"Features > Overview" page, the controller would be the `Pages::FeaturesController`.
|
44
44
|
|
45
45
|
### Routes
|
46
46
|
|
@@ -1,73 +1,102 @@
|
|
1
|
-
module ActionDispatch
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
1
|
+
module ActionDispatch
|
2
|
+
module Routing
|
3
|
+
# Hacking on ActionDispatch::Routing::Mapper provides additional methods for defining
|
4
|
+
# routes. These include being able to organize routes into multiple files and
|
5
|
+
# a whole lot of magic for generating routes and controllers for static pages.
|
6
|
+
class Mapper
|
7
|
+
# Method for organizing the routes file into multiple files
|
8
|
+
# @see http://blog.arkency.com/2015/02/how-to-split-routes-dot-rb-into-smaller-parts/
|
9
|
+
def draw(routes_name)
|
10
|
+
instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
13
|
+
# Method for drawing the routes for static pages automatically.
|
14
|
+
# Any .html.erb file in `app/views/pages` will be served as the dasherized file name.
|
15
|
+
# @example
|
16
|
+
# about_us.html.erb will be served at http:/example/about-us
|
17
|
+
# and the link helper will be available as `about_us_path`
|
18
|
+
def draw_static_pages
|
19
|
+
draw_routes
|
20
|
+
generate_controller
|
21
|
+
draw_sub_pages
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
+
private
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
Class.new(ApplicationController) do
|
29
|
-
# Defines where the controller will look for the view files.
|
30
|
-
def self.controller_path
|
31
|
-
controller_path = name.gsub("Controller", "").underscore
|
32
|
-
controller_path == "pages" ? "pages" : File.join("pages", controller_path)
|
26
|
+
# Returns an application controller for serving static files.
|
27
|
+
def controller
|
28
|
+
Class.new(ApplicationController) do
|
33
29
|
end
|
34
30
|
end
|
35
|
-
end
|
36
31
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
# about_us/team.html.erb will be served at http:/example/about-us/team
|
42
|
-
# and the link helper will be available as `about_us_team_path`
|
43
|
-
def draw_sub_pages
|
44
|
-
files = Dir["#{Rails.root.join('app', 'views', 'pages')}/*"]
|
45
|
-
directories = files.select {|file| File.directory? file }
|
46
|
-
directories.each do |directory|
|
47
|
-
directory_name = File.basename(directory)
|
48
|
-
draw_routes(directory_name)
|
49
|
-
generate_controller(directory_name)
|
32
|
+
# Returns whether or not a custom controller file exists.
|
33
|
+
# @param directory_name [String]
|
34
|
+
def custom_controller_exists(directory_name)
|
35
|
+
File.exist?(Rails.root.join("app", "controllers", "pages", "#{directory_name}_controller.rb"))
|
50
36
|
end
|
51
|
-
end
|
52
37
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
38
|
+
# Draws the routes for static pages within sub folders.
|
39
|
+
# Any .html.erb file in a folder within `app/views/pages` will be served as
|
40
|
+
# the dasherized folder name and file name.
|
41
|
+
# @example
|
42
|
+
# about_us/team.html.erb will be served at http:/example/about-us/team
|
43
|
+
# and the link helper will be available as `about_us_team_path`
|
44
|
+
def draw_sub_pages
|
45
|
+
files = Dir["#{Rails.root.join('app', 'views', 'pages')}/*"]
|
46
|
+
directories = files.select {|file| File.directory? file }
|
47
|
+
directories.each do |directory|
|
48
|
+
directory_name = File.basename(directory)
|
49
|
+
draw_routes(directory_name)
|
50
|
+
generate_controller(directory_name)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Draw the route using the directory and file name.
|
55
|
+
# @param directory_name [String]
|
56
|
+
def draw_routes(directory_name = "")
|
57
|
+
page_files = Dir["#{Rails.root.join('app', 'views', 'pages', directory_name)}/*.html.erb"]
|
58
|
+
page_files.each do |file_name|
|
59
|
+
page = File.basename(file_name, ".html.erb")
|
60
|
+
controller_name = directory_name.blank? ? "pages" : "pages/#{directory_name}"
|
61
|
+
path_name = directory_name.blank? ? page : "#{directory_name}_#{page}"
|
62
|
+
path = page == "index" ? directory_name.dasherize : "#{directory_name}/#{page}".dasherize
|
63
|
+
get path, to: "#{controller_name}##{page}", as: path_name
|
64
|
+
end
|
62
65
|
end
|
63
|
-
end
|
64
66
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
67
|
+
# Generate a controller using the directory and file name if it doesn't exist.
|
68
|
+
# @param directory_name [String]
|
69
|
+
def generate_controller(directory_name = "pages")
|
70
|
+
if directory_name == "pages"
|
71
|
+
generate_pages_controller
|
72
|
+
else
|
73
|
+
generate_custom_controller(directory_name)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Generates a controller based off the directory name if it
|
78
|
+
# doesn't exist in the `Pages` module.
|
79
|
+
# @param directory_name [String]
|
80
|
+
def generate_custom_controller(directory_name)
|
81
|
+
Object.const_set("Pages", Module.new) unless pages_module_exists
|
82
|
+
controller_class_name = "#{directory_name.camelize}Controller"
|
83
|
+
Pages.const_set(controller_class_name, controller) unless custom_controller_exists(directory_name)
|
84
|
+
end
|
85
|
+
|
86
|
+
# Generates the `PagesController` if it doesn't already exist
|
87
|
+
def generate_pages_controller
|
88
|
+
Object.const_set("PagesController", controller) unless pages_controller_exists
|
89
|
+
end
|
90
|
+
|
91
|
+
# Returns whether or not the PagesController file exists.
|
92
|
+
def pages_controller_exists
|
93
|
+
File.exist?(Rails.root.join("app", "controllers", "pages_controller.rb"))
|
94
|
+
end
|
95
|
+
|
96
|
+
# Returns whether or not the pages module directory exists
|
97
|
+
def pages_module_exists
|
98
|
+
File.exist?(Rails.root.join("app", "controllers", "pages"))
|
99
|
+
end
|
71
100
|
end
|
72
101
|
end
|
73
102
|
end
|