mholling-subdomain_routes 0.2.2 → 0.2.3

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.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 2
2
+ :patch: 3
3
3
  :major: 0
4
4
  :minor: 2
@@ -4,7 +4,7 @@ module SubdomainRoutes
4
4
  include SplitHost
5
5
 
6
6
  def self.included(base)
7
- [ :extract_request_environment, :add_route ].each { |method| base.alias_method_chain method, :subdomains }
7
+ [ :extract_request_environment, :add_route, :raise_named_route_error ].each { |method| base.alias_method_chain method, :subdomains }
8
8
  end
9
9
 
10
10
  def extract_request_environment_with_subdomains(request)
@@ -21,6 +21,20 @@ module SubdomainRoutes
21
21
  end
22
22
  with_options(options) { |routes| routes.add_route_without_subdomains(*args) }
23
23
  end
24
+
25
+ def raise_named_route_error_with_subdomains(options, named_route, named_route_name)
26
+ unless named_route.conditions[:subdomains].is_a?(Symbol)
27
+ raise_named_route_error_without_subdomains(options, named_route, named_route_name)
28
+ else
29
+ begin
30
+ options.delete(named_route.conditions[:subdomains])
31
+ raise_named_route_error_without_subdomains(options, named_route, named_route_name)
32
+ rescue ActionController::RoutingError => e
33
+ e.message << " You may also need to specify #{named_route.conditions[:subdomains].inspect} for the subdomain."
34
+ raise e
35
+ end
36
+ end
37
+ end
24
38
 
25
39
  def reserved_subdomains
26
40
  routes.map(&:reserved_subdomains).flatten.uniq
@@ -29,7 +43,7 @@ module SubdomainRoutes
29
43
 
30
44
  module Route
31
45
  def self.included(base)
32
- [ :recognition_conditions, :segment_keys, :recognition_extraction ].each { |method| base.alias_method_chain method, :subdomains }
46
+ [ :recognition_conditions, :generation_extraction, :segment_keys, :recognition_extraction ].each { |method| base.alias_method_chain method, :subdomains }
33
47
  end
34
48
 
35
49
  def recognition_conditions_with_subdomains
@@ -43,9 +57,17 @@ module SubdomainRoutes
43
57
  result
44
58
  end
45
59
 
60
+ def generation_extraction_with_subdomains
61
+ results = [ generation_extraction_without_subdomains ]
62
+ if conditions[:subdomains].is_a?(Symbol)
63
+ results << "return [nil,nil] unless hash.delete(#{conditions[:subdomains].inspect})"
64
+ end
65
+ results.compact * "\n"
66
+ end
67
+
46
68
  def segment_keys_with_subdomains
47
69
  result = segment_keys_without_subdomains
48
- result.unshift(:subdomain) if conditions[:subdomains].is_a? Symbol
70
+ result.unshift(conditions[:subdomains]) if conditions[:subdomains].is_a? Symbol
49
71
  result
50
72
  end
51
73
 
@@ -12,21 +12,23 @@ module SubdomainRoutes
12
12
  def rewrite_subdomain_options(options, host)
13
13
  if subdomains = options[:subdomains]
14
14
  old_subdomain, domain = split_host(host)
15
- new_subdomain = options.has_key?(:subdomain) ? options[:subdomain].to_param.to_s.downcase : old_subdomain
15
+ options_subdomain = options.has_key?(:subdomain) ? options[:subdomain].to_param.to_s.downcase : nil
16
+ new_subdomain = options_subdomain || old_subdomain
16
17
  begin
17
18
  case subdomains
18
19
  when Array
19
20
  unless subdomains.include? new_subdomain
20
- if subdomains.size > 1 || options.has_key?(:subdomain)
21
+ if subdomains.size > 1 || options_subdomain
21
22
  raise ActionController::RoutingError, "expected subdomain in #{subdomains.inspect}, instead got subdomain #{new_subdomain.inspect}"
22
23
  else
23
24
  new_subdomain = subdomains.first
24
25
  end
25
26
  end
26
27
  when Symbol
28
+ new_subdomain = options[subdomains].to_param.to_s.downcase
27
29
  unless new_subdomain.blank? || SubdomainRoutes.valid_subdomain?(new_subdomain)
28
30
  raise ActionController::RoutingError, "subdomain #{new_subdomain.inspect} is invalid"
29
- end
31
+ end
30
32
  end
31
33
  rescue ActionController::RoutingError => e
32
34
  raise ActionController::RoutingError, "Route for #{options.inspect} failed to generate (#{e.message})"
@@ -65,7 +67,7 @@ module SubdomainRoutes
65
67
  base::RESERVED_OPTIONS << :subdomain # untested!
66
68
  end
67
69
 
68
- def rewrite_with_subdomains(options)
70
+ def rewrite_with_subdomains(options)
69
71
  host = options[:host] || @request.host
70
72
  if options[:subdomains] && host.blank?
71
73
  raise HostNotSupplied, "Missing host to link to!"
@@ -168,7 +168,7 @@ describe "URL writing" do
168
168
  @boston = City.new
169
169
  @boston.stub!(:new_record?).and_return(false)
170
170
  @boston.stub!(:to_param).and_return("boston")
171
- @url_options = { :controller => "city/events", :action => "index", :subdomains => :city_id, :subdomain => @boston.to_param }
171
+ @url_options = { :controller => "city/events", :action => "index", :subdomains => :city_id, :city_id => @boston }
172
172
  @path_options = @url_options.merge(:only_path => true)
173
173
  end
174
174
 
@@ -197,8 +197,8 @@ describe "URL writing" do
197
197
  with_host "www.example.com" do
198
198
  lambda { city_events_url(@newyork) }.should raise_error(ActionController::RoutingError)
199
199
  lambda { city_events_path(@newyork) }.should raise_error(ActionController::RoutingError)
200
- lambda { url_for(@url_options.merge(:subdomain => @newyork.to_param)) }.should raise_error(ActionController::RoutingError)
201
- lambda { url_for(@path_options.merge(:subdomain => @newyork.to_param)) }.should raise_error(ActionController::RoutingError)
200
+ lambda { url_for(@url_options.merge(:city_id => @newyork)) }.should raise_error(ActionController::RoutingError)
201
+ lambda { url_for(@path_options.merge(:city_id => @newyork)) }.should raise_error(ActionController::RoutingError)
202
202
  end
203
203
  end
204
204
 
@@ -208,5 +208,13 @@ describe "URL writing" do
208
208
  city_events_path(@boston, :subdomain => :canberra).should == "http://boston.example.com/events"
209
209
  end
210
210
  end
211
+
212
+ it "should raise a routing error if no subdomain object is supplied to the named route" do
213
+ with_host "www.example.com" do
214
+ [ lambda { city_events_url }, lambda { city_event_url("id") } ].each do |lamb|
215
+ lamb.should raise_error(ActionController::RoutingError) { |e| e.message.should include ":city_id" }
216
+ end
217
+ end
218
+ end
211
219
  end
212
220
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mholling-subdomain_routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Hollingworth
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-01 00:00:00 -07:00
12
+ date: 2009-07-28 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -55,6 +55,7 @@ files:
55
55
  - spec/validations_spec.rb
56
56
  has_rdoc: false
57
57
  homepage: http://github.com/mholling/subdomain_routes
58
+ licenses:
58
59
  post_install_message:
59
60
  rdoc_options:
60
61
  - --charset=UTF-8
@@ -75,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
76
  requirements: []
76
77
 
77
78
  rubyforge_project:
78
- rubygems_version: 1.2.0
79
+ rubygems_version: 1.3.5
79
80
  signing_key:
80
81
  specification_version: 2
81
82
  summary: A Rails library for incorporating subdomains into route generation and recognition.