middleman-router 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 979526ec50e8d23d348acf97ea76b08f39560897
4
+ data.tar.gz: 6d39307fdd6ec98bf6cadfc5738f471399aece69
5
+ SHA512:
6
+ metadata.gz: 37453c7dbd4cfe26a2236d32f6dce387c7d1aa0ecbf105818138e60c8287884aa129f345846481b03b143f72531553fa012c9ae1d26a3eebf325c1f921c0d4e1
7
+ data.tar.gz: ced32ec7694de15389c600ec729cbbdb4d7287900c98ae7a81c572a63a91a0f58bbe28c53551b8cdd6d09de6b7480786d9e7127e2119f27ef846e14e90e76215
@@ -0,0 +1,10 @@
1
+ # Ignore bundler lock file
2
+ /Gemfile.lock
3
+
4
+ # Ignore pkg folder
5
+ /pkg
6
+
7
+ /.ruby-version
8
+ /.ruby-gemset
9
+
10
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ # If you do not have OpenSSL installed, update
2
+ # the following line to use "http://" instead
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in middleman-router.gemspec
6
+ gemspec
7
+
8
+ group :development do
9
+ gem 'rake'
10
+ gem 'rdoc'
11
+ gem 'yard'
12
+ end
13
+
14
+ group :test do
15
+ gem 'cucumber'
16
+ gem 'aruba'
17
+ gem 'rspec'
18
+ end
@@ -0,0 +1,188 @@
1
+ # Middleman Router
2
+
3
+ `middleman-router` is an extension of Middleman that provides an easy way to organize and manage all of your page routes.
4
+
5
+ ## Install
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```
10
+ gem 'middleman-router'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```
16
+ $ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```
22
+ $ gem install middleman-robots
23
+ ```
24
+
25
+ Activate the plugin in the config.rb file
26
+
27
+ ```
28
+ # config.rb
29
+ activate :router
30
+ ```
31
+
32
+ By default, your routes should be stored in `lib/routes.rb` but you can customize the location if you'd like:
33
+
34
+ ```
35
+ # config.rb
36
+ activate :router, routes_location: 'routes/routes'
37
+ ```
38
+
39
+ ## Defining Your Routes
40
+
41
+ In order to use Middlman Router, you need to setup your routes files.
42
+
43
+ ```ruby
44
+ # lib/routes.rb
45
+ Router.draw do
46
+ route :home, path: ""
47
+ route :about
48
+ route :contact
49
+
50
+ route :services do
51
+ route :strategy, path: "digital-strategy"
52
+ route :design
53
+ route :development, path: "web-development"
54
+ route :search
55
+ end
56
+
57
+ route :careers, url: "https://external-link.jobboard.com"
58
+ end
59
+ ```
60
+
61
+ ## Using Routes
62
+
63
+ Once you've defined all of your routes in your routes file, you can easily call any named route by using the helper. If you have a named route of `route :about` then you can simply call:
64
+
65
+ ```ruby
66
+ = link_to "About Us", Router.about_path
67
+ ```
68
+
69
+ When the site is compiled, it will render the URL from your route. If you change a route within your routes file then there is no need to change the route helper declaration unless you change the named route (e.g. you changed `route :about` to `route :about_us`.
70
+
71
+ For nested routes, you'll need to use the parent named route as part of the path, e.g.:
72
+
73
+ ```ruby
74
+ # lib/routes.rb
75
+ route :about do
76
+ route :team
77
+ end
78
+
79
+ # html/erb file
80
+ = link_to "Team", Router.team_about_path
81
+ ```
82
+
83
+ ## Route Attributes
84
+
85
+ Defining a `route` is the base for creating a new route. There are a few options you can pass to `route` to customize it.
86
+
87
+ **name**
88
+ The name of the route is the only required attribute.
89
+
90
+ ```ruby
91
+ route :about_us
92
+ ```
93
+
94
+ This will create a route like `about_us` and point to the file `about_us.html`.
95
+
96
+ **path**
97
+ You can customize the file that a give route loads. This is useful if you prefer hypenated urls rather than snake case.
98
+
99
+ ```ruby
100
+ route :about_us, path: "about-us"
101
+ ```
102
+
103
+ This will look for a file `about-us.html` for the given route. You can point any route to whatever file or directory that you want.
104
+
105
+ ```ruby
106
+ route :about_us, path: "about/about-company"
107
+ ```
108
+ **url**
109
+ You can have routes that link to external endpoints, simple add the `url` attribute to your route:
110
+
111
+ ```ruby
112
+ route :client_login, url: "https://link-to-external-site.com"
113
+ ```
114
+
115
+ **Nested Paths**
116
+ You can easily nest paths by adding additional routes to a route block. For example:
117
+
118
+ ```ruby
119
+ route :about do
120
+ route :team
121
+ route :history
122
+ route :location
123
+ end
124
+ ```
125
+
126
+ This will create the following routes:
127
+
128
+ - /about
129
+ - /about/team
130
+ - /about/history
131
+ - /about/location
132
+
133
+ You can continue to nest if you'd like.
134
+
135
+ ```ruby
136
+ route :about do
137
+ route :team do
138
+ route :bob
139
+ route :sally do
140
+ route :bio
141
+ route :experience
142
+ end
143
+ end
144
+ end
145
+ ```
146
+
147
+ This will create the following routes:
148
+
149
+ - /about
150
+ - /about/team
151
+ - /about/team/bob
152
+ - /about/team/sally
153
+ - /about/team/sally/bio
154
+ - /about/team/sally/experience
155
+
156
+
157
+ **index**
158
+ If you want to nest pages but don't want an index route then you can exlude that from the route generation.
159
+
160
+ ```ruby
161
+ route :service, index: false do
162
+ route :design
163
+ route :development
164
+ end
165
+ ```
166
+
167
+ This will avoid trying to match the url `/services` to `services/index.html`. This is useful wen you don't have index files for a subdirectory.
168
+
169
+
170
+ **query params**
171
+ You can pass query params to any route like you do with the rails router. Simple pass the query key and value as a hash to the route.
172
+
173
+ ```ruby
174
+ = link_to "Contact Us", contact_path(ref: "work", location: "header")
175
+ #=> /contact?ref=work&location=header
176
+ ```
177
+
178
+ ## Things to note
179
+
180
+ One thing to note is that the index root needs to be setup like this:
181
+
182
+ ```ruby
183
+ Router.draw do
184
+ route :home, path: ""
185
+ end
186
+ ```
187
+
188
+ Otherwise, it'll try to route `Router.home_path` to `home.html`.
@@ -0,0 +1,14 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'cucumber/rake/task'
5
+
6
+ Cucumber::Rake::Task.new(:cucumber, 'Run features that should pass') do |t|
7
+ t.cucumber_opts = '--color --tags ~@wip --strict'
8
+ end
9
+
10
+ require 'rake/clean'
11
+
12
+ task test: ['cucumber']
13
+
14
+ task default: :test
@@ -0,0 +1,4 @@
1
+ PROJECT_ROOT_PATH = File.dirname(File.dirname(File.dirname(__FILE__)))
2
+ require 'middleman-core'
3
+ require 'middleman-core/step_definitions'
4
+ require File.join(PROJECT_ROOT_PATH, 'lib', 'middleman-router')
@@ -0,0 +1,6 @@
1
+ require "middleman-core"
2
+
3
+ Middleman::Extensions.register(:router) do
4
+ require "middleman-router/extension"
5
+ Router
6
+ end
@@ -0,0 +1,58 @@
1
+ # Require core library
2
+ require 'middleman-core'
3
+ require 'middleman-router/mapper'
4
+
5
+ # TODO
6
+ # -[ ] Auto reload the routes when they are updated
7
+ # -[ ] Need ability to add `root_path` which defaults to index.html
8
+ # -[ ] index: false should work
9
+ # -[ ] Refactor so "Router.path_name" is not required, only 'path_name'
10
+ # -[ ] Ability to print out all routes, e.g. `rake routes`
11
+ # -[ ] Ability to add "/" at the end of each route
12
+
13
+ # Extension namespace
14
+ class Router < ::Middleman::Extension
15
+ option :routes_location, 'lib/routes'
16
+
17
+ @app = nil
18
+
19
+ def initialize(app, options_hash={}, &block)
20
+ super
21
+
22
+ @app = app
23
+ require options.routes_location
24
+ end
25
+
26
+ def after_configuration
27
+ # Do something
28
+ end
29
+
30
+ class << self
31
+ def draw(&block)
32
+ mapper = Mapper.new(self)
33
+ mapper.instance_exec(&block)
34
+ nil
35
+ end
36
+
37
+ def add_route(path_name, path_url)
38
+ puts "#{path_name} => #{path_url}"
39
+ define_singleton_method(path_name) do |*args|
40
+ url(path_url, args.first)
41
+ end
42
+
43
+ self.expose_to_template << path_name
44
+ end
45
+
46
+ private
47
+
48
+ def url(path, *args)
49
+ options = args.first
50
+
51
+ if options
52
+ path = "#{path}?#{options.to_query}"
53
+ end
54
+
55
+ path
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,132 @@
1
+ class Mapper
2
+ # A Resource class for a route. Basically just a way
3
+ # to hold data for a route
4
+ class Resource
5
+ attr_accessor :name, :route_name, :path, :enabled
6
+
7
+ def initialize(options={})
8
+ @name = options[:name]
9
+ @route_name = options[:route_name]
10
+ @path = options[:path]
11
+ @enabled = options[:enabled]
12
+ end
13
+
14
+ def enabled?
15
+ @enabled == true
16
+ end
17
+ end
18
+
19
+ # Initialize the Mapper
20
+ def initialize(set)
21
+ @set = set
22
+ @resources = []
23
+ end
24
+
25
+ # Route
26
+ # Creates a new route and any child routes
27
+ #
28
+ # Usage: route :about_us, path: "about-us", as: "about"
29
+ def route(*args, &block)
30
+ resource = build_resource(args, @scope)
31
+
32
+ if resource.enabled?
33
+ @set.add_route(resource.route_name, resource.path)
34
+ end
35
+
36
+ # If a block is passed, then let's create routes
37
+ # for each child route in the block
38
+ #
39
+ if block_given?
40
+ previous_scope = @scope
41
+ @scope = resource
42
+
43
+ self.instance_exec(&block)
44
+
45
+ @scope = previous_scope
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ # Create a Resource for the route
52
+ def build_resource(args, scope=nil)
53
+ options = args.last.is_a?(::Hash) ? args.pop : {}
54
+ options[:scope] = scope if scope
55
+ enabled = !(options[:index] == false)
56
+ route = args.first
57
+
58
+ # Set the route attributes, name, path, etc
59
+ name = named(route, options)
60
+ route_name = named_route(route, options)
61
+ path = url_route(route, options)
62
+
63
+ # Create and return the route resource
64
+ Resource.new(
65
+ name: name,
66
+ route_name: route_name,
67
+ path: path,
68
+ enabled: enabled
69
+ )
70
+ end
71
+
72
+ # Returns the named_route that can be access as a
73
+ # global helper on the front-end
74
+ def named_route(route_name, options={})
75
+ label = named(route_name, options)
76
+ # Return it as a underscored symbol
77
+ "#{label}_path".underscore.to_sym
78
+ end
79
+
80
+ def named(route_name, options={})
81
+ # User can override the name
82
+ if options[:as]
83
+ route_name = options[:as]
84
+ end
85
+
86
+ # If there is a parent/scope, then let's prepend
87
+ # that route to the named_route
88
+ if options[:scope]
89
+ route_name = "#{route_name}_#{options[:scope].name}"
90
+ end
91
+
92
+ route_name
93
+ end
94
+
95
+ # Return the URL fro the route
96
+ def url_route(route_name, options={})
97
+ if options[:url]
98
+ return options[:url]
99
+ end
100
+
101
+ # Ability to customize this
102
+ if options[:path]
103
+ if options[:path].empty?
104
+ return "/"
105
+ else
106
+ path = options[:path]
107
+ end
108
+ else
109
+ path = route_name
110
+ end
111
+
112
+ # Nest under a parent/scope route if required
113
+ if options[:scope]
114
+ path = "#{options[:scope].path}#{parameterize(path)}"
115
+ else
116
+ path = "/#{parameterize(path)}"
117
+ end
118
+
119
+ "#{path}/"
120
+ end
121
+
122
+ # Parameterize a string
123
+ def parameterize(string)
124
+ string = string.to_s
125
+
126
+ # Turn unwanted chars into the separator.
127
+ string.gsub!(/[^a-z0-9\-_]+/i, '-')
128
+ string.downcase!
129
+
130
+ string
131
+ end
132
+ end
@@ -0,0 +1 @@
1
+ require 'middleman-router'
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "middleman-router"
6
+ s.version = "0.0.1"
7
+ s.licenses = ['MIT']
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["John Koht"]
10
+ s.email = ["john@kohactive.com"]
11
+ s.homepage = "https://github.com/kohactive/middleman-router"
12
+ s.summary = %q{A simple router for Middleman projects}
13
+ s.description = %q{A simple router for Middleman projets, inspired by the Rails router}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ # The version of middleman-core your extension depends on
21
+ s.add_runtime_dependency("middleman-core", '~> 4.2', '>= 4.2.1')
22
+
23
+ # Additional dependencies
24
+ # s.add_runtime_dependency("gem-name", "gem-version")
25
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: middleman-router
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - John Koht
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: middleman-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 4.2.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '4.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 4.2.1
33
+ description: A simple router for Middleman projets, inspired by the Rails router
34
+ email:
35
+ - john@kohactive.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - ".gitignore"
41
+ - Gemfile
42
+ - README.md
43
+ - Rakefile
44
+ - features/support/env.rb
45
+ - lib/middleman-router.rb
46
+ - lib/middleman-router/extension.rb
47
+ - lib/middleman-router/mapper.rb
48
+ - lib/middleman_extension.rb
49
+ - middleman-router.gemspec
50
+ homepage: https://github.com/kohactive/middleman-router
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.6.11
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: A simple router for Middleman projects
74
+ test_files:
75
+ - features/support/env.rb