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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3745af9140e5596642e6f781076060d75357b6d8
4
- data.tar.gz: eb89629bbdd5972394fb86a0d87171db6849e4d6
3
+ metadata.gz: dadaf83a31dbb1dd37151f3772267d31dabc3e3d
4
+ data.tar.gz: 8c79d9b25c9c668dbcaeea7acd614cfcefd2e330
5
5
  SHA512:
6
- metadata.gz: bfdd0ca3a1ee8cb0b05f390286eb6fdc4afd5381c92023585ac536a7f0ea0aa06f503bc3f9bce5d6408435f46eeeadf44ca5c3ac611f357922b34a29065ec8ee
7
- data.tar.gz: 5967749774d25a4f0d77b855537b101e20de17f2d9f234a24ad2d58351540dbee770a82deca887d0a1ab79865655b07f875f0ed949f1aca6a83fe10fdaa07b56
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,3 +1,3 @@
1
1
  module CodelationPages
2
- VERSION = "0.0.1".freeze
2
+ VERSION = "0.0.2".freeze
3
3
  end
@@ -1,73 +1,102 @@
1
- module ActionDispatch::Routing
2
- # Hacking on ActionDispatch::Routing::Mapper provides additional methods for defining
3
- # routes. These include being able to organize routes into multiple files and
4
- # a whole lot of magic for generating routes and controllers for static pages.
5
- class Mapper
6
- # Method for organizing the routes file into multiple files
7
- # @see http://blog.arkency.com/2015/02/how-to-split-routes-dot-rb-into-smaller-parts/
8
- def draw(routes_name)
9
- instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
10
- end
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
- # Method for drawing the routes for static pages automatically.
13
- # Any .html.erb file in `app/views/pages` will be served as the dasherized file name.
14
- # @example
15
- # about_us.html.erb will be served at http:/example/about-us
16
- # and the link helper will be available as `about_us_path`
17
- def draw_static_pages
18
- draw_routes
19
- generate_controller
20
- draw_sub_pages
21
- end
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
- private
24
+ private
24
25
 
25
- # Returns a controller for serving statis files. The view path is
26
- # set so the view files can all be in the `pages` directory.
27
- def controller(directory_name = "")
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
- # Draws the routes for static pages within sub folders.
38
- # Any .html.erb file in a folder within `app/views/pages` will be served as
39
- # the dasherized folder name and file name.
40
- # @example
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
- # Draw the route using the directory and file name.
54
- # @param directory_name [String]
55
- def draw_routes(directory_name = "")
56
- page_files = Dir["#{Rails.root.join('app', 'views', 'pages', directory_name)}/*.html.erb"]
57
- page_files.each do |file_name|
58
- page = File.basename(file_name, ".html.erb")
59
- controller_name = directory_name.blank? ? "pages" : directory_name.underscore
60
- path_name = directory_name.blank? ? page : "#{directory_name.underscore}_#{page}"
61
- get "#{directory_name}/#{page}".dasherize, to: "#{controller_name}##{page}", as: path_name
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
- # Generate a controller using the directory and file name if it doesn't exist.
66
- # @param directory_name [String]
67
- def generate_controller(directory_name = "pages")
68
- controller_class_name = "#{directory_name.camelize}Controller"
69
- controller_exists = Object.const_defined?(controller_class_name)
70
- Object.const_set(controller_class_name, controller(directory_name)) unless controller_exists
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codelation_pages
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Pattison