oembed_provider 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +104 -0
  5. data/Rakefile +59 -0
  6. data/VERSION +1 -0
  7. data/app/controllers/oembed_provider_controller.rb +55 -0
  8. data/config/routes.rb +8 -0
  9. data/lib/oembed_providable.rb +201 -0
  10. data/lib/oembed_provider.rb +91 -0
  11. data/lib/oembed_provider_helper.rb +18 -0
  12. data/rails/init.rb +1 -0
  13. data/test/full_2_3_5_app_with_tests/.gitignore +27 -0
  14. data/test/full_2_3_5_app_with_tests/README +1 -0
  15. data/test/full_2_3_5_app_with_tests/Rakefile +10 -0
  16. data/test/full_2_3_5_app_with_tests/app/controllers/application_controller.rb +7 -0
  17. data/test/full_2_3_5_app_with_tests/app/controllers/items_controller.rb +85 -0
  18. data/test/full_2_3_5_app_with_tests/app/controllers/photos_controller.rb +85 -0
  19. data/test/full_2_3_5_app_with_tests/app/helpers/application_helper.rb +4 -0
  20. data/test/full_2_3_5_app_with_tests/app/helpers/items_helper.rb +2 -0
  21. data/test/full_2_3_5_app_with_tests/app/helpers/photos_helper.rb +2 -0
  22. data/test/full_2_3_5_app_with_tests/app/models/item.rb +4 -0
  23. data/test/full_2_3_5_app_with_tests/app/models/photo.rb +4 -0
  24. data/test/full_2_3_5_app_with_tests/app/views/items/edit.html.erb +24 -0
  25. data/test/full_2_3_5_app_with_tests/app/views/items/index.html.erb +24 -0
  26. data/test/full_2_3_5_app_with_tests/app/views/items/new.html.erb +23 -0
  27. data/test/full_2_3_5_app_with_tests/app/views/items/show.html.erb +21 -0
  28. data/test/full_2_3_5_app_with_tests/app/views/layouts/items.html.erb +20 -0
  29. data/test/full_2_3_5_app_with_tests/app/views/layouts/photos.html.erb +18 -0
  30. data/test/full_2_3_5_app_with_tests/app/views/photos/edit.html.erb +12 -0
  31. data/test/full_2_3_5_app_with_tests/app/views/photos/index.html.erb +18 -0
  32. data/test/full_2_3_5_app_with_tests/app/views/photos/new.html.erb +11 -0
  33. data/test/full_2_3_5_app_with_tests/app/views/photos/show.html.erb +3 -0
  34. data/test/full_2_3_5_app_with_tests/config/boot.rb +110 -0
  35. data/test/full_2_3_5_app_with_tests/config/database.yml +22 -0
  36. data/test/full_2_3_5_app_with_tests/config/environment.rb +54 -0
  37. data/test/full_2_3_5_app_with_tests/config/environments/development.rb +17 -0
  38. data/test/full_2_3_5_app_with_tests/config/environments/production.rb +28 -0
  39. data/test/full_2_3_5_app_with_tests/config/environments/test.rb +28 -0
  40. data/test/full_2_3_5_app_with_tests/config/initializers/backtrace_silencers.rb +7 -0
  41. data/test/full_2_3_5_app_with_tests/config/initializers/inflections.rb +10 -0
  42. data/test/full_2_3_5_app_with_tests/config/initializers/mime_types.rb +5 -0
  43. data/test/full_2_3_5_app_with_tests/config/initializers/new_rails_defaults.rb +21 -0
  44. data/test/full_2_3_5_app_with_tests/config/initializers/oembed_provider.rb +2 -0
  45. data/test/full_2_3_5_app_with_tests/config/initializers/session_store.rb +15 -0
  46. data/test/full_2_3_5_app_with_tests/config/locales/en.yml +4 -0
  47. data/test/full_2_3_5_app_with_tests/config/locales/fr.yml +4 -0
  48. data/test/full_2_3_5_app_with_tests/config/locales/zh.yml +4 -0
  49. data/test/full_2_3_5_app_with_tests/config/locales.yml +5 -0
  50. data/test/full_2_3_5_app_with_tests/config/routes.rb +4 -0
  51. data/test/full_2_3_5_app_with_tests/db/migrate/20110212010602_create_items.rb +13 -0
  52. data/test/full_2_3_5_app_with_tests/db/migrate/20110212022722_create_photos.rb +15 -0
  53. data/test/full_2_3_5_app_with_tests/db/schema.rb +35 -0
  54. data/test/full_2_3_5_app_with_tests/db/seeds.rb +7 -0
  55. data/test/full_2_3_5_app_with_tests/doc/README_FOR_APP +2 -0
  56. data/test/full_2_3_5_app_with_tests/public/404.html +30 -0
  57. data/test/full_2_3_5_app_with_tests/public/422.html +30 -0
  58. data/test/full_2_3_5_app_with_tests/public/500.html +30 -0
  59. data/test/full_2_3_5_app_with_tests/public/favicon.ico +0 -0
  60. data/test/full_2_3_5_app_with_tests/public/images/rails.png +0 -0
  61. data/test/full_2_3_5_app_with_tests/public/index.html +275 -0
  62. data/test/full_2_3_5_app_with_tests/public/javascripts/application.js +2 -0
  63. data/test/full_2_3_5_app_with_tests/public/javascripts/controls.js +963 -0
  64. data/test/full_2_3_5_app_with_tests/public/javascripts/dragdrop.js +973 -0
  65. data/test/full_2_3_5_app_with_tests/public/javascripts/effects.js +1128 -0
  66. data/test/full_2_3_5_app_with_tests/public/javascripts/prototype.js +4320 -0
  67. data/test/full_2_3_5_app_with_tests/public/robots.txt +5 -0
  68. data/test/full_2_3_5_app_with_tests/public/stylesheets/scaffold.css +54 -0
  69. data/test/full_2_3_5_app_with_tests/script/about +4 -0
  70. data/test/full_2_3_5_app_with_tests/script/console +3 -0
  71. data/test/full_2_3_5_app_with_tests/script/dbconsole +3 -0
  72. data/test/full_2_3_5_app_with_tests/script/destroy +3 -0
  73. data/test/full_2_3_5_app_with_tests/script/generate +3 -0
  74. data/test/full_2_3_5_app_with_tests/script/performance/benchmarker +3 -0
  75. data/test/full_2_3_5_app_with_tests/script/performance/profiler +3 -0
  76. data/test/full_2_3_5_app_with_tests/script/plugin +3 -0
  77. data/test/full_2_3_5_app_with_tests/script/runner +3 -0
  78. data/test/full_2_3_5_app_with_tests/script/server +3 -0
  79. data/test/full_2_3_5_app_with_tests/test/factories.rb +14 -0
  80. data/test/full_2_3_5_app_with_tests/test/functional/oembed_provider_controller_test.rb +34 -0
  81. data/test/full_2_3_5_app_with_tests/test/integration/oembed_test.rb +88 -0
  82. data/test/full_2_3_5_app_with_tests/test/performance/browsing_test.rb +9 -0
  83. data/test/full_2_3_5_app_with_tests/test/selenium.rb +83 -0
  84. data/test/full_2_3_5_app_with_tests/test/test_helper.rb +55 -0
  85. data/test/full_2_3_5_app_with_tests/test/unit/oembed_providable_test.rb +211 -0
  86. data/test/full_2_3_5_app_with_tests/test/unit/oembed_provider_test.rb +97 -0
  87. data/test/full_2_3_5_app_with_tests/tmp/restart.txt +0 -0
  88. data/test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/init.rb +1 -0
  89. data/test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/lib/routing_filter/base.rb +31 -0
  90. data/test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/lib/routing_filter/force_extension.rb +57 -0
  91. data/test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/lib/routing_filter/locale.rb +70 -0
  92. data/test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/lib/routing_filter/pagination.rb +33 -0
  93. data/test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/lib/routing_filter/uuid_token.rb +78 -0
  94. data/test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/lib/routing_filter.rb +94 -0
  95. data/test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/spec/force_extension_spec.rb +65 -0
  96. data/test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/spec/generation_spec.rb +367 -0
  97. data/test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/spec/pagination_extension_spec.rb +19 -0
  98. data/test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/spec/recognition_spec.rb +76 -0
  99. data/test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/spec/routing_filter_spec.rb +114 -0
  100. data/test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/spec/spec_helper.rb +108 -0
  101. metadata +238 -0
@@ -0,0 +1,97 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ class OembedProviderTest < ActiveSupport::TestCase
5
+ context "The OembedProvider object" do
6
+ should "be able to set and return its provider name" do
7
+ name = "Media Conglomerate International"
8
+ OembedProvider.provider_name = name
9
+ assert_equal OembedProvider.provider_name, name
10
+ end
11
+
12
+ should "be able to set and return its provider url" do
13
+ url = "http://example.com"
14
+ OembedProvider.provider_url = url
15
+ assert_equal OembedProvider.provider_url, url
16
+ end
17
+
18
+ should "be able to set and return its cache age" do
19
+ age = "1440"
20
+ OembedProvider.cache_age = age
21
+ assert_equal OembedProvider.cache_age, age
22
+ end
23
+
24
+ should "return version as 1.0 of oEmbed spec" do
25
+ assert_equal "1.0", OembedProvider.version
26
+ end
27
+
28
+ should "have an array of base attribute keys" do
29
+ base_attributes = [:provider_url,
30
+ :provider_name,
31
+ :cache_age,
32
+ :version]
33
+
34
+ assert_equal OembedProvider.base_attributes, base_attributes
35
+ end
36
+
37
+ should "have an array of optional attribute keys" do
38
+ optional_attributes = [:title,
39
+ :author_name,
40
+ :author_url,
41
+ :thumbnail_url,
42
+ :thumbnail_width,
43
+ :thumbnail_height]
44
+
45
+ assert_equal OembedProvider.optional_attributes, optional_attributes
46
+ end
47
+
48
+ should "have a hash of keyed by oembed type with required attribute keys" do
49
+ required_attributes = {
50
+ :photo => [:url, :width, :height],
51
+ :video => [:html, :width, :height],
52
+ :link => [],
53
+ :rich => [:html, :width, :height] }
54
+
55
+ assert_equal OembedProvider.required_attributes, required_attributes
56
+ end
57
+
58
+ should "have a hash of keyed by oembed type photo with required attribute keys" do
59
+ requires = [:url, :width, :height]
60
+ assert_equal OembedProvider.required_attributes[:photo], requires
61
+ end
62
+
63
+ should "have a hash of keyed by oembed type video with required attribute keys" do
64
+ requires = [:html, :width, :height]
65
+ assert_equal OembedProvider.required_attributes[:video], requires
66
+ end
67
+
68
+ should "have a hash of keyed by oembed type link with required attribute keys" do
69
+ requires = []
70
+ assert_equal OembedProvider.required_attributes[:link], requires
71
+ end
72
+
73
+ should "have a hash of keyed by oembed type rich with required attribute keys" do
74
+ requires = [:html, :width, :height]
75
+ assert_equal OembedProvider.required_attributes[:rich], requires
76
+ end
77
+
78
+ context "have an cattr accessor for mapping controllers that aren't tableized version of models, to their corresponding models" do
79
+ should "be able to set and read controller_model_maps" do
80
+ assert OembedProvider.respond_to?(:controller_model_maps)
81
+ assert_equal Hash.new, OembedProvider.controller_model_maps
82
+ test_hash = { :images => 'StillImage' }
83
+ OembedProvider.controller_model_maps = test_hash
84
+ assert_equal test_hash, OembedProvider.controller_model_maps
85
+ test_hash[:audio] = 'AudioRecording'
86
+ OembedProvider.controller_model_maps[:audio] = 'AudioRecording'
87
+ assert_equal test_hash, OembedProvider.controller_model_maps
88
+ end
89
+ end
90
+
91
+ should "be able to answer find_provided_from with provided object based on passed in url" do
92
+ @photo = Factory.create(:photo)
93
+ url = "http://example.com/photos/#{@photo.id}"
94
+ assert_equal @photo, OembedProvider.find_provided_from(url)
95
+ end
96
+ end
97
+ end
File without changes
@@ -0,0 +1 @@
1
+ require 'routing_filter'
@@ -0,0 +1,31 @@
1
+ module RoutingFilter
2
+ class Base
3
+ class_inheritable_accessor :active
4
+ self.active = true
5
+
6
+ attr_accessor :chain, :options
7
+
8
+ def initialize(options = {})
9
+ @options = options
10
+ options.each { |name, value| instance_variable_set :"@#{name}", value }
11
+ end
12
+
13
+ def run(method, *args, &block)
14
+ _next = successor ? lambda { successor.run(method, *args, &block) } : block
15
+ active ? send(method, *args, &_next) : _next.call(*args)
16
+ end
17
+
18
+ def run_reverse(method, *args, &block)
19
+ _prev = predecessor ? lambda { predecessor.run(method, *args, &block) } : block
20
+ active ? send(method, *args, &_prev) : _prev.call(*args)
21
+ end
22
+
23
+ def predecessor
24
+ @chain.predecessor(self)
25
+ end
26
+
27
+ def successor
28
+ @chain.successor(self)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,57 @@
1
+ require 'routing_filter/base'
2
+
3
+ module RoutingFilter
4
+ class ForceExtension < Base
5
+ attr_reader :extension, :exclude
6
+
7
+ def initialize(*args)
8
+ super
9
+ @extension ||= 'html'
10
+ @exclude = %r(^(http.?://[^/]+)?\/?$) if @exclude.nil?
11
+ end
12
+
13
+ def around_recognize(path, env, &block)
14
+ extract_extension!(path) unless excluded?(path)
15
+ yield(path, env)
16
+ end
17
+
18
+ def around_generate(*args, &block)
19
+ returning yield do |result|
20
+ url = result.is_a?(Array) ? result.first : result
21
+ append_extension!(url) if append_extension?(url)
22
+ end
23
+ end
24
+
25
+ protected
26
+
27
+ def extract_extension!(path)
28
+ path.sub! /\.#{extension}$/, ''
29
+ $1
30
+ end
31
+
32
+ def append_extension?(url)
33
+ !(url.blank? || excluded?(url) || mime_extension?(url))
34
+ end
35
+
36
+ def excluded?(url)
37
+ case exclude
38
+ when Regexp
39
+ url =~ exclude
40
+ when Proc
41
+ exclude.call(url)
42
+ end
43
+ end
44
+
45
+ def mime_extension?(url)
46
+ url =~ /\.#{Mime::EXTENSION_LOOKUP.keys.join('|')}(\?|$)/
47
+ end
48
+
49
+ def append_extension!(url)
50
+ url.replace url.sub(/(\?|$)/, ".#{extension}\\1")
51
+ end
52
+
53
+ def append_page!(url, page)
54
+ url.replace "#{url}/pages/#{page}"
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,70 @@
1
+ require 'i18n'
2
+ require 'routing_filter/base'
3
+
4
+ module RoutingFilter
5
+ class Locale < Base
6
+ @@include_default_locale = true
7
+ cattr_writer :include_default_locale
8
+
9
+ class << self
10
+ def include_default_locale?
11
+ @@include_default_locale
12
+ end
13
+
14
+ def locales
15
+ @@locales ||= I18n.available_locales.map(&:to_sym)
16
+ end
17
+
18
+ def locales=(locales)
19
+ @@locales = locales.map(&:to_sym)
20
+ end
21
+
22
+ def locales_pattern
23
+ @@locales_pattern ||= %r(^/(#{self.locales.map { |l| Regexp.escape(l.to_s) }.join('|')})(?=/|$))
24
+ end
25
+ end
26
+
27
+ def around_recognize(path, env, &block)
28
+ locale = extract_locale!(path) # remove the locale from the beginning of the path
29
+ returning yield do |params| # invoke the given block (calls more filters and finally routing)
30
+ params[:locale] = locale if locale # set recognized locale to the resulting params hash
31
+ end
32
+ end
33
+
34
+ def around_generate(*args, &block)
35
+ locale = args.extract_options!.delete(:locale) # extract the passed :locale option
36
+ locale = I18n.locale if locale.nil? # default to I18n.locale when locale is nil (could also be false)
37
+ locale = nil unless valid_locale?(locale) # reset to no locale when locale is not valid
38
+
39
+ returning yield do |result|
40
+ if locale && prepend_locale?(locale)
41
+ url = result.is_a?(Array) ? result.first : result
42
+ prepend_locale!(url, locale)
43
+ end
44
+ end
45
+ end
46
+
47
+ protected
48
+
49
+ def extract_locale!(path)
50
+ path.sub! self.class.locales_pattern, ''
51
+ $1
52
+ end
53
+
54
+ def prepend_locale?(locale)
55
+ self.class.include_default_locale? || !default_locale?(locale)
56
+ end
57
+
58
+ def valid_locale?(locale)
59
+ locale && self.class.locales.include?(locale.to_sym)
60
+ end
61
+
62
+ def default_locale?(locale)
63
+ locale && locale.to_sym == I18n.default_locale.to_sym
64
+ end
65
+
66
+ def prepend_locale!(url, locale)
67
+ url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{locale}#{$2}" }
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,33 @@
1
+ require 'routing_filter/base'
2
+
3
+ module RoutingFilter
4
+ class Pagination < Base
5
+ def around_recognize(path, env, &block)
6
+ page = extract_page!(path)
7
+ returning yield(path, env) do |params|
8
+ params[:page] = page.to_i if page
9
+ end
10
+ end
11
+
12
+ def around_generate(*args, &block)
13
+ page = args.extract_options!.delete(:page)
14
+ returning yield do |result|
15
+ if page && page != 1
16
+ url = result.is_a?(Array) ? result.first : result
17
+ append_page!(url, page)
18
+ end
19
+ end
20
+ end
21
+
22
+ protected
23
+
24
+ def extract_page!(path)
25
+ path.sub! %r(/pages/([\d]+)/?$), ''
26
+ $1
27
+ end
28
+
29
+ def append_page!(url, page)
30
+ url.sub!(/($|\?)/) { "/pages/#{page}#{$1}" }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,78 @@
1
+ # This is a routing filter that extracts UUIDs from urls and exposes them via
2
+ # params[:uuid_token]
3
+ #
4
+ # URL scheme: http://example.com/d00fbbd1-82b6-4c1a-a57d-098d529d6854/your_route
5
+ #
6
+ # To enable this filter add map.filter :uuid_token to your routes.rb
7
+ #
8
+ # To make your restful path helpers use this :uuid_token use:
9
+ # new_post_path(:uuid_token => params[:uuid_token])
10
+ #
11
+ # If don't want to pass the :uuid_token manually you can add a custom module
12
+ # like this to your RAILS_ROOT/lib directory:
13
+ #
14
+ # module AccessToken
15
+ #
16
+ # class << self
17
+ # def current=(token)
18
+ # Thread.current[:uuid_token] = token
19
+ # end
20
+ #
21
+ # def current
22
+ # Thread.current[:uuid_token] ||= ""
23
+ # end
24
+ # end
25
+ #
26
+ # end
27
+ #
28
+ # Now in your application_controller you can set a before_filter which sets that
29
+ # token for every request:
30
+ #
31
+ # before_filter :set_token
32
+ #
33
+ # protected
34
+ #
35
+ # def set_token
36
+ # AccessToken.current = params[:uuid_token] if params[:uuid_token]
37
+ # end
38
+ #
39
+ # As you can see below in the around_generate method, if you don't provide a
40
+ # :uuid_token argument for your restful path helpers it will try to get the
41
+ # current :uuid_token from the AccessToken module.
42
+
43
+ require 'routing_filter/base'
44
+
45
+ module RoutingFilter
46
+ class UuidToken < Base
47
+
48
+ def around_recognize(path, env, &block)
49
+ token = extract_token!(path) # remove the token from the beginning of the path
50
+ returning yield do |params| # invoke the given block (calls more filters and finally routing)
51
+ params[:uuid_token] = token if token # set recognized token to the resulting params hash
52
+ end
53
+ end
54
+
55
+ def around_generate(*args, &block)
56
+ token = args.extract_options!.delete(:uuid_token) # extract the passed :token option
57
+ token = AccessToken.current if AccessToken && token.nil? # default to AccessToken.current when token is nil (could also be false)
58
+
59
+ returning yield do |result|
60
+ if token
61
+ url = result.is_a?(Array) ? result.first : result
62
+ prepend_token!(url, token)
63
+ end
64
+ end
65
+ end
66
+
67
+ protected
68
+
69
+ def extract_token!(path)
70
+ path.sub! /([a-z\d]{8}\-[a-z\d]{4}\-[a-z\d]{4}\-[a-z\d]{4}\-[a-z\d]{12})\//, ''
71
+ $1
72
+ end
73
+
74
+ def prepend_token!(url, token)
75
+ url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{token}#{$2}" }
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,94 @@
1
+ module RoutingFilter
2
+ mattr_accessor :active
3
+ @@active = true
4
+
5
+ class Chain < Array
6
+ def << (filter)
7
+ filter.chain = self
8
+ super
9
+ end
10
+
11
+ def run(method, *args, &final)
12
+ RoutingFilter.active && !first.nil? ? first.run(method, *args, &final) : final.call
13
+ end
14
+
15
+ def run_reverse(method, *args, &final)
16
+ RoutingFilter.active && !last.nil? ? last.run_reverse(method, *args, &final) : final.call
17
+ end
18
+
19
+ def predecessor(filter)
20
+ ix = index(filter)
21
+ ix > 0 ? self[ix - 1] : nil
22
+ end
23
+
24
+ def successor(filter)
25
+ ix = index(filter)
26
+ ix < length - 1 ? self[ix + 1] : nil
27
+ end
28
+ end
29
+ end
30
+
31
+ # allows to install a filter to the route set by calling: map.filter 'locale'
32
+ ActionController::Routing::RouteSet::Mapper.class_eval do
33
+ def filter(name, options = {})
34
+ require options.delete(:file) || "routing_filter/#{name}"
35
+ klass = RoutingFilter.const_get(name.to_s.camelize)
36
+ @set.filters << klass.new(options)
37
+ end
38
+ end
39
+
40
+ # same here for the optimized url generation in named routes
41
+ ActionController::Routing::RouteSet::NamedRouteCollection.class_eval do
42
+ # gosh. monkey engineering optimization code
43
+ def generate_optimisation_block_with_filtering(*args)
44
+ code = generate_optimisation_block_without_filtering(*args)
45
+ if match = code.match(%r(^return (.*) if (.*)))
46
+ # returned string must not contain newlines, or we'll spill out of inline code comments in
47
+ # ActionController::Routing::RouteSet::NamedRouteCollection#define_url_helper
48
+ "returning(#{match[1]}) { |result|" +
49
+ " ActionController::Routing::Routes.filters.run_reverse(:around_generate, *args, &lambda{ result }) " +
50
+ "} if #{match[2]}"
51
+ end
52
+ end
53
+ alias_method_chain :generate_optimisation_block, :filtering
54
+ end
55
+
56
+ ActionController::Routing::RouteSet.class_eval do
57
+ def clear_with_filtering!
58
+ @filters.clear if @filters
59
+ clear_without_filtering!
60
+ end
61
+ alias_method_chain :clear!, :filtering
62
+
63
+ attr_writer :filters
64
+
65
+ def filters
66
+ @filters ||= RoutingFilter::Chain.new
67
+ end
68
+
69
+ def recognize_path_with_filtering(path, env = {})
70
+ path = ::URI.unescape(path.dup) # string is frozen due to memoize
71
+ filters.run(:around_recognize, path, env, &lambda{ recognize_path_without_filtering(path, env) })
72
+ end
73
+ alias_method_chain :recognize_path, :filtering
74
+
75
+ def generate_with_filtering(*args)
76
+ filters.run_reverse(:around_generate, args.first, &lambda{ generate_without_filtering(*args) })
77
+ end
78
+ alias_method_chain :generate, :filtering
79
+
80
+ # add some useful information to the request environment
81
+ # right, this is from jamis buck's excellent article about routes internals
82
+ # http://weblog.jamisbuck.org/2006/10/26/monkey-patching-rails-extending-routes-2
83
+ # TODO move this ... where?
84
+ alias_method :extract_request_environment_without_host, :extract_request_environment unless method_defined? :extract_request_environment_without_host
85
+ def extract_request_environment(request)
86
+ returning extract_request_environment_without_host(request) do |env|
87
+ env.merge! :host => request.host,
88
+ :port => request.port,
89
+ :host_with_port => request.host_with_port,
90
+ :domain => request.domain,
91
+ :subdomain => request.subdomains.first
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,65 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe 'RoutingFilter::ForceExtension' do
4
+ include RoutingFilterHelpers
5
+
6
+ before :each do
7
+ setup_environment(:force_extension)
8
+ end
9
+
10
+ describe 'url recognition' do
11
+ it 'recognizes the path /sections/1.html' do
12
+ should_recognize_path '/sections/1.html', @section_params
13
+ end
14
+
15
+ it 'recognizes the path /sections/1/articles/1.html' do
16
+ should_recognize_path '/sections/1/articles/1.html', @article_params
17
+ end
18
+
19
+ it 'does not recognize the path /sections/1/articles/1.foobar' do
20
+ lambda { @set.recognize_path('/sections/1/articles/1.foobar', {}) }.should raise_error(ActionController::RoutingError)
21
+ end
22
+ end
23
+
24
+ describe 'url generation' do
25
+ it 'appends the .html extension to generated paths (section_path)' do
26
+ section_path(:id => 1).should == '/sections/1.html'
27
+ end
28
+
29
+ it 'appends the .html extension to generated paths (section_article_path)' do
30
+ section_article_path(:section_id => 1, :id => 1).should == '/sections/1/articles/1.html'
31
+ end
32
+
33
+ it 'appends the .html extension to generated paths (admin_articles_path)' do
34
+ admin_articles_path.should == '/admin/articles.html'
35
+ end
36
+
37
+ it 'does not replace or add on an existing extension' do
38
+ section_path(:id => 1, :format => 'xml').should == '/sections/1.xml'
39
+ end
40
+
41
+ it 'works with url query params' do
42
+ section_path(:id => 1, :foo => 'bar').should == '/sections/1.html?foo=bar'
43
+ end
44
+
45
+ it 'excludes / by default' do
46
+ home_path.should == '/'
47
+ end
48
+
49
+ it 'excludes http://test.host/ by default' do
50
+ home_url.should == 'http://test.host/'
51
+ end
52
+
53
+ it 'excludes with custom regexp' do
54
+ setup_environment { |map| map.filter :force_extension, :exclude => %r(^/(admin|$)) }
55
+ home_path.should == '/'
56
+ admin_articles_path.should == '/admin/articles'
57
+ section_path(:id => 1).should == '/sections/1.html'
58
+ end
59
+
60
+ it 'does not exclude / when :exclude => false was passed' do
61
+ setup_environment { |map| map.filter :force_extension, :exclude => false }
62
+ home_path.should == '/.html'
63
+ end
64
+ end
65
+ end