strikeroff-routing-filter 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
- vendor
1
+ vendor
2
+ gem_build.bat
data/README.markdown CHANGED
@@ -117,6 +117,30 @@ So, even though the plugin itself is a blatant monkey-patch to one of the
117
117
  most complex area of Rails internals, this solution seems to be effectively
118
118
  less intrusive and pricey than others are.
119
119
 
120
+
121
+ # Difference from origin
122
+
123
+ 1. Locale filter now has option :scip_locale_filter
124
+ In you routes rb you can
125
+ map.change_locale "main/change_locale/:new_locale", :controller=>"main", :action=>"change_locale", :scip_locale_filter=>true
126
+ And locale filter wouldn't add locale to url while generate
127
+
128
+ 2. extract_locale! and prepend_locale! methods become class methods aka static. So you can use them outside of gem
129
+ Usecase:
130
+ For example to create on page language changer without requests to served. You need only change locale in url
131
+
132
+ Example code:
133
+ Current locale - :ru,but you need to have a link with locale = :en.
134
+
135
+ link_to("EN", url_with_locale(request.request_uri.dup, :en))
136
+
137
+ def url_with_locale(url,locale)
138
+ RoutingFilter::Locale.extract_locale! url
139
+ RoutingFilter::Locale.prepend_locale! url, locale
140
+ end
141
+
142
+
143
+
120
144
  ## Etc
121
145
 
122
146
  Authors: [Sven Fuchs](http://www.artweb-design.de) <svenfuchs at artweb-design dot de>
@@ -22,49 +22,61 @@ module RoutingFilter
22
22
  def locales_pattern
23
23
  @@locales_pattern ||= %r(^/(#{self.locales.map { |l| Regexp.escape(l.to_s) }.join('|')})(?=/|$))
24
24
  end
25
+
26
+ def extract_locale!(path)
27
+ path.sub! self.locales_pattern, ''
28
+ $1
29
+ end
30
+
31
+ def prepend_locale!(url, locale)
32
+ url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{locale}#{$2}" }
33
+ end
25
34
  end
26
35
 
27
36
  def around_recognize(path, env, &block)
28
- locale = extract_locale!(path) # remove the locale from the beginning of the path
37
+ locale = self.class.extract_locale!(path) # remove the locale from the beginning of the path
29
38
  returning yield do |params| # invoke the given block (calls more filters and finally routing)
30
39
  params[:locale] = locale if locale # set recognized locale to the resulting params hash
31
40
  end
32
41
  end
33
42
 
34
43
  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)
44
+ options = args.extract_options!
45
+
46
+ locale = options.delete(:locale)
47
+ unless skip_prepend_locale? options
48
+ locale = I18n.locale if locale.nil? # default to I18n.locale when locale is nil (could also be false)
49
+ locale = nil unless valid_locale?(locale) # reset to no locale when locale is not valid
50
+ returning yield do |result|
51
+ if locale && prepend_locale?(locale)
52
+ url = result.is_a?(Array) ? result.first : result
53
+ self.class.prepend_locale!(url, locale)
54
+ end
43
55
  end
56
+ else
57
+ yield
44
58
  end
45
59
  end
46
60
 
47
61
  protected
48
62
 
49
- def extract_locale!(path)
50
- path.sub! self.class.locales_pattern, ''
51
- $1
52
- end
53
63
 
54
- def prepend_locale?(locale)
55
- self.class.include_default_locale? || !default_locale?(locale)
56
- end
64
+ def prepend_locale?(locale)
65
+ self.class.include_default_locale? || !default_locale?(locale)
66
+ end
57
67
 
58
- def valid_locale?(locale)
59
- locale && self.class.locales.include?(locale.to_sym)
60
- end
68
+ def skip_prepend_locale?(options)
69
+ !!options[:scip_locale_filter]
70
+ end
71
+
72
+ def valid_locale?(locale)
73
+ locale && self.class.locales.include?(locale.to_sym)
74
+ end
75
+
76
+ def default_locale?(locale)
77
+ locale && locale.to_sym == I18n.default_locale.to_sym
78
+ end
61
79
 
62
- def default_locale?(locale)
63
- locale && locale.to_sym == I18n.default_locale.to_sym
64
- end
65
80
 
66
- def prepend_locale!(url, locale)
67
- url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{locale}#{$2}" }
68
- end
69
81
  end
70
82
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strikeroff-routing-filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Fuchs
@@ -41,7 +41,6 @@ files:
41
41
  - spec/spec_helper.rb
42
42
  has_rdoc: false
43
43
  homepage: http://github.com/svenfuchs/routing-filter
44
- licenses:
45
44
  post_install_message:
46
45
  rdoc_options:
47
46
  - --charset=UTF-8
@@ -62,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
61
  requirements: []
63
62
 
64
63
  rubyforge_project:
65
- rubygems_version: 1.3.5
64
+ rubygems_version: 1.2.0
66
65
  signing_key:
67
66
  specification_version: 3
68
67
  summary: routing-filter wraps around the complex beast that the Rails routing system is, allowing for unseen flexibility and power in Rails URL recognition and generation.