zh_kostev_ext 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -40,24 +40,42 @@ module ControllerExtensions
40
40
  end
41
41
 
42
42
  #redirects to back.
43
- #You can specify REDIRECT_DEFAULT_PATH contant in ActionController::Base to setup default redirect path(by default REDIRECT_DEFAULT_PATH='/')
43
+ # You can specify REDIRECT_DEFAULT_PATH constant in ActionController::Base to setup default redirect path(by default REDIRECT_DEFAULT_PATH='/')
44
44
  #
45
- #You can also pass params to redirect to method
46
- #example
45
+ # You can also pass params to redirect to method
46
+ # Example
47
47
  # redirect_back_or_default :test_param => "test"
48
48
  def redirect_back_or_default(options = {})
49
- redirect_to (session[:return_to] || ::REDIRECT_DEFAULT_PATH) + "#{'?' unless options.blank?}"+options.map { |key, value| "#{key}=#{value}" }.join("&")
49
+ url = (session[:return_to] || ::REDIRECT_DEFAULT_PATH)
50
+ url = add_params_to_url(url, options)
50
51
  session[:return_to] = nil
52
+ redirect_to url
51
53
  end
52
54
 
53
55
  private
54
56
  #stores request path if request is get and not ajax
55
57
  #
56
- #example
58
+ # Example
57
59
  # after_filter :store_location
58
60
  def store_location
59
61
  session[:return_to] = request.url if request.get? && !request.xhr? && controller_name != "sessions"
60
62
  end
63
+
64
+ #add params to url. Prevent situation with identical param name
65
+ #
66
+ # Example:
67
+ # add_params_to_url('localhost:3000?test=3', {:test => 5, :asd => 2} #result will be 'localhost:3000?test=5&asd=2'
68
+ def add_params_to_url(url, options)
69
+ main_url, main_params = url.split('?')
70
+ if main_params
71
+ main_params = Hash[main_params.split('&').map { |el| el.split("=") }]
72
+ main_params = main_params.symbolize_keys
73
+ options = options.symbolize_keys
74
+ options = main_params.merge(options)
75
+ end
76
+
77
+ main_url + "#{'?' unless options.blank?}"+options.map { |key, value| "#{key}=#{value}" }.join("&")
78
+ end
61
79
  end
62
80
  end
63
81
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zh_kostev_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-16 00:00:00.000000000 Z
12
+ date: 2012-10-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: spreadsheet
@@ -136,7 +136,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
136
136
  version: '0'
137
137
  segments:
138
138
  - 0
139
- hash: 833124783
139
+ hash: -711035597
140
140
  required_rubygems_version: !ruby/object:Gem::Requirement
141
141
  none: false
142
142
  requirements: