acts_as_sanitizable 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/acts_as_sanitizable.gemspec +2 -2
- data/lib/acts_as_sanitizable.rb +16 -14
- data/spec/models/acts_as_sanitizable_spec.rb +4 -3
- data/spec/support/user.rb +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e4032dfb42f0d9a4f5935a37e456ecfcd17bb9d
|
4
|
+
data.tar.gz: 20702d4096f6eaa01d99671873b3f5c6ecbe0c54
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c7dfd888ca9ca4fb4cd8f3ffaacd9aa0bce869175e61da41b64630392863bad3d52bb232f671689416269bb69f9ddd090ec750461883892ffc9f88bac59aa43
|
7
|
+
data.tar.gz: 7e9b12707785bd8f0a565f50aaeeccd498790ed132f03a1697d4ca76474e03d4571cdcfbd7fa58927ae435c7d5a97b82f7c7223b7498e3d4c9019f84f088a0cf
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/acts_as_sanitizable.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: acts_as_sanitizable 0.
|
5
|
+
# stub: acts_as_sanitizable 0.2.0 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "acts_as_sanitizable"
|
9
|
-
s.version = "0.
|
9
|
+
s.version = "0.2.0"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
data/lib/acts_as_sanitizable.rb
CHANGED
@@ -10,26 +10,28 @@ module ActsAsSanitizable
|
|
10
10
|
# === Example:
|
11
11
|
# class User < ActiveRecord::Base
|
12
12
|
# sanitizes :content, with: :squish
|
13
|
+
# sanitizes :content, :another_attribute, :nth_attribute, with: [:strip, :upcase]
|
13
14
|
# sanitizes :content do |content|
|
14
15
|
# content.squish.downcase
|
15
16
|
# end
|
17
|
+
# sanitizes :content, with: ->(content) { content.squish.downcase }
|
16
18
|
# end
|
17
19
|
def sanitizes(*attribute_names, &block)
|
18
|
-
options = attribute_names.extract_options
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
sanitizer_name = sanitizer # this is necessary cuz of some strange behaviour
|
24
|
-
sanitizer = -> (value) {
|
25
|
-
Array(sanitizer_name).inject(value) { |prev_value, method| prev_value.send(method) }
|
26
|
-
}
|
27
|
-
end
|
20
|
+
options = attribute_names.extract_options!.assert_valid_keys(:with, :on).reverse_merge(with: block_given? ? block : :strip)
|
21
|
+
|
22
|
+
attribute_sanitizers = Array(options[:with]).map(&:to_proc)
|
23
|
+
|
24
|
+
if attribute_sanitizers.any?
|
28
25
|
before_validation options.slice(:on) do
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
26
|
+
attribute_names.each do |attribute_name|
|
27
|
+
attribute_value = self.send(attribute_name)
|
28
|
+
unless attribute_value.nil?
|
29
|
+
sanitized_value = attribute_sanitizers.inject(attribute_value) do |prev_value, sanitizer_proc|
|
30
|
+
sanitizer_proc.call(prev_value)
|
31
|
+
end
|
32
|
+
self.send("#{attribute_name}=", sanitized_value)
|
33
|
+
end
|
34
|
+
end
|
33
35
|
end
|
34
36
|
end
|
35
37
|
end
|
data/spec/support/user.rb
CHANGED