codelation_pages 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3745af9140e5596642e6f781076060d75357b6d8
4
+ data.tar.gz: eb89629bbdd5972394fb86a0d87171db6849e4d6
5
+ SHA512:
6
+ metadata.gz: bfdd0ca3a1ee8cb0b05f390286eb6fdc4afd5381c92023585ac536a7f0ea0aa06f503bc3f9bce5d6408435f46eeeadf44ca5c3ac611f357922b34a29065ec8ee
7
+ data.tar.gz: 5967749774d25a4f0d77b855537b101e20de17f2d9f234a24ad2d58351540dbee770a82deca887d0a1ab79865655b07f875f0ed949f1aca6a83fe10fdaa07b56
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Brian Pattison
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Codelation Pages
2
+
3
+ A extension of Rails routes mapper for automatically registering static pages.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "codelation_pages"
11
+ ```
12
+
13
+ Install the Codelation Pages gem with Bundler:
14
+
15
+ ```bash
16
+ bundle install
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ **Example:** Creating an "About Us" page.
22
+
23
+ ### Views
24
+
25
+ All static pages are served by the `PagesController`, so the HTML/ERB files live in
26
+ `app/views/pages`. The `PagesController` is a generated controller,
27
+ so you won't find it as a file in the code. If you define the controller,
28
+ it will be used instead of the generated controller.
29
+
30
+ If I want my about page to live at `http://example.com/about-us`,
31
+ I would create the view file: `app/views/pages/about_us.html.erb`.
32
+ The link helper will be available as `about_us_path`.
33
+
34
+ #### Subfolders
35
+
36
+ You can also create pages within subfolders, so if wanted to create a
37
+ "Features > Overview" page that lives at `http://example.com/features/overview`,
38
+ I would create the view file: `app/views/pages/features/overview.html.erb`.
39
+ The link helper will be available as `features_overview_path`.
40
+
41
+ Files within subfolders will not be served by the `PagesController`. They will
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`.
44
+
45
+ ### Routes
46
+
47
+ Add `draw_static_pages` to `config/routes.rb`:
48
+
49
+ ```ruby
50
+ RailsProject::Application.routes.draw do
51
+ draw_static_pages
52
+ end
53
+ ```
54
+
55
+ ## IMPORTANT
56
+
57
+ **You will need to restart your Rails server after adding new page files.***
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require "bundler/setup"
3
+ rescue LoadError
4
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
5
+ end
6
+
7
+ require "rdoc/task"
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = "rdoc"
11
+ rdoc.title = "Codelation Assets"
12
+ rdoc.options << "--line-numbers"
13
+ rdoc.rdoc_files.include("README.rdoc")
14
+ rdoc.rdoc_files.include("lib/**/*.rb")
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load "rails/tasks/engine.rake"
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require "rake/testtask"
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << "lib"
28
+ t.libs << "test"
29
+ t.pattern = "test/**/*_test.rb"
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,3 @@
1
+ module CodelationPages
2
+ VERSION = "0.0.1".freeze
3
+ end
@@ -0,0 +1,4 @@
1
+ require "extensions/action_dispatch/routing/mapper"
2
+
3
+ module CodelationPages
4
+ end
@@ -0,0 +1,73 @@
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
11
+
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
22
+
23
+ private
24
+
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)
33
+ end
34
+ end
35
+ end
36
+
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)
50
+ end
51
+ end
52
+
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
62
+ end
63
+ end
64
+
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
71
+ end
72
+ end
73
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codelation_pages
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brian Pattison
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Codelation Pages automatically registers static pages in `app/views/pages`
42
+ with dasherized routes and automatically generated controllers for pages in subfolders.
43
+ email:
44
+ - brian@brianpattison.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - lib/codelation_pages.rb
53
+ - lib/codelation_pages/version.rb
54
+ - lib/extensions/action_dispatch/routing/mapper.rb
55
+ homepage: https://github.com/codelation/codelation_pages
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.5.1
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: A extension of Rails routes mapper for automatically registering static pages.
79
+ test_files: []