allowable 1.0.2 → 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
2
  SHA1:
3
- metadata.gz: 9a158170af78796338bf4cf75bf5e4e2ec58c52d
4
- data.tar.gz: 61fed37bc38d0175c977c7e18054a45fb3ad671f
3
+ metadata.gz: efa2ac1dd6e30afa5a91724b2eec484f143c978e
4
+ data.tar.gz: 68b89445aa30b0b245210f4e5eb0da1fffef917c
5
5
  SHA512:
6
- metadata.gz: 932e5d6d255a5cadab770b07c1a9500316ce9ad75ee55386fe96c62de631c15ce5d2611c1ff3a0f50850355bd3f029b25483cc98cd7555e5e3aa2243081ff9ea
7
- data.tar.gz: 279294123d8af70b000b5be1576a5e2f710d278f6b82cf779574fb69a9d8f66ca75efbae42469bf0664a3dcdc654afbbe2358b718bf58af51ab513729c800415
6
+ metadata.gz: 0a7f2d873f4c981ec505373e842e19a1591545b5152bd63d7fea1342b48962426c699896c95d8167e95fbd49b634caab02908573e9209faa4f85ea324d78bf0a
7
+ data.tar.gz: f646f7720e71e9b444aefc5d0dbff1f4074e6a291042406f64f42df12efb4198924476e5738a6aa2c6953a9b1991ed415616ac73b611fc2f7d0e4c7deece38bd
@@ -1,4 +1,10 @@
1
1
  # Changelog
2
+ 1.1.0
3
+ -----
4
+ * Include in `ActiveSupport::HashWithIndifferentAccess`, which also expands Rails compatibility down to version 3.2.
5
+ * Expand testing with `Appraisal` gem. Test against Rails 3.2, 4.2, and 5.1, and Ruby >= 1.9.3
6
+ * Consolidate all methods into `Allowable` module, remove `Allowable::Allow` and `Allowable::Forbid`
7
+
2
8
  1.0.2
3
9
  -----
4
10
  * Match array values e.g. `{ key: ['val_one', 'val_two'] }.forbid(key: ['val_one', 'val_two']) # => {}` TODO: Slows down benchmarked performance of `#allow`, need to figure out why
data/README.md CHANGED
@@ -42,9 +42,9 @@ hash.forbid!(two: 'two') # => {}
42
42
 
43
43
  hash # => {}
44
44
 
45
- hash.merge(one: ['one', 1]) # => { one: ["one", 1], two: "two" }
45
+ hash.merge(one: ['one', 1]).forbid(one: 'one') # => { one: ["one", 1] }
46
46
 
47
- hash.merge(one: ['one', 1]).forbid(one: ['one', 1]) # => { two: "two" }
47
+ hash.merge(one: ['one', 1]).forbid(one: ['one', 1]) # => {}
48
48
  ```
49
49
 
50
50
  #### Type sensitive for `Hash`
@@ -59,7 +59,9 @@ hash.forbid('one' => 'one') # => { "two" => "two" }
59
59
 
60
60
  ## With Rails and strong parameters
61
61
 
62
- When added to the `Gemfile` in a Rails project, `ActionController::Parameters` will automatically receive these methods so you can use them with your `strong_parameters`:
62
+ If `ActiveSupport::HashWithIndifferentAccess` is defined it will receive the methods. This automatically makes them available in `ActionController::Parameters` for Rails 4 and 3. Rails 3 requires that you add `gem 'strong_parameters'` to your `Gemfile` in order to use strong parameters.
63
+
64
+ Starting in Rails 5, `ActionController::Parameters` no longer inherits from `ActiveSupport::HashWithIndifferentAccess`. When `allowable` is added to the `Gemfile` in a Rails 5+ project, load hooks will add the methods directly to `ActionController::Parameters` so you can use them with your strong parameters.
63
65
 
64
66
  ```ruby
65
67
  def user_params
@@ -67,7 +69,7 @@ def user_params
67
69
  end
68
70
  ```
69
71
 
70
- #### Type insensitive for `HashWithIndifferentAccess`
72
+ #### Type insensitive for `HashWithIndifferentAccess` and `ActionController::Parameters`
71
73
  ```ruby
72
74
  params = ActionController::Parameters.new('one' => 'one', 'two' => 'two').permit(:one, :two)
73
75
 
@@ -86,11 +88,14 @@ end
86
88
 
87
89
  ## Platform support
88
90
 
89
- The core module should work with all rubies, however at the moment it is only tested for MRI Ruby >= 2.2.2 and JRuby 9.1.6.0
91
+ The core module should work with all rubies. It is tested for MRI Ruby >= 1.9.3 and JRuby 9.1.6.0
90
92
 
91
- The Rails plugin is currently being tested only with Rails 5.1.2. Rails >= 4.2.0 should not experience any issues.
93
+ Rails compatibility is currently being tested only for versions ~> 3.2, ~> 4.2, and ~> 5.1.
92
94
 
93
95
  Tested against:
96
+ * MRI 1.9.3
97
+ * MRI 2.0.0
98
+ * MRI 2.1.10
94
99
  * MRI 2.2.2
95
100
  * MRI 2.3.0
96
101
  * MRI 2.3.4
@@ -3,4 +3,12 @@
3
3
  require 'allowable/version'
4
4
  require 'allowable/allowable'
5
5
  require 'allowable/core_ext/hash'
6
+
7
+ begin
8
+ require 'active_support/core_ext/hash/indifferent_access'
9
+ ActiveSupport::HashWithIndifferentAccess.send :include, Allowable
10
+ rescue LoadError
11
+ # do nothing
12
+ end
13
+
6
14
  require 'allowable/railtie' if defined? Rails
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'allowable/allow'
4
- require 'allowable/forbid'
5
-
6
3
  # Filter hashes by setting allowed or forbidden values for specific keys.
7
4
  #
8
5
  # hash = { one: 'one', two: 'two' }
@@ -21,6 +18,25 @@ require 'allowable/forbid'
21
18
  #
22
19
  # hash # => {}
23
20
  module Allowable
24
- include Forbid
25
- include Allow
21
+ def allow(filters = {})
22
+ dup.allow!(filters)
23
+ end
24
+
25
+ def allow!(filters = {})
26
+ filters.each do |key, val|
27
+ delete(key) unless Array(val).include?(self[key]) || val == self[key]
28
+ end
29
+ self
30
+ end
31
+
32
+ def forbid(filters = {})
33
+ dup.forbid!(filters)
34
+ end
35
+
36
+ def forbid!(filters = {})
37
+ filters.each do |key, val|
38
+ delete(key) if Array(val).include?(self[key]) || val == self[key]
39
+ end
40
+ self
41
+ end
26
42
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'allowable/allowable'
4
+
3
5
  # Add #allow, #allow!, #forbid, and #forbid! to Hash
4
6
  class Hash
5
7
  include Allowable
@@ -5,9 +5,11 @@ require 'rails/railtie'
5
5
  module Allowable
6
6
  # Railtie initializer that adds Allowable methods to controller params
7
7
  class Railtie < ::Rails::Railtie
8
- initializer :allowable do
9
- ActiveSupport.on_load :action_controller do
10
- ActionController::Parameters.send :include, Allowable
8
+ if Rails::VERSION::MAJOR >= 5
9
+ initializer :allowable do
10
+ ActiveSupport.on_load :action_controller do
11
+ ActionController::Parameters.send :include, ::Allowable
12
+ end
11
13
  end
12
14
  end
13
15
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Allowable
4
- VERSION = '1.0.2'
4
+ VERSION = '1.1.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: allowable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - M. Simon Borg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-04 00:00:00.000000000 Z
11
+ date: 2017-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -49,10 +49,8 @@ files:
49
49
  - LICENSE.txt
50
50
  - README.md
51
51
  - lib/allowable.rb
52
- - lib/allowable/allow.rb
53
52
  - lib/allowable/allowable.rb
54
53
  - lib/allowable/core_ext/hash.rb
55
- - lib/allowable/forbid.rb
56
54
  - lib/allowable/railtie.rb
57
55
  - lib/allowable/version.rb
58
56
  homepage: https://github.com/msimonborg/allowable
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Allowable
4
- # Adds the #allow and #allow! methods
5
- module Allow
6
- def allow(filters = {})
7
- dup.allow!(filters)
8
- end
9
-
10
- def allow!(filters = {})
11
- filters.each do |key, val|
12
- delete(key) unless Array(val).include?(self[key]) || val == self[key]
13
- end
14
- self
15
- end
16
- end
17
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Allowable
4
- # Adds the #forbid and #forbid! methods
5
- module Forbid
6
- def forbid(filters = {})
7
- dup.forbid!(filters)
8
- end
9
-
10
- def forbid!(filters = {})
11
- filters.each do |key, val|
12
- delete(key) if Array(val).include?(self[key]) || val == self[key]
13
- end
14
- self
15
- end
16
- end
17
- end