auto_strip_attributes 2.1.0 → 2.2.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: 4bd8eadb5f2deb62f0fb5b0b0fbe62296865465c
4
- data.tar.gz: 72fd859cbcc3cb6fae5d5b8fd2c58e1bb91d98fc
3
+ metadata.gz: 624c9f6db12d0350cb1a4185eafa37c992b310f1
4
+ data.tar.gz: b6ef7f53808cd252c8bdea23f83935fff4533751
5
5
  SHA512:
6
- metadata.gz: 5cb7ed37fa4a3f86e20c47b00c7dad36999a994850bc58d73c627c789520486a2cbf10f24607c580053ae05032959bf1e4f556f672d33937820bdf1f28aa6e79
7
- data.tar.gz: b1ebe7f5bd4f4265573b549e180ed91064c86c294e4a73ef6a7b44ef8b0cc50c0c636173a19fa96466d4f277ba24842460e50c5d44050e2741e8f575139beda0
6
+ metadata.gz: 6cc0bf5cdced2dba776f05e6ca9290e19f3a3e8df4822595c5163e62fa6f8840231907125b7d3f00d4e5be51e0835d2aa1baa63bb9db29964e27e8df83a7c70b
7
+ data.tar.gz: 8ad8b23e63c8451fd6c14a95f44ec26273c7a73f52984a2e1f3ef6d3d42568e94aa3ddf50bb7b872f011576b6745ac664ff6a947dd3d26f54ed4d097a4bad9a1
@@ -2,15 +2,11 @@ language: ruby
2
2
  sudo: false
3
3
  cache: bundler
4
4
  rvm:
5
- - 2.1.9
6
5
  - 2.2.5
7
6
  - 2.3.1
7
+ - 2.4.0
8
8
  env:
9
- - "RAILS_VERSION=4.0"
10
- - "RAILS_VERSION=4.1"
11
9
  - "RAILS_VERSION=4.2"
12
10
  - "RAILS_VERSION=5.0"
13
- matrix:
14
- exclude:
15
- - rvm: 2.1.9
16
- env: "RAILS_VERSION=5.0"
11
+ - "RAILS_VERSION=5.1"
12
+
@@ -1,5 +1,9 @@
1
1
  ## [Master]
2
2
 
3
+ ## [2.2] - 2016-07-28
4
+
5
+ - Added 'virtual'-option (thnks to [@nasa42](https://github.com/holli/auto_strip_attributes/pull/23))
6
+
3
7
  ## [2.1] - 2016-07-28
4
8
 
5
9
  - Drop support for ruby 1.9.3
data/README.md CHANGED
@@ -8,13 +8,15 @@ It works by adding a before_validation hook to the record. No other methods are
8
8
  Gem has option to set empty strings to nil or to remove extra spaces inside the string.
9
9
 
10
10
  [<img src="https://secure.travis-ci.org/holli/auto_strip_attributes.png" />](http://travis-ci.org/holli/auto_strip_attributes)
11
+ [![Gem](https://img.shields.io/gem/dt/auto_strip_attributes.svg?maxAge=2592000)](https://rubygems.org/gems/auto_strip_attributes/)
12
+ [![Gem](https://img.shields.io/gem/v/auto_strip_attributes.svg?maxAge=2592000)](https://rubygems.org/gems/auto_strip_attributes/)
11
13
 
12
14
  ## Howto / examples
13
15
 
14
16
  Include gem in your Gemfile:
15
17
 
16
18
  ```ruby
17
- gem "auto_strip_attributes", "~> 2.0"
19
+ gem "auto_strip_attributes", "~> 2.2"
18
20
  ```
19
21
 
20
22
  ```ruby
@@ -28,19 +30,23 @@ class User < ActiveRecord::Base
28
30
 
29
31
  # Won't set to null even if string is blank. " " => ""
30
32
  auto_strip_attributes :email, :nullify => false
33
+
34
+ # Use with attributes that are not mapped to a column
35
+ auto_strip_attributes :password, virtual: true
31
36
  end
32
37
  ```
33
38
 
34
- # Filters
35
- ### Default filters
39
+ # Options
40
+ ### Default options
36
41
 
37
- By default the following filters are defined (listed in the order of processing):
42
+ By default the following options are defined (listed in the order of processing):
38
43
 
39
- - :convert_non_breaking_spaces (disabled by default) - converts non-breaking spaces to normal spaces (Unicode U+00A0)
40
44
  - :strip (enabled by default) - removes whitespaces from the beginning and the end of string
41
45
  - :nullify (enabled by default) - replaces empty strings with nil
42
46
  - :squish (disabled by default) - replaces extra whitespaces (including tabs) with one space
43
47
  - :delete_whitespaces (disabled by default) - delete all whitespaces (including tabs)
48
+ - :convert_non_breaking_spaces (disabled by default) - converts non-breaking spaces to normal spaces (Unicode U+00A0)
49
+ - :virtual (disabled by default) - By default `auto_strip_attributes` doesn't work with non-persistent attributes (e.g., attributes that are created with `attr_accessor`). This is to avoid calling their custom getter/setter methods. Use this option with non-persistent attributes.
44
50
 
45
51
  ### Custom Filters
46
52
 
@@ -96,7 +102,7 @@ AutoStripAttributes::Config.setup accepts following options
96
102
 
97
103
  # Requirements
98
104
 
99
- Gem has been tested with newest Ruby & Rails combination and it probably works also with older versions. See test matrix at https://github.com/holli/auto_strip_attributes/blob/master/.travis.yml
105
+ Gem has been tested with newest Ruby & Rails combination and it probably works also with older versions. See test matrix at https://github.com/holli/auto_strip_attributes/blob/master/.travis.yml
100
106
 
101
107
  [<img src="https://secure.travis-ci.org/holli/auto_strip_attributes.png" />](http://travis-ci.org/holli/auto_strip_attributes)
102
108
 
@@ -128,4 +134,3 @@ different approaches. See discussion in previous chapter.
128
134
  # Licence
129
135
 
130
136
  Released under the MIT license (http://www.opensource.org/licenses/mit-license.php)
131
-
@@ -26,5 +26,6 @@ Gem::Specification.new do |s|
26
26
  s.add_development_dependency "minitest", ">= 2.8.1"
27
27
  s.add_development_dependency "mocha", "~> 0.14"
28
28
  s.add_development_dependency 'rake'
29
+ # s.add_development_dependency 'pry'
29
30
 
30
31
  end
@@ -7,14 +7,26 @@ module AutoStripAttributes
7
7
  options = options.merge(attributes.pop)
8
8
  end
9
9
 
10
+ # option `:virtual` is needed because we want to guarantee that
11
+ # getter/setter methods for an attribute will _not_ be invoked by default
12
+ virtual = options.delete(:virtual)
13
+
10
14
  attributes.each do |attribute|
11
15
  before_validation do |record|
12
16
  #debugger
13
- value = record[attribute]
17
+ if virtual
18
+ value = record.public_send(attribute)
19
+ else
20
+ value = record[attribute]
21
+ end
14
22
  AutoStripAttributes::Config.filters_order.each do |filter_name|
15
23
  next unless options[filter_name]
16
24
  value = AutoStripAttributes::Config.filters[filter_name].call value
17
- record[attribute] = value
25
+ if virtual
26
+ record.public_send("#{attribute}=", value)
27
+ else
28
+ record[attribute] = value
29
+ end
18
30
  end
19
31
  end
20
32
  end
@@ -1,3 +1,3 @@
1
1
  module AutoStripAttributes
2
- VERSION = "2.1.0"
2
+ VERSION = "2.2.0"
3
3
  end
@@ -9,10 +9,9 @@ require "auto_strip_attributes"
9
9
  require 'mocha/setup'
10
10
 
11
11
  # if you need debug, add relevant line to auto_strip_attributes.gemspec
12
- # s.add_development_dependency 'ruby-debug'
13
- # s.add_development_dependency 'ruby-debug19'
14
- # and uncomment following line
15
- # require 'ruby-debug'
12
+ # s.add_development_dependency 'pry'
13
+ # and uncomment following line, and then write binding.pry somewhere
14
+ # require 'pry'
16
15
 
17
16
 
18
17
  class MockRecordParent
@@ -27,7 +26,8 @@ class MockRecordParent
27
26
  end
28
27
 
29
28
  def [](key)
30
- instance_variable_get(:"@#{key}")
29
+ k = :"@#{key}"
30
+ instance_variable_defined?(k) ? instance_variable_get(k) : nil
31
31
  end
32
32
 
33
33
  end
@@ -230,6 +230,31 @@ describe AutoStripAttributes do
230
230
  end
231
231
  end
232
232
 
233
+ describe "Virtual attributes" do
234
+ class MockVirtualAttribute < MockRecordParent
235
+ undef :[]=
236
+ undef :[]
237
+
238
+ auto_strip_attributes :foo, virtual: true
239
+
240
+ def foo
241
+ @bar
242
+ end
243
+
244
+ def foo=(val)
245
+ @bar = val
246
+ end
247
+ end
248
+
249
+ it "should handle everything ok" do
250
+ @record = MockVirtualAttribute.new
251
+ @record.foo = " foo "
252
+ @record.foo.must_equal " foo "
253
+ @record.valid?
254
+ @record.foo.must_equal "foo"
255
+ end
256
+ end
257
+
233
258
  describe "Configuration tests" do
234
259
  it "should have defined AutoStripAttributes::Config" do
235
260
  assert AutoStripAttributes.const_defined?(:Config)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auto_strip_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Olli Huotari
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-28 00:00:00.000000000 Z
11
+ date: 2017-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  version: '0'
109
109
  requirements: []
110
110
  rubyforge_project: auto_strip_attributes
111
- rubygems_version: 2.5.1
111
+ rubygems_version: 2.6.12
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: Removes unnecessary whitespaces in attributes. Extension to ActiveRecord