path_rewrite 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +26 -0
  4. data/app/controllers/path_rewrite/path_controller.rb +15 -0
  5. data/app/models/path_rewrite/path_translation.rb +4 -0
  6. data/config/routes.rb +7 -0
  7. data/db/migrate/20150618003028_create_path_rewrite_path_translations.rb +10 -0
  8. data/lib/path_rewrite.rb +6 -0
  9. data/lib/path_rewrite/configuration.rb +27 -0
  10. data/lib/path_rewrite/engine.rb +10 -0
  11. data/lib/path_rewrite/version.rb +3 -0
  12. data/lib/tasks/path_rewrite_tasks.rake +4 -0
  13. data/spec/controllers/path_rewrite/path_controller_spec.rb +30 -0
  14. data/spec/lib/path_rewrite/configuration_spec.rb +33 -0
  15. data/spec/rails_helper.rb +50 -0
  16. data/spec/rcov_exclude_list.rb +4 -0
  17. data/spec/routing/routes_spec.rb +51 -0
  18. data/spec/spec_helper.rb +83 -0
  19. data/spec/test_app/README.rdoc +28 -0
  20. data/spec/test_app/Rakefile +6 -0
  21. data/spec/test_app/app/assets/javascripts/application.js +13 -0
  22. data/spec/test_app/app/assets/stylesheets/application.css +15 -0
  23. data/spec/test_app/app/controllers/application_controller.rb +5 -0
  24. data/spec/test_app/app/helpers/application_helper.rb +2 -0
  25. data/spec/test_app/app/views/layouts/application.html.erb +14 -0
  26. data/spec/test_app/bin/bundle +3 -0
  27. data/spec/test_app/bin/rails +4 -0
  28. data/spec/test_app/bin/rake +4 -0
  29. data/spec/test_app/bin/setup +29 -0
  30. data/spec/test_app/config.ru +4 -0
  31. data/spec/test_app/config/application.rb +32 -0
  32. data/spec/test_app/config/boot.rb +5 -0
  33. data/spec/test_app/config/database.yml +25 -0
  34. data/spec/test_app/config/environment.rb +5 -0
  35. data/spec/test_app/config/environments/development.rb +41 -0
  36. data/spec/test_app/config/environments/production.rb +79 -0
  37. data/spec/test_app/config/environments/test.rb +42 -0
  38. data/spec/test_app/config/initializers/assets.rb +11 -0
  39. data/spec/test_app/config/initializers/backtrace_silencers.rb +7 -0
  40. data/spec/test_app/config/initializers/cookies_serializer.rb +3 -0
  41. data/spec/test_app/config/initializers/filter_parameter_logging.rb +4 -0
  42. data/spec/test_app/config/initializers/inflections.rb +16 -0
  43. data/spec/test_app/config/initializers/mime_types.rb +4 -0
  44. data/spec/test_app/config/initializers/session_store.rb +3 -0
  45. data/spec/test_app/config/initializers/wrap_parameters.rb +14 -0
  46. data/spec/test_app/config/locales/en.yml +23 -0
  47. data/spec/test_app/config/routes.rb +5 -0
  48. data/spec/test_app/config/secrets.yml +22 -0
  49. data/spec/test_app/db/development.sqlite3 +0 -0
  50. data/spec/test_app/db/schema.rb +23 -0
  51. data/spec/test_app/db/test.sqlite3 +0 -0
  52. data/spec/test_app/log/development.log +121 -0
  53. data/spec/test_app/log/test.log +760 -0
  54. data/spec/test_app/public/404.html +67 -0
  55. data/spec/test_app/public/422.html +67 -0
  56. data/spec/test_app/public/500.html +66 -0
  57. data/spec/test_app/public/favicon.ico +0 -0
  58. metadata +231 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 25d0011b70654e6cd76012e1245d3489adf5cfe6
4
+ data.tar.gz: 258f9d1c33e3c4fbc9ba729f0208fbe1ed04a87b
5
+ SHA512:
6
+ metadata.gz: 201b09505e619678aebb089a705d4b245f768eb77f9b1416be7995bf8d003d5bb45c30ec4f7ee2b1d0975a08c2bcaa87095b2f7365eb7a2ac02c8f68a4ad54f1
7
+ data.tar.gz: d7c3abb0f7858687395115cc45bf38b7aec16eaefe26b1faf2d0607fe768bef2e0ec403b20ef61c6b3cdd421fc12b0d156a7837373af06eaea4596a103a18b01
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Michael Tucker
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/Rakefile ADDED
@@ -0,0 +1,26 @@
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 = 'PathRewrite'
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("../spec/test_app/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
@@ -0,0 +1,15 @@
1
+ module PathRewrite
2
+ class PathController < ActionController::Base
3
+
4
+ def rewrite
5
+ rewrite = PathTranslation.find_by(old_path: "/#{params["path"]}") or raise ActionController::RoutingError.new("Not Found")
6
+
7
+ Rails.logger.debug "Redirecting #{rewrite.old_path} to #{rewrite.new_path} from request #{request.url}"
8
+
9
+ redirect_to rewrite.new_path, status: 301
10
+ end
11
+
12
+ end
13
+ end
14
+ # FEATURE FLAG (in nemo)
15
+ # TEST/REGRESSION
@@ -0,0 +1,4 @@
1
+ module PathRewrite
2
+ class PathTranslation < ActiveRecord::Base
3
+ end
4
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,7 @@
1
+ PathRewrite::Engine.routes.draw do
2
+ get "*path", to: "path#rewrite", constraints: -> (req) do
3
+ PathRewrite.configuration.check_redirect? &&
4
+ PathRewrite::PathTranslation.find_by(old_path: "/#{req.params["path"]}").present?
5
+ end
6
+
7
+ end
@@ -0,0 +1,10 @@
1
+ class CreatePathRewritePathTranslations < ActiveRecord::Migration
2
+ def change
3
+ create_table :path_rewrite_path_translations do |t|
4
+ t.string :old_path
5
+ t.string :new_path
6
+
7
+ t.timestamps null: false
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ require "path_rewrite/engine"
2
+
3
+ require "path_rewrite/configuration"
4
+
5
+ module PathRewrite
6
+ end
@@ -0,0 +1,27 @@
1
+ module PathRewrite
2
+ class Configuration
3
+
4
+ def check_redirect=(value)
5
+ @check_redirect = value
6
+ end
7
+
8
+ def check_redirect?
9
+ return @check_redirect unless @check_redirect.respond_to?(:call)
10
+
11
+ @check_redirect.call
12
+ end
13
+
14
+ end
15
+
16
+ def self.configure
17
+ yield configuration
18
+ end
19
+
20
+ def self.configuration
21
+ @configuration ||= Configuration.new
22
+ end
23
+
24
+ end
25
+
26
+ # initialize configuration
27
+ PathRewrite.configuration.check_redirect = true
@@ -0,0 +1,10 @@
1
+ module PathRewrite
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace PathRewrite
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module PathRewrite
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :path_rewrite do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,30 @@
1
+ module PathRewrite
2
+ RSpec.describe PathController, type: :controller do
3
+
4
+ # ensure requests use the engine route. alternative is to specify use_route: "path_rewrite" in every test
5
+ routes { PathRewrite::Engine.routes }
6
+
7
+ context "when a translation matches the request path" do
8
+
9
+ let(:old_path) { "test" }
10
+ let(:new_path) { "/captured" }
11
+
12
+ before { PathRewrite::PathTranslation.create!(old_path: "/#{old_path}", new_path: "#{new_path}") }
13
+
14
+ it "redirects 301 to new path" do
15
+ get :rewrite, path: old_path
16
+ expect(response).to redirect_to new_path
17
+ end
18
+
19
+ end
20
+
21
+ context "when no translations match the request path" do
22
+
23
+ it "raises routing error" do
24
+ expect { get :rewrite, path: "non_translated_path" }.to raise_error ActionController::RoutingError
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,33 @@
1
+ module PathRewrite
2
+ describe Configuration do
3
+
4
+ it "exposes a singleton through #configuration" do
5
+ expect(PathRewrite.configuration).to be PathRewrite.configuration
6
+ end
7
+
8
+ it "allows configuration of singleton through #configure block" do
9
+ PathRewrite.configure do |config|
10
+ expect(config).to be PathRewrite.configuration
11
+ end
12
+ end
13
+
14
+ context "check_redirect" do
15
+
16
+ after do
17
+ PathRewrite.configuration.check_redirect = nil
18
+ end
19
+
20
+ it "supports static value setting" do
21
+ PathRewrite.configuration.check_redirect = false
22
+ expect(PathRewrite.configuration.check_redirect?).to eq false
23
+ end
24
+
25
+ it "supports lambda setting" do
26
+ PathRewrite.configuration.check_redirect = -> { true }
27
+ expect(PathRewrite.configuration.check_redirect?).to eq true
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,50 @@
1
+ # This file is copied to spec/ when you run 'rails generate rspec:install'
2
+ ENV['RAILS_ENV'] ||= 'test'
3
+ require 'spec_helper'
4
+ require File.expand_path('../test_app/config/environment', __FILE__)
5
+ require 'rspec/rails'
6
+ # Add additional requires below this line. Rails is not loaded until this point!
7
+
8
+ # Requires supporting ruby files with custom matchers and macros, etc, in
9
+ # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
10
+ # run as spec files by default. This means that files in spec/support that end
11
+ # in _spec.rb will both be required and run as specs, causing the specs to be
12
+ # run twice. It is recommended that you do not name files matching this glob to
13
+ # end with _spec.rb. You can configure this pattern with the --pattern
14
+ # option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
15
+ #
16
+ # The following line is provided for convenience purposes. It has the downside
17
+ # of increasing the boot-up time by auto-requiring all files in the support
18
+ # directory. Alternatively, in the individual `*_spec.rb` files, manually
19
+ # require only the support files necessary.
20
+ #
21
+ # Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
22
+
23
+ # Checks for pending migrations before tests are run.
24
+ # If you are not using ActiveRecord, you can remove this line.
25
+ ActiveRecord::Migration.maintain_test_schema!
26
+
27
+ RSpec.configure do |config|
28
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
29
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
30
+
31
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
32
+ # examples within a transaction, remove the following line or assign false
33
+ # instead of true.
34
+ config.use_transactional_fixtures = true
35
+
36
+ # RSpec Rails can automatically mix in different behaviours to your tests
37
+ # based on their file location, for example enabling you to call `get` and
38
+ # `post` in specs under `spec/controllers`.
39
+ #
40
+ # You can disable this behaviour by removing the line below, and instead
41
+ # explicitly tag your specs with their type, e.g.:
42
+ #
43
+ # RSpec.describe UsersController, :type => :controller do
44
+ # # ...
45
+ # end
46
+ #
47
+ # The different available types are documented in the features, such as in
48
+ # https://relishapp.com/rspec/rspec-rails/docs
49
+ config.infer_spec_type_from_file_location!
50
+ end
@@ -0,0 +1,4 @@
1
+ @exclude_list = [
2
+ 'spec/**/*.rb',
3
+ 'db/**/*.rb'
4
+ ]
@@ -0,0 +1,51 @@
1
+ describe "routes", :type => :routing do
2
+
3
+ routes { PathRewrite::Engine.routes }
4
+
5
+ before { PathRewrite.configuration.check_redirect = true }
6
+
7
+ context "when check_redirect is configured to allow rewrites" do
8
+
9
+ context "and a matching route exists" do
10
+
11
+ before { PathRewrite::PathTranslation.create!(old_path: "/test", new_path: "/redirected" ) }
12
+
13
+ it "routes to path_controller rewrite action" do
14
+ expect(get: "/test").to route_to(controller: "path_rewrite/path", action: "rewrite", path: "test")
15
+ end
16
+
17
+ end
18
+
19
+ context "and no matching routes exist" do
20
+
21
+ it "does not route" do
22
+ expect(get: "/test").not_to be_routable
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+
29
+ context "when check_redirect is configured to disable rewrites" do
30
+
31
+ before { PathRewrite.configuration.check_redirect = false }
32
+
33
+ context "and a matching route exists" do
34
+
35
+ it "does not route" do
36
+ expect(get: "/test").not_to be_routable
37
+ end
38
+
39
+ end
40
+
41
+ context "and no matching routes exist" do
42
+
43
+ it "does not route" do
44
+ expect(get: "/test").not_to be_routable
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,83 @@
1
+ # configure code coverage
2
+ if ENV["COVERAGE"]
3
+ require_relative 'rcov_exclude_list.rb'
4
+ exlist = Dir.glob(@exclude_list)
5
+ require 'simplecov'
6
+ require 'simplecov-rcov'
7
+ SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
8
+ SimpleCov.start do
9
+ exlist.each do |p|
10
+ add_filter p
11
+ end
12
+ end
13
+ end
14
+
15
+ RSpec.configure do |config|
16
+ # rspec-expectations config goes here. You can use an alternate
17
+ # assertion/expectation library such as wrong or the stdlib/minitest
18
+ # assertions if you prefer.
19
+ config.expect_with :rspec do |expectations|
20
+ # This option will default to `true` in RSpec 4. It makes the `description`
21
+ # and `failure_message` of custom matchers include text for helper methods
22
+ # defined using `chain`, e.g.:
23
+ # be_bigger_than(2).and_smaller_than(4).description
24
+ # # => "be bigger than 2 and smaller than 4"
25
+ # ...rather than:
26
+ # # => "be bigger than 2"
27
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
28
+ end
29
+
30
+ # rspec-mocks config goes here. You can use an alternate test double
31
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
32
+ config.mock_with :rspec do |mocks|
33
+ # Prevents you from mocking or stubbing a method that does not exist on
34
+ # a real object. This is generally recommended, and will default to
35
+ # `true` in RSpec 4.
36
+ mocks.verify_partial_doubles = true
37
+ end
38
+
39
+ # Print the 10 slowest examples and example groups at the
40
+ # end of the spec run, to help surface which specs are running
41
+ # particularly slow.
42
+ config.profile_examples = 10
43
+
44
+ # Run specs in random order to surface order dependencies. If you find an
45
+ # order dependency and want to debug it, you can fix the order by providing
46
+ # the seed, which is printed after each run.
47
+ # --seed 1234
48
+ config.order = :random
49
+
50
+ # Seed global randomization in this process using the `--seed` CLI option.
51
+ # Setting this allows you to use `--seed` to deterministically reproduce
52
+ # test failures related to randomization by passing the same `--seed` value
53
+ # as the one that triggered the failure.
54
+ Kernel.srand config.seed
55
+
56
+ # The settings below are suggested to provide a good initial experience
57
+ # with RSpec, but feel free to customize to your heart's content.
58
+ =begin
59
+ # These two settings work together to allow you to limit a spec run
60
+ # to individual examples or groups you care about by tagging them with
61
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
62
+ # get run.
63
+ config.filter_run :focus
64
+ config.run_all_when_everything_filtered = true
65
+
66
+ # Limits the available syntax to the non-monkey patched syntax that is
67
+ # recommended. For more details, see:
68
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
69
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
70
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
71
+ config.disable_monkey_patching!
72
+
73
+ # Many RSpec users commonly either run the entire suite or an individual
74
+ # file, and it's useful to allow more verbose output when running an
75
+ # individual spec file.
76
+ if config.files_to_run.one?
77
+ # Use the documentation formatter for detailed output,
78
+ # unless a formatter has already been configured
79
+ # (e.g. via a command-line flag).
80
+ config.default_formatter = 'doc'
81
+ end
82
+ =end
83
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.