friendly_routes 0.1.0

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: 70334657055066f9b06ff494e4da152f2b550ff7
4
+ data.tar.gz: bd6ce37d205ed5eff3f93f9664f20fefa81a4a86
5
+ SHA512:
6
+ metadata.gz: 3686f6b415ef16fee8ac88055b9446e37eb218bbabbb61c2fc432a05449299415fc4856bcb0fd7e175c72bcce6ebc93c8e27655c5a4db2eb42247edaa19e4c68
7
+ data.tar.gz: 0b4fb915304f26dccdef5063ad73dbfef4970656bb2065372a477e0085ef47a9df53cd59dea55e8fb95609439b09df7788ab2d2d03eb22538eb031f3003b1077
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Roman Gritsay
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,85 @@
1
+ # Friendly Routes
2
+ Friendly Routes creates DSL for creating rails routes with human friendly URLs
3
+
4
+ [![Code Climate](https://codeclimate.com/github/RoM4iK/friendly_routes/badges/gpa.svg)](https://codeclimate.com/github/RoM4iK/friendly_routes)
5
+ [![Build Status](https://travis-ci.org/RoM4iK/friendly_routes.svg?branch=master)](https://travis-ci.org/RoM4iK/friendly_routes)
6
+ ## Usage
7
+ How to use my plugin.
8
+
9
+ ## Installation
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'friendly_routes'
14
+ ```
15
+
16
+ And then execute:
17
+ ```bash
18
+ $ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+ ```bash
23
+ $ gem install friendly_routes
24
+ ```
25
+ ## Usage example
26
+
27
+ ```ruby
28
+ # config/routes.rb
29
+ dummies_route = FriendlyRoutes::Route.new(:get, '/', controller: :dummies, action: :index)
30
+ dummies_route.boolean(:free, true: :free, false: :paid)
31
+ dummies_route.collection(:category_id, Category, :title)
32
+ friendly_url_for dummies_route
33
+
34
+ # app/controllers/dummies_controller.rb
35
+ class DummiesController < ApplicationController
36
+ before_action :parse_friendly_routes, only: [:index]
37
+ def index
38
+ end
39
+ end
40
+
41
+ # Categories:
42
+ # <Category id: 1, title: "lorem">
43
+ # <Category id: 2, title: "ipsum">
44
+
45
+
46
+ # GET "/free/lorem" - { free: true, category_id: 1}
47
+ # GET "/free/ipsum" - { free: true, category_id: 2}
48
+ # GET "/paid/lorem" - { free: false, category_id: 1}
49
+ # GET "/paid/ipsum" - { free: false, category_id: 2}
50
+ ```
51
+
52
+ ## Configuration
53
+ ### Routes
54
+
55
+ 1. Create new route, pass **method**, **path**, **controller name**, and **action** to it.
56
+ ```ruby
57
+ dummies_route = FriendlyRoutes::Route.new(:get, '/', controller: :dummies, action: :index)
58
+ ```
59
+ 2. Create route params. *Note: params in route will be accepted in creation order*
60
+
61
+ Boolean param, pass **name**, and hash with **true** and **false** keys.
62
+ ```ruby
63
+ dummies_route.boolean(:discount, true: :true_condition, false: :false_condition)
64
+ ```
65
+
66
+ collection param, pass **name**, **collection**, and **attribute** keys.
67
+ ```ruby
68
+ dummies_route.collection(:categories, Category.where(active: true), :title)
69
+ ```
70
+ 3. Initialize route wih `friendly_url_for`
71
+ ```ruby
72
+ friendly_url_for dummies_route
73
+ ```
74
+
75
+ ### Controllers
76
+ Simply call `parse_friendly_routes` before controller action to parse params.
77
+ ```ruby
78
+ before_action :parse_friendly_routes, only: [:index]
79
+ ```
80
+
81
+ ## Contributing
82
+ Feel free to contribute into this repository by creating pull requests.
83
+
84
+ ## License
85
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rdoc/task'
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'FriendlyRoutes'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ require 'bundler/gem_tasks'
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyRoutes
4
+ module Dispatcher
5
+ def friendly_url_for(route)
6
+ public_send(
7
+ route.method,
8
+ route.path,
9
+ controller: route.controller,
10
+ action: route.action,
11
+ friendly_route: route,
12
+ constraints: route.constraints,
13
+ as: route.as
14
+ )
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyRoutes
4
+ module Helper
5
+ def parse_friendly_routes
6
+ FriendlyRoutes::Parser.new(params).call
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyRoutes
4
+ module Params
5
+ class Base
6
+ attr_accessor :type, :name
7
+
8
+ def initialize(type, name)
9
+ @type = type
10
+ @name = name
11
+ end
12
+
13
+ def constraints
14
+ raise NotImplementedError
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyRoutes
4
+ module Params
5
+ class Boolean < Base
6
+ attr_accessor :true, :false
7
+
8
+ def initialize(name, options)
9
+ check_params(options)
10
+ super(:boolean, name)
11
+ @true = options[:true]
12
+ @false = options[:false]
13
+ end
14
+
15
+ def constraints
16
+ Regexp.new "#{@true}|#{@false}"
17
+ end
18
+
19
+ def parse(value)
20
+ value == @true
21
+ end
22
+
23
+ private
24
+
25
+ def check_params(options)
26
+ valid = options.is_a?(Hash) && options[:true] && options[:false]
27
+ raise ArgumentError, 'True and false options is required' unless valid
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyRoutes
4
+ module Params
5
+ class Collection < Base
6
+ attr_accessor :collection, :key_attr
7
+
8
+ def initialize(name, collection, key_attr)
9
+ super(:collection, name)
10
+ @collection = collection
11
+ @key_attr = key_attr
12
+ check_params
13
+ end
14
+
15
+ def constraints
16
+ Regexp.new @collection.all.map(&@key_attr).join('|')
17
+ end
18
+
19
+ def parse(value)
20
+ @collection.find_by(@key_attr => value).try(:id)
21
+ end
22
+
23
+ private
24
+
25
+ def check_params
26
+ unless @collection.try(:has_attribute?, @key_attr)
27
+ raise ArgumentError, "Collection not passed, or doesn't have passed attribute"
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyRoutes
4
+ class Parser
5
+ def initialize(params)
6
+ @params = params
7
+ @route = @params[:friendly_route]
8
+ end
9
+
10
+ def call
11
+ @route.params.each do |param|
12
+ parse(param)
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def parse(param)
19
+ value = @params[@route.prefixed_param_name(param)]
20
+ return unless value
21
+ @params[param.name] = param.parse(value)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyRoutes
4
+ class Route
5
+ attr_accessor :method, :controller, :action, :params, :prefix
6
+
7
+ def initialize(method, path, controller: nil, action: nil, prefix: 'friendly_routes')
8
+ @method = method
9
+ @original_path = path
10
+ @controller = controller
11
+ @action = action
12
+ @params = []
13
+ @prefix = prefix
14
+ end
15
+
16
+ def boolean(name, params)
17
+ @params.push(Params::Boolean.new(name, params))
18
+ end
19
+
20
+ def collection(name, collection, key_attr)
21
+ @params.push(Params::Collection.new(name, collection, key_attr))
22
+ end
23
+
24
+ def path
25
+ @original_path + mapped_params
26
+ end
27
+
28
+ def prefixed_param_name(param)
29
+ "#{@prefix}_#{param.name}"
30
+ end
31
+
32
+ def as
33
+ "#{@prefix}_#{@controller}_#{@action}"
34
+ end
35
+
36
+ def constraints
37
+ @params.map do |param|
38
+ [prefixed_param_name(param).to_sym, param.constraints]
39
+ end.to_h
40
+ end
41
+
42
+ private
43
+
44
+ def mapped_params
45
+ mapped = @params.map do |param|
46
+ ':' + prefixed_param_name(param)
47
+ end
48
+ mapped.join('/')
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyRoutes
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_routes/dispatcher'
4
+ require 'friendly_routes/helper'
5
+ require 'friendly_routes/parser'
6
+ require 'friendly_routes/route'
7
+ require 'friendly_routes/params/base'
8
+ require 'friendly_routes/params/boolean'
9
+ require 'friendly_routes/params/collection'
10
+
11
+ module FriendlyRoutes
12
+ end
13
+
14
+ ActionDispatch::Routing::Mapper.include FriendlyRoutes::Dispatcher
15
+ ActiveSupport.on_load(:action_controller) do
16
+ include FriendlyRoutes::Helper
17
+ end
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: friendly_routes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Roman Gritsay
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-31 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 5.0.0
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 5.0.0.1
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: 5.0.0
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 5.0.0.1
47
+ - !ruby/object:Gem::Dependency
48
+ name: sqlite3
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: pry
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec-rails
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: factory_girl_rails
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: database_cleaner
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: faker
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ description: Friendly routes creates DSL for creating rails routes with human friendly
132
+ URLs
133
+ email:
134
+ - w2@live.ru
135
+ executables: []
136
+ extensions: []
137
+ extra_rdoc_files: []
138
+ files:
139
+ - MIT-LICENSE
140
+ - README.md
141
+ - Rakefile
142
+ - lib/friendly_routes.rb
143
+ - lib/friendly_routes/dispatcher.rb
144
+ - lib/friendly_routes/helper.rb
145
+ - lib/friendly_routes/params/base.rb
146
+ - lib/friendly_routes/params/boolean.rb
147
+ - lib/friendly_routes/params/collection.rb
148
+ - lib/friendly_routes/parser.rb
149
+ - lib/friendly_routes/route.rb
150
+ - lib/friendly_routes/version.rb
151
+ homepage: https://github.com/RoM4iK/friendly_routes
152
+ licenses:
153
+ - MIT
154
+ metadata: {}
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ requirements: []
170
+ rubyforge_project:
171
+ rubygems_version: 2.5.1
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: Human friendly routes for Rails application
175
+ test_files: []