railings 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,18 +1,36 @@
1
1
  module Railings
2
2
  module Domains
3
- MAPPINGS = YAML::load_file Rails.root.join 'config', 'domains.yml' rescue false
4
- MAPPINGS ||= { }
5
-
6
- def const_missing domain
7
- Class.new do
8
- @domain = domain.downcase.to_s
9
- class << self
10
- def matches? request
11
- request.subdomain.include? subdomain
12
- end
3
+ YAML::load_file(Rails.root.join 'config', 'domains.yml').tap do |mappings|
4
+ mappings.each do |domain, mapping|
5
+ save domain, Subdomain.new(domain, mapping)
6
+ end
7
+ end rescue nil
13
8
 
14
- def subdomain
15
- MAPPINGS.has_key?(@domain) ? MAPPINGS[@domain] : @domain
9
+ class Subdomain < String
10
+ def initialize name, mapping = nil
11
+ replace (mapping || name).to_s.downcase
12
+ end
13
+
14
+ def matches? request
15
+ subdomain == request.subdomain
16
+ end
17
+ end
18
+
19
+ class << self
20
+ def const_missing name
21
+ save name, Subdomain.new(name)
22
+ end
23
+
24
+ def save name, subdomain
25
+ const_set(name, subdomain).tap do
26
+ ApplicationController.send :helper, Module.new do
27
+ define_method "url_for_#{name.downcase}" do |options|
28
+ unless subdomain == request.subdomain
29
+ options[:host] = with_subdomain subdomain
30
+ options[:only_path] = false
31
+ end
32
+ options
33
+ end
16
34
  end
17
35
  end
18
36
  end
@@ -1,16 +1,5 @@
1
1
  module Railings
2
2
  module RoutingHelper
3
- Rails::application::class::parent::Domains::MAPPINGS.keys.each do |name|
4
- define_method "url_for_#{name}" do |options|
5
- domain = Rails::application::class::parent::Domains.const_get(name.to_s.capitalize)
6
- if domain && domain.respond_to?(:subdomain)
7
- options[:host] = with_subdomain domain.subdomain
8
- options[:only_path] = false
9
- end
10
- options
11
- end
12
- end if Rails::application::class::parent.const_defined? 'Domains'
13
-
14
3
  def url_for_root options
15
4
  request.subdomain.present? ? options.merge!(:host => with_subdomain, :only_path => false) : options
16
5
  end
@@ -1,3 +1,3 @@
1
1
  module Railings
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Nathaniel Jones
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-04-05 00:00:00 -05:00
17
+ date: 2011-04-06 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency