params_keeper_rails 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: e3849671b65b09469406cf424369f6738686eb9a
4
- data.tar.gz: 0cd0bf4977454d69558c1c3f4556ccbd99d9bf62
2
+ SHA256:
3
+ metadata.gz: 42544c02623dbab162cb32dfbf47788df7ccec735e0b606591577033712c9687
4
+ data.tar.gz: 3a1e1ba2b64830eb1615c3de5200df092166739c6676b94dd17dd495ab420b2b
5
5
  SHA512:
6
- metadata.gz: a0450d4f2c493ec326d95fe97e641a9d8e9838252c44c3585badb4b4eaee3091523b7d5e240f258930626a9e920e50704bf904585406b00d772ae30759f9142c
7
- data.tar.gz: 7d8833985dc7d95fc448ea23a1caec19489dd80cf4426fb452bd95644b523e4c535a53489631415309572c106ac432cc11f0b17ef903bbbf352dbab8cf1bb78b
6
+ metadata.gz: a1c5604ee7acfc4db4b552a6e9408df49a4cb0085cb1e04556582241b303abde1e04216c9404e80315bc65b8f7e8d7925f60edbf1df68d001ca112d040e342c1
7
+ data.tar.gz: 3548469944a9851c9ca439d9423a9ad9393b5e546fe25c842b7a575da027521f7e724976043fe6c6aa4fa6e9c753daf1cb08f8b175c42e8ab440b1fc553d05c5
@@ -1,8 +1,10 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.3.4
4
- - 2.4.1
3
+ - 2.3
4
+ - 2.4
5
+ - 2.5
6
+ - 2.6
5
7
  gemfile:
6
8
  - gemfiles/rails50.gemfile
7
9
  - gemfiles/rails51.gemfile
8
- before_install: gem install bundler -v 1.15.3
10
+ - gemfiles/rails52.gemfile
@@ -0,0 +1,19 @@
1
+ # CHANGELOG
2
+
3
+ ## 1.1.0
4
+
5
+ Features:
6
+
7
+ * Add :url_options config.
8
+
9
+ Changes:
10
+
11
+ * Change :args to :for.
12
+
13
+ Fixes:
14
+
15
+ * Refactoring.
16
+
17
+ ## 1.0.0
18
+
19
+ * First release.
data/Gemfile CHANGED
@@ -1,4 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in params_keeper_rails.gemspec
4
3
  gemspec
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ParamsKeeperRails
2
2
 
3
- A rails gem for keeping specific parameters through links.
3
+ A rails controller extension for keeping specific parameters through links.
4
4
 
5
5
  ## Dependencies
6
6
 
@@ -35,10 +35,10 @@ For example:
35
35
  ```ruby
36
36
  GET "/examples?key1=**&key2=**"
37
37
 
38
- # hash argument
38
+ # hash
39
39
  url_for(action: :show) #=> '/examples/:id?key1=**&key2=**'
40
40
 
41
- # string and active model arguments don't keep parameters by defalut
41
+ # string and active model don't keep parameters by defalut
42
42
  url_for('/examples') #=> '/examples'
43
43
  url_for(@example) #=> '/examples/:id'
44
44
 
@@ -49,12 +49,23 @@ url_for(controller: 'examples2', action: :index) #=> '/examples2'
49
49
  url_for(action: :show, keep_params: false) #=> '/examples/:id'
50
50
  ```
51
51
 
52
- Specify class of url_for argument:
52
+ Enable only specific class of url_for:
53
53
 
54
54
  ```ruby
55
- keep_params :key1, :key2, args: [:hash] # only hash argument like url_for(action: :show) keeps parameters
56
- keep_params :key1, :key2, args: [:string] # only string argument like url_for('/examples') keeps parameters
57
- keep_params :key1, :key2, args: [:model] # only model argument like url_for(@example) keeps parameters
55
+ # enable only hash like url_for(action: :show)
56
+ keep_params :key1, :key2, for: :hash
57
+
58
+ # enable only string like url_for('/examples')
59
+ keep_params :key1, :key2, for: :string
60
+
61
+ # enable only model like url_for(@example)
62
+ keep_params :key1, :key2, for: :model
63
+ ```
64
+
65
+ Specify default options of url_for:
66
+
67
+ ```ruby
68
+ keep_params :key1, :key2, url_options: { fixed_param: :something }
58
69
  ```
59
70
 
60
71
  Keep parameters throught multiple controllers:
@@ -73,7 +84,7 @@ end
73
84
 
74
85
  ## Contributing
75
86
 
76
- Bug reports and pull requests are welcome on GitHub at https://github.com/kanety/params_keeper_rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
87
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kanety/params_keeper_rails.
77
88
 
78
89
  ## License
79
90
 
@@ -1,6 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem "rails", "~> 5.0.5"
3
+ gem "rails", "~> 5.0.0"
4
4
 
5
- # Specify your gem's dependencies in params_keeper_rails.gemspec
6
5
  gemspec path: "../"
@@ -1,6 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem "rails", "~> 5.1.3"
3
+ gem "rails", "~> 5.1.0"
4
4
 
5
- # Specify your gem's dependencies in params_keeper_rails.gemspec
6
5
  gemspec path: "../"
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 5.2.0"
4
+
5
+ gemspec path: "../"
@@ -1,64 +1,25 @@
1
- require 'params_keeper/helper'
2
-
3
1
  module ParamsKeeper::Controller
4
2
  extend ActiveSupport::Concern
5
3
 
6
4
  included do
7
- class_attribute :keep_params_keys, :keep_params_configs
5
+ class_attribute :keep_params_config
8
6
  helper ParamsKeeper::Helper
9
7
  end
10
8
 
11
9
  def url_for(options = nil)
12
- return super if options.is_a?(Hash) && options.delete(:keep_params) == false
13
-
14
- configs = self.class.keep_params_configs
15
- keys = self.class.keep_params_keys
16
- return super if keys.blank?
17
-
18
- if configs.key?(:args)
19
- args = Array(configs[:args])
20
- return super if (options.is_a?(Hash) && !args.include?(:hash)) ||
21
- (options.is_a?(String) && !args.include?(:string)) ||
22
- (options.class.respond_to?(:model_name) && !args.include?(:model))
23
- elsif !options.is_a?(Hash)
24
- return super
25
- end
26
-
27
- if options.is_a?(Hash)
28
- if keep_params?(options, configs)
29
- super(ParamsKeeper::Helper.merge_params(options, params, keys))
30
- else
31
- super
32
- end
33
- else
34
- url = super(options)
35
- url_opts = begin
36
- Rails.application.routes.recognize_path(url)
37
- rescue ActionController::RoutingError
38
- nil
39
- end
40
- if url_opts && keep_params?(url_opts, configs)
41
- super(ParamsKeeper::Helper.merge_params(url_opts, params, keys))
42
- else
43
- url
44
- end
45
- end
46
- end
47
-
48
- def keep_params?(options, configs)
49
- controller = options[:controller].to_s
50
- if configs[:to]
51
- target = [controller, controller_name, controller_path]
52
- Array(configs[:to]).any? { |c| c.to_s.in?(target) }
53
- else
54
- controller.blank? || controller.in?([controller_name, controller_path])
55
- end
10
+ ParamsKeeper::Resolver.new(self, self, options).resolve || super
56
11
  end
57
12
 
58
13
  class_methods do
59
14
  def keep_params(*args)
60
- self.keep_params_configs = args.last.is_a?(Hash) ? args.pop : {}
61
- self.keep_params_keys = Array(args)
15
+ self.keep_params_config = {
16
+ keys: nil,
17
+ to: nil,
18
+ for: :hash,
19
+ url_options: nil
20
+ }
21
+ self.keep_params_config.merge!(args.last.is_a?(Hash) ? args.pop : {})
22
+ self.keep_params_config[:keys] = Array(args)
62
23
  end
63
24
  end
64
25
  end
@@ -1,12 +1,5 @@
1
1
  module ParamsKeeper::Helper
2
2
  def url_for(options = nil)
3
- controller.url_for(options)
4
- end
5
-
6
- class << self
7
- def merge_params(options, params, keep_params_keys)
8
- keeps = params.to_unsafe_h.deep_symbolize_keys.slice(*keep_params_keys.to_a)
9
- options.reverse_merge(keeps)
10
- end
3
+ ParamsKeeper::Resolver.new(self, controller, options).resolve || super
11
4
  end
12
5
  end
@@ -0,0 +1,84 @@
1
+ class ParamsKeeper::Resolver
2
+ def initialize(klass, controller, options)
3
+ @klass = klass
4
+ @controller = controller
5
+ @options = options
6
+ end
7
+
8
+ def resolve
9
+ return if !configured? || !enable_options? || !target_options?
10
+
11
+ if @options.is_a?(Hash)
12
+ resolve_from_hash
13
+ else
14
+ resolve_from_routing
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def resolve_from_hash
21
+ if link_to_target_controller?(@options)
22
+ base_url_for(self.class.merge_params(@options, @controller.params, config[:keys]))
23
+ else
24
+ base_url_for(@options)
25
+ end
26
+ end
27
+
28
+ def resolve_from_routing
29
+ url = base_url_for(@options)
30
+ url_opts = recognize_path(url)
31
+
32
+ if url_opts && link_to_target_controller?(url_opts)
33
+ base_url_for(self.class.merge_params(url_opts, @controller.params, config[:keys]))
34
+ else
35
+ url
36
+ end
37
+ end
38
+
39
+ def link_to_target_controller?(options)
40
+ controller = options[:controller].to_s
41
+ if config[:to].present?
42
+ ([controller] - Array(config[:to])).blank?
43
+ else
44
+ controller.blank? || controller.in?([@controller.controller_name, @controller.controller_path])
45
+ end
46
+ end
47
+
48
+ def config
49
+ @controller.class.keep_params_config
50
+ end
51
+
52
+ def configured?
53
+ config && config[:keys].present?
54
+ end
55
+
56
+ def enable_options?
57
+ !@options.is_a?(Hash) || @options.delete(:keep_params) != false
58
+ end
59
+
60
+ def target_options?
61
+ fors = Array(config[:for] || :hash)
62
+ (fors.include?(:hash) && @options.is_a?(Hash)) ||
63
+ (fors.include?(:string) && @options.is_a?(String)) ||
64
+ (fors.include?(:model) && @options.class.respond_to?(:model_name))
65
+ end
66
+
67
+ def recognize_path(url)
68
+ Rails.application.routes.recognize_path(url)
69
+ rescue ActionController::RoutingError
70
+ nil
71
+ end
72
+
73
+ def base_url_for(options)
74
+ options = options.merge(config[:url_options]) if options.is_a?(Hash) && config[:url_options]
75
+ @klass.method(:url_for).super_method.call(options)
76
+ end
77
+
78
+ class << self
79
+ def merge_params(options, params, keys)
80
+ keeps = params.to_unsafe_h.deep_symbolize_keys.slice(*keys.to_a)
81
+ options.reverse_merge(keeps)
82
+ end
83
+ end
84
+ end
@@ -1,3 +1,3 @@
1
1
  module ParamsKeeper
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -1,3 +1,5 @@
1
1
  require 'active_support'
2
2
  require 'params_keeper/version'
3
+ require 'params_keeper/resolver'
4
+ require 'params_keeper/helper'
3
5
  require 'params_keeper/controller'
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["kaneta@sitebridge.co.jp"]
11
11
 
12
12
  spec.summary = %q{keep specific parameters through links.}
13
- spec.description = %q{A rails gem for keeping specific parameters through links.}
13
+ spec.description = %q{A rails controller extension for keeping specific parameters through links.}
14
14
  spec.homepage = "https://github.com/kanety/params_keeper_rails"
15
15
  spec.license = "MIT"
16
16
 
@@ -26,6 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "rake"
27
27
  spec.add_development_dependency "rspec"
28
28
  spec.add_development_dependency "rspec-rails"
29
- spec.add_development_dependency "rails-controller-testing"
30
29
  spec.add_development_dependency "simplecov"
31
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: params_keeper_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshikazu Kaneta
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-04 00:00:00.000000000 Z
11
+ date: 2019-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -66,20 +66,6 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: rails-controller-testing
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
69
  - !ruby/object:Gem::Dependency
84
70
  name: simplecov
85
71
  requirement: !ruby/object:Gem::Requirement
@@ -94,7 +80,8 @@ dependencies:
94
80
  - - ">="
95
81
  - !ruby/object:Gem::Version
96
82
  version: '0'
97
- description: A rails gem for keeping specific parameters through links.
83
+ description: A rails controller extension for keeping specific parameters through
84
+ links.
98
85
  email:
99
86
  - kaneta@sitebridge.co.jp
100
87
  executables: []
@@ -104,6 +91,7 @@ files:
104
91
  - ".gitignore"
105
92
  - ".rspec"
106
93
  - ".travis.yml"
94
+ - CHANGELOG.md
107
95
  - CODE_OF_CONDUCT.md
108
96
  - Gemfile
109
97
  - LICENSE.txt
@@ -113,8 +101,10 @@ files:
113
101
  - bin/setup
114
102
  - gemfiles/rails50.gemfile
115
103
  - gemfiles/rails51.gemfile
104
+ - gemfiles/rails52.gemfile
116
105
  - lib/params_keeper/controller.rb
117
106
  - lib/params_keeper/helper.rb
107
+ - lib/params_keeper/resolver.rb
118
108
  - lib/params_keeper/version.rb
119
109
  - lib/params_keeper_rails.rb
120
110
  - params_keeper_rails.gemspec
@@ -137,8 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
127
  - !ruby/object:Gem::Version
138
128
  version: '0'
139
129
  requirements: []
140
- rubyforge_project:
141
- rubygems_version: 2.6.13
130
+ rubygems_version: 3.0.1
142
131
  signing_key:
143
132
  specification_version: 4
144
133
  summary: keep specific parameters through links.