acts_as_sanitizable 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 00de510e9016a4db0dd1e4eab1221f09bce87e62
4
- data.tar.gz: 2b7a6eb4832cce85f59b2e47e8b6b11d6afe7dbe
3
+ metadata.gz: 1e4032dfb42f0d9a4f5935a37e456ecfcd17bb9d
4
+ data.tar.gz: 20702d4096f6eaa01d99671873b3f5c6ecbe0c54
5
5
  SHA512:
6
- metadata.gz: 978df2e79295d0fe70de086049bd782c9c93c1e859005ab6162e72ea70b8542963811ed9b2317fa205b9e1569fb6e58436d179a44e43e5ee8ee7c79c0c9839fe
7
- data.tar.gz: 06b1b9b52afb6c71d785f610f65bc3b5dc475d4d48cca82a2d576be73374c502b8ce76c6e7bc3a0a97c158edb865272141b4d9196ee56530f6bda2b1eadbd842
6
+ metadata.gz: 2c7dfd888ca9ca4fb4cd8f3ffaacd9aa0bce869175e61da41b64630392863bad3d52bb232f671689416269bb69f9ddd090ec750461883892ffc9f88bac59aa43
7
+ data.tar.gz: 7e9b12707785bd8f0a565f50aaeeccd498790ed132f03a1697d4ca76474e03d4571cdcfbd7fa58927ae435c7d5a97b82f7c7223b7498e3d4c9019f84f088a0cf
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -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.1.1 ruby lib
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.1.1"
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"]
@@ -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
- options.assert_valid_keys(:with)
20
- options.reverse_merge!(with: block_given? ? block : :strip)
21
- if sanitizer = options[:with]
22
- unless sanitizer.respond_to?(:call)
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
- attribute_names.each do |attribute_name|
30
- attribute_value = send(attribute_name)
31
- send("#{attribute_name}=", sanitizer.call(attribute_value)) unless attribute_value.nil?
32
- end
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
@@ -19,8 +19,9 @@ describe ActsAsSanitizable do
19
19
  expect(user.email).to eq("christoph@chilian.de")
20
20
  end
21
21
 
22
-
23
-
24
-
22
+ it "username should be downcased gsub'ed" do
23
+ user = create(:user, username: '@YamYam')
24
+ expect(user.username).to eq 'yamyam'
25
+ end
25
26
 
26
27
  end
data/spec/support/user.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  class User < ActiveRecord::Base
2
2
 
3
+ sanitizes :username, with: [:downcase, ->(content) { content.gsub(/\A@/, '') }]
3
4
  sanitizes :first_name, with: :strip
4
5
  sanitizes :last_name, with: :upcase
5
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_sanitizable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christoph Chilian