acts_as_sanitizable 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -0
- data/Gemfile +0 -1
- data/Gemfile.lock +0 -3
- data/README.md +46 -3
- data/VERSION +1 -1
- data/acts_as_sanitizable.gemspec +11 -12
- data/lib/acts_as_sanitizable.rb +0 -40
- data/lib/sanitizable/attribute_sanitizer.rb +15 -0
- data/lib/sanitizable/macro.rb +60 -0
- data/lib/sanitizable/model.rb +39 -0
- data/lib/sanitizable.rb +9 -0
- data/spec/db/schema.rb +1 -1
- data/spec/models/sanitizable_spec.rb +141 -0
- data/spec/spec_helper.rb +3 -8
- data/spec/support/models.rb +2 -0
- metadata +9 -21
- data/README.rdoc +0 -19
- data/spec/factories/user.rb +0 -6
- data/spec/models/acts_as_sanitizable_spec.rb +0 -27
- data/spec/support/user.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b972376078e9eed1a1775f90b95273f497ad948
|
4
|
+
data.tar.gz: bbf4c81e36b1ec67715243c623e3865754b8102a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56c954eabaa0adaf98ff9e1d71e9aea2278d197d8aa8ded34c9258cb4189571e0641c4efbda3f9e8e2c55f413752b157bc7a44a3492e5e8d74632127f2a2b548
|
7
|
+
data.tar.gz: 05c95047407bc919d93186703a9f9b540dec17c409e19d9426dc7bb074a1b663143f6b7b9eab04cb28d01b7d3315a0ff060a7a7bfdd563584db1773439a0f1a9
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -35,8 +35,6 @@ GEM
|
|
35
35
|
diff-lcs (1.2.5)
|
36
36
|
docile (1.1.3)
|
37
37
|
erubis (2.7.0)
|
38
|
-
factory_girl (4.4.0)
|
39
|
-
activesupport (>= 3.0.0)
|
40
38
|
faraday (0.9.0)
|
41
39
|
multipart-post (>= 1.2, < 3)
|
42
40
|
git (1.2.6)
|
@@ -156,7 +154,6 @@ DEPENDENCIES
|
|
156
154
|
activemodel (>= 3.0.0)
|
157
155
|
activerecord (>= 2.3.5)
|
158
156
|
bundler (~> 1.0)
|
159
|
-
factory_girl
|
160
157
|
jeweler (~> 2.0.1)
|
161
158
|
rails (>= 3.2.0, < 5)
|
162
159
|
rdoc (~> 3.12)
|
data/README.md
CHANGED
@@ -1,4 +1,47 @@
|
|
1
|
-
acts_as_sanitizable
|
2
|
-
===================
|
1
|
+
# acts_as_sanitizable [![Gem Version](https://badge.fury.io/rb/acts_as_sanitizable.svg)](http://badge.fury.io/rb/acts_as_sanitizable) [![Build Status](https://travis-ci.org/chilian/acts_as_sanitizable.svg?branch=master)](https://travis-ci.org/chilian/acts_as_sanitizable) [![Code Climate](https://codeclimate.com/github/chilian/acts_as_sanitizable.png)](https://codeclimate.com/github/chilian/acts_as_sanitizable)
|
3
2
|
|
4
|
-
Sanitizable concern to sanitize attributes the way you like
|
3
|
+
Sanitizable concern to sanitize activerecord model attributes the way you like. E.g. very useful if you want to convert empty strings from your form params into `nil`...
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add `acts_as_sanitizable` to your `Gemfile`:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'acts_as_sanitizable'
|
11
|
+
```
|
12
|
+
|
13
|
+
Download and install by running:
|
14
|
+
|
15
|
+
```
|
16
|
+
bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
Add the `sanitizes`-method to your model:
|
20
|
+
|
21
|
+
```
|
22
|
+
class User < ActiveRecord::Base
|
23
|
+
sanitizes :content, with: :squish
|
24
|
+
sanitizes :content, :another_attribute, :nth_attribute, with: [:strip, :upcase]
|
25
|
+
sanitizes :content do |content|
|
26
|
+
content.squish.downcase
|
27
|
+
end
|
28
|
+
sanitizes :content, with: ->(content) { content.squish.downcase }
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
|
33
|
+
## Contributing to acts_as_sanitizable
|
34
|
+
|
35
|
+
Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
36
|
+
|
37
|
+
Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
38
|
+
|
39
|
+
Fork the project.
|
40
|
+
|
41
|
+
Start a feature/bugfix branch.
|
42
|
+
|
43
|
+
Commit and push until you are happy with your contribution.
|
44
|
+
|
45
|
+
Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
46
|
+
|
47
|
+
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/acts_as_sanitizable.gemspec
CHANGED
@@ -2,40 +2,42 @@
|
|
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.3.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.3.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"]
|
13
13
|
s.authors = ["Christoph Chilian"]
|
14
|
-
s.date = "2014-04-
|
14
|
+
s.date = "2014-04-16"
|
15
15
|
s.description = "Sanitizable concern to sanitize attributes the way you like"
|
16
16
|
s.email = "christoph@chilian.de"
|
17
17
|
s.extra_rdoc_files = [
|
18
18
|
"LICENSE.txt",
|
19
|
-
"README.md"
|
20
|
-
"README.rdoc"
|
19
|
+
"README.md"
|
21
20
|
]
|
22
21
|
s.files = [
|
23
22
|
".document",
|
23
|
+
".travis.yml",
|
24
24
|
"Gemfile",
|
25
25
|
"Gemfile.lock",
|
26
26
|
"LICENSE.txt",
|
27
27
|
"README.md",
|
28
|
-
"README.rdoc",
|
29
28
|
"Rakefile",
|
30
29
|
"VERSION",
|
31
30
|
"acts_as_sanitizable.gemspec",
|
32
31
|
"lib/acts_as_sanitizable.rb",
|
32
|
+
"lib/sanitizable.rb",
|
33
|
+
"lib/sanitizable/attribute_sanitizer.rb",
|
34
|
+
"lib/sanitizable/macro.rb",
|
35
|
+
"lib/sanitizable/model.rb",
|
33
36
|
"spec/db/database.yml",
|
34
37
|
"spec/db/schema.rb",
|
35
|
-
"spec/
|
36
|
-
"spec/models/acts_as_sanitizable_spec.rb",
|
38
|
+
"spec/models/sanitizable_spec.rb",
|
37
39
|
"spec/spec_helper.rb",
|
38
|
-
"spec/support/
|
40
|
+
"spec/support/models.rb",
|
39
41
|
"test/helper.rb",
|
40
42
|
"test/test_acts_as_sanitizable.rb"
|
41
43
|
]
|
@@ -59,7 +61,6 @@ Gem::Specification.new do |s|
|
|
59
61
|
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
60
62
|
s.add_development_dependency(%q<activerecord>, [">= 2.3.5"])
|
61
63
|
s.add_development_dependency(%q<activemodel>, [">= 3.0.0"])
|
62
|
-
s.add_development_dependency(%q<factory_girl>, [">= 0"])
|
63
64
|
else
|
64
65
|
s.add_dependency(%q<rails>, ["< 5", ">= 3.2.0"])
|
65
66
|
s.add_dependency(%q<sqlite3>, [">= 0"])
|
@@ -72,7 +73,6 @@ Gem::Specification.new do |s|
|
|
72
73
|
s.add_dependency(%q<simplecov>, [">= 0"])
|
73
74
|
s.add_dependency(%q<activerecord>, [">= 2.3.5"])
|
74
75
|
s.add_dependency(%q<activemodel>, [">= 3.0.0"])
|
75
|
-
s.add_dependency(%q<factory_girl>, [">= 0"])
|
76
76
|
end
|
77
77
|
else
|
78
78
|
s.add_dependency(%q<rails>, ["< 5", ">= 3.2.0"])
|
@@ -86,7 +86,6 @@ Gem::Specification.new do |s|
|
|
86
86
|
s.add_dependency(%q<simplecov>, [">= 0"])
|
87
87
|
s.add_dependency(%q<activerecord>, [">= 2.3.5"])
|
88
88
|
s.add_dependency(%q<activemodel>, [">= 3.0.0"])
|
89
|
-
s.add_dependency(%q<factory_girl>, [">= 0"])
|
90
89
|
end
|
91
90
|
end
|
92
91
|
|
data/lib/acts_as_sanitizable.rb
CHANGED
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'active_record'
|
3
|
-
|
4
|
-
|
5
|
-
module ActsAsSanitizable
|
6
|
-
# use sanitizes concern to modify strings on specified fields
|
7
|
-
#
|
8
|
-
# -----
|
9
|
-
#
|
10
|
-
# === Example:
|
11
|
-
# class User < ActiveRecord::Base
|
12
|
-
# sanitizes :content, with: :squish
|
13
|
-
# sanitizes :content, :another_attribute, :nth_attribute, with: [:strip, :upcase]
|
14
|
-
# sanitizes :content do |content|
|
15
|
-
# content.squish.downcase
|
16
|
-
# end
|
17
|
-
# sanitizes :content, with: ->(content) { content.squish.downcase }
|
18
|
-
# end
|
19
|
-
def sanitizes(*attribute_names, &block)
|
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?
|
25
|
-
before_validation options.slice(:on) do
|
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
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
ActiveRecord::Base.extend(ActsAsSanitizable) if defined? ActiveRecord
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Sanitizable
|
2
|
+
class AttributeSanitizer
|
3
|
+
attr_reader :name, :sanitizers, :context
|
4
|
+
|
5
|
+
def initialize(name, options = {})
|
6
|
+
@name = name
|
7
|
+
@sanitizers = Array(options[:with]).map(&:to_proc)
|
8
|
+
@context = options[:on]
|
9
|
+
end
|
10
|
+
|
11
|
+
def inspect
|
12
|
+
"#<#{self.class.name} name: #{name.inspect}, sanitizers: #{sanitizers.inspect}, context: #{context.inspect}>"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module Sanitizable
|
4
|
+
module Macro
|
5
|
+
# Defines one or multiple columns that should be sanitized. The method used
|
6
|
+
# for sanitizing can be defined via the :option key and can either contain the name
|
7
|
+
# of any method that is implemented on the particular class or a custom Proc.
|
8
|
+
# Additionally, an Array of Symbols or Procs can be speficied. Alternatively a block
|
9
|
+
# can be supplied as sanitizer.
|
10
|
+
#
|
11
|
+
# -----
|
12
|
+
#
|
13
|
+
# === Example:
|
14
|
+
# class User < ActiveRecord::Base
|
15
|
+
# sanitizes :content, with: :squish
|
16
|
+
# sanitizes :name, :content, :description, with: [:strip, :upcase]
|
17
|
+
# sanitizes :content do |content|
|
18
|
+
# content.squish.downcase
|
19
|
+
# end
|
20
|
+
# sanitizes :content, with: ->(content) { content.squish.downcase }
|
21
|
+
# end
|
22
|
+
def sanitizes(*attribute_names, &block)
|
23
|
+
include Model unless self.include?(Model)
|
24
|
+
|
25
|
+
options = attribute_names.extract_options!.assert_valid_keys(:with, :on)
|
26
|
+
options[:with] = block || options[:with]
|
27
|
+
raise ArgumentError, "#{self.name}#sanitizes must define a :with option or have block" unless options[:with]
|
28
|
+
raise ArgumentError, "#{self.name}#sanitizes must define at least one attribute" if attribute_names.empty?
|
29
|
+
|
30
|
+
@sanitizable_attributes ||= []
|
31
|
+
@sanitizable_attributes += attribute_names.map do |attribute_name|
|
32
|
+
AttributeSanitizer.new(attribute_name.to_s, options.dup)
|
33
|
+
end
|
34
|
+
@sanitizable_attributes.uniq!
|
35
|
+
end
|
36
|
+
|
37
|
+
def acts_as_sanitizable(*attribute_names, &block)
|
38
|
+
sanitizes(*attribute_names, &block)
|
39
|
+
end
|
40
|
+
|
41
|
+
def skip_sanitization_on(*attribute_names)
|
42
|
+
if self.include?(Model)
|
43
|
+
options = attribute_names.extract_options!.assert_valid_keys(:on)
|
44
|
+
attribute_names = attribute_names.map(&:to_s)
|
45
|
+
context = options[:on]
|
46
|
+
@sanitizable_attributes.delete_if do |attribute|
|
47
|
+
attribute_names.include?(attribute.name) and context == attribute.context
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def reset_sanitization
|
53
|
+
sanitizable_attributes.clear if self.include?(Model)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
ActiveSupport.on_load(:active_record) do
|
59
|
+
ActiveRecord::Base.extend(Sanitizable::Macro)
|
60
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Sanitizable
|
2
|
+
module Model
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
before_validation :_sanitize_attributes
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def inherited(base)
|
11
|
+
base.instance_variable_set(:@sanitizable_attributes, sanitizable_attributes.dup)
|
12
|
+
end
|
13
|
+
|
14
|
+
def sanitizable_attributes
|
15
|
+
@sanitizable_attributes ||= []
|
16
|
+
end
|
17
|
+
|
18
|
+
def sanitizable_attribute_names
|
19
|
+
sanitizable_attributes.collect(&:name).uniq.freeze
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def _sanitize_attributes
|
25
|
+
context = new_record? ? :create : :update
|
26
|
+
self.class.sanitizable_attributes.each do |attribute|
|
27
|
+
if attribute.context.nil? or (!attribute.context.nil? and attribute.context == context)
|
28
|
+
original_value = self.send(attribute.name)
|
29
|
+
unless original_value.nil?
|
30
|
+
sanitized_value = attribute.sanitizers.inject(original_value) do |prev_value, sanitizer_proc|
|
31
|
+
sanitizer_proc.call(prev_value)
|
32
|
+
end
|
33
|
+
self.send("#{attribute.name}=", sanitized_value)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/sanitizable.rb
ADDED
data/spec/db/schema.rb
CHANGED
@@ -0,0 +1,141 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Sanitizable do
|
4
|
+
before :each do
|
5
|
+
User.reset_sanitization
|
6
|
+
end
|
7
|
+
|
8
|
+
context 'macro' do
|
9
|
+
it 'must define an attribute' do
|
10
|
+
expect {
|
11
|
+
User.sanitizes with: :strip
|
12
|
+
}.to raise_error ArgumentError, 'User#sanitizes must define at least one attribute'
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'can define multiple attributes' do
|
16
|
+
User.sanitizes :first_name, :last_name, :username, :email, with: :strip
|
17
|
+
expect(User.sanitizable_attribute_names).to match_array %w(first_name last_name username email)
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'sanitizer' do
|
21
|
+
it 'must be defined through :with or a block' do
|
22
|
+
expect {
|
23
|
+
User.sanitizes :first_name
|
24
|
+
}.to raise_error ArgumentError, 'User#sanitizes must define a :with option or have block'
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'prefers block sanitizer over :with sanitizer' do
|
28
|
+
User.sanitizes :first_name, with: :upcase do |first_name|
|
29
|
+
first_name.downcase
|
30
|
+
end
|
31
|
+
user = User.create(first_name: 'Tobias')
|
32
|
+
expect(user.first_name).to eq 'tobias'
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'using :with option' do
|
36
|
+
it 'can define a sanitizer' do
|
37
|
+
User.sanitizes :first_name, :last_name, with: :strip
|
38
|
+
user = User.create(first_name: ' Tobias ', last_name: ' Casper')
|
39
|
+
expect(user.first_name).to eq 'Tobias'
|
40
|
+
expect(user.last_name).to eq 'Casper'
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'can define multiple sanitizers' do
|
44
|
+
User.sanitizes :email, with: [:strip, :downcase]
|
45
|
+
user = User.create(email: 'Tobias.Casper@GMail.com ')
|
46
|
+
expect(user.email).to eq 'tobias.casper@gmail.com'
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'can define a sanitizer from Proc' do
|
50
|
+
User.sanitizes :email, with: ->(email) { email.strip.downcase }
|
51
|
+
user = User.create(email: ' Tobias.Casper@GMail.com')
|
52
|
+
expect(user.email).to eq 'tobias.casper@gmail.com'
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'can define sanitizers from mixed Symbols and Procs' do
|
56
|
+
sanitizers = []
|
57
|
+
sanitizers << lambda { |email| email.strip }
|
58
|
+
sanitizers << :downcase
|
59
|
+
User.sanitizes :email, with: sanitizers
|
60
|
+
user = User.create(email: ' Tobias.Casper@GMail.com')
|
61
|
+
expect(user.email).to eq 'tobias.casper@gmail.com'
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'using a block' do
|
66
|
+
it 'can define a sanitizer' do
|
67
|
+
User.sanitizes :email do |email|
|
68
|
+
email.strip.downcase
|
69
|
+
end
|
70
|
+
user = User.create(email: ' Tobias.Casper@GMail.com ')
|
71
|
+
expect(user.email).to eq 'tobias.casper@gmail.com'
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'in inherited models' do
|
77
|
+
it 'can inherit a copy of the sanitizable attributes from the parent class' do
|
78
|
+
User.sanitizes :first_name, :last_name, with: [:strip, :upcase]
|
79
|
+
Admin = Class.new(User)
|
80
|
+
Admin.sanitizes :email, with: [:strip, :downcase]
|
81
|
+
expect(User.sanitizable_attribute_names).to match_array %w(first_name last_name)
|
82
|
+
expect(Admin.sanitizable_attribute_names).to match_array %w(first_name last_name email)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'contexts' do
|
87
|
+
before :each do
|
88
|
+
User.sanitizes :first_name, with: :downcase, on: :create
|
89
|
+
User.sanitizes :first_name, with: :upcase, on: :update
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'recognizes a new object' do
|
93
|
+
new_user = User.new
|
94
|
+
new_user.first_name = 'Tobias'
|
95
|
+
new_user.save
|
96
|
+
expect(new_user.first_name).to eq 'tobias'
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'recognizes a persisted object' do
|
100
|
+
persisted_user = User.create
|
101
|
+
persisted_user.first_name = 'Tobias'
|
102
|
+
persisted_user.save
|
103
|
+
expect(persisted_user.first_name).to eq 'TOBIAS'
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context 'skip sanitation macro' do
|
109
|
+
it 'skips sanitation for attribute without context' do
|
110
|
+
User.sanitizes :first_name, with: :upcase
|
111
|
+
User.skip_sanitization_on :first_name
|
112
|
+
|
113
|
+
user = User.create(first_name: 'Tobias')
|
114
|
+
expect(user.first_name).to eq 'Tobias'
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'skips sanitation for attribute in same context' do
|
118
|
+
User.sanitizes :first_name, with: :upcase, on: :create
|
119
|
+
User.skip_sanitization_on :first_name, on: :create
|
120
|
+
|
121
|
+
user = User.create(first_name: 'Tobias')
|
122
|
+
expect(user.first_name).to eq 'Tobias'
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'does not skip sanitation for attribute in main context' do
|
126
|
+
User.sanitizes :first_name, with: :upcase, on: :create
|
127
|
+
User.skip_sanitization_on :first_name
|
128
|
+
|
129
|
+
user = User.create(first_name: 'Tobias')
|
130
|
+
expect(user.first_name).to eq 'TOBIAS'
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'does not skip sanitation for attribute in another context' do
|
134
|
+
User.sanitizes :first_name, with: :upcase, on: :create
|
135
|
+
User.skip_sanitization_on :first_name, on: :update
|
136
|
+
|
137
|
+
user = User.create(first_name: 'Tobias')
|
138
|
+
expect(user.first_name).to eq 'TOBIAS'
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -3,24 +3,19 @@ TEST_DIR = File.dirname(__FILE__)
|
|
3
3
|
require 'rubygems'
|
4
4
|
require 'bundler/setup'
|
5
5
|
require 'yaml'
|
6
|
-
require 'factory_girl'
|
7
6
|
require 'rails/all'
|
8
7
|
|
9
|
-
|
10
|
-
#Bundler.setup
|
8
|
+
# Bundler.setup
|
11
9
|
|
12
10
|
ActiveRecord::Base.configurations = YAML.load_file("#{TEST_DIR}/db/database.yml")
|
13
11
|
ActiveRecord::Base.establish_connection(:sqlite3mem)
|
14
12
|
ActiveRecord::Migration.verbose = false
|
15
13
|
|
16
|
-
require '
|
17
|
-
require 'support/
|
18
|
-
Dir["#{TEST_DIR}/factories/*.rb"].each { |file| require file }
|
14
|
+
require 'sanitizable'
|
15
|
+
require 'support/models'
|
19
16
|
require 'rspec/rails'
|
20
17
|
|
21
18
|
RSpec.configure do |config|
|
22
|
-
config.include FactoryGirl::Syntax::Methods
|
23
|
-
|
24
19
|
config.before :suite do
|
25
20
|
I18n.enforce_available_locales = true
|
26
21
|
load "#{TEST_DIR}/db/schema.rb"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_sanitizable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christoph Chilian
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -170,20 +170,6 @@ dependencies:
|
|
170
170
|
- - ">="
|
171
171
|
- !ruby/object:Gem::Version
|
172
172
|
version: 3.0.0
|
173
|
-
- !ruby/object:Gem::Dependency
|
174
|
-
name: factory_girl
|
175
|
-
requirement: !ruby/object:Gem::Requirement
|
176
|
-
requirements:
|
177
|
-
- - ">="
|
178
|
-
- !ruby/object:Gem::Version
|
179
|
-
version: '0'
|
180
|
-
type: :development
|
181
|
-
prerelease: false
|
182
|
-
version_requirements: !ruby/object:Gem::Requirement
|
183
|
-
requirements:
|
184
|
-
- - ">="
|
185
|
-
- !ruby/object:Gem::Version
|
186
|
-
version: '0'
|
187
173
|
description: Sanitizable concern to sanitize attributes the way you like
|
188
174
|
email: christoph@chilian.de
|
189
175
|
executables: []
|
@@ -191,24 +177,26 @@ extensions: []
|
|
191
177
|
extra_rdoc_files:
|
192
178
|
- LICENSE.txt
|
193
179
|
- README.md
|
194
|
-
- README.rdoc
|
195
180
|
files:
|
196
181
|
- ".document"
|
182
|
+
- ".travis.yml"
|
197
183
|
- Gemfile
|
198
184
|
- Gemfile.lock
|
199
185
|
- LICENSE.txt
|
200
186
|
- README.md
|
201
|
-
- README.rdoc
|
202
187
|
- Rakefile
|
203
188
|
- VERSION
|
204
189
|
- acts_as_sanitizable.gemspec
|
205
190
|
- lib/acts_as_sanitizable.rb
|
191
|
+
- lib/sanitizable.rb
|
192
|
+
- lib/sanitizable/attribute_sanitizer.rb
|
193
|
+
- lib/sanitizable/macro.rb
|
194
|
+
- lib/sanitizable/model.rb
|
206
195
|
- spec/db/database.yml
|
207
196
|
- spec/db/schema.rb
|
208
|
-
- spec/
|
209
|
-
- spec/models/acts_as_sanitizable_spec.rb
|
197
|
+
- spec/models/sanitizable_spec.rb
|
210
198
|
- spec/spec_helper.rb
|
211
|
-
- spec/support/
|
199
|
+
- spec/support/models.rb
|
212
200
|
- test/helper.rb
|
213
201
|
- test/test_acts_as_sanitizable.rb
|
214
202
|
homepage: http://github.com/chilian/acts_as_sanitizable
|
data/README.rdoc
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
= acts_as_sanitizable
|
2
|
-
|
3
|
-
Description goes here.
|
4
|
-
|
5
|
-
== Contributing to acts_as_sanitizable
|
6
|
-
|
7
|
-
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
8
|
-
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
9
|
-
* Fork the project.
|
10
|
-
* Start a feature/bugfix branch.
|
11
|
-
* Commit and push until you are happy with your contribution.
|
12
|
-
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
-
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
-
|
15
|
-
== Copyright
|
16
|
-
|
17
|
-
Copyright (c) 2014 Christoph Chilian. See LICENSE.txt for
|
18
|
-
further details.
|
19
|
-
|
data/spec/factories/user.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
|
4
|
-
describe ActsAsSanitizable do
|
5
|
-
|
6
|
-
|
7
|
-
it "first_name ' Christoph ' returns 'Christoph' after strip" do
|
8
|
-
user = create(:user, first_name: " Christoph ")
|
9
|
-
expect(user.first_name).to eq("Christoph")
|
10
|
-
end
|
11
|
-
|
12
|
-
it "last_name 'Chilian' returns 'CHILIAN' after upcase" do
|
13
|
-
user = create(:user, last_name: "Chilian")
|
14
|
-
expect(user.last_name).to eq("CHILIAN")
|
15
|
-
end
|
16
|
-
|
17
|
-
it "email 'chriStoph@Chilian.de ' returns 'christoph@chilian.de' after squish and downcase" do
|
18
|
-
user = create(:user, email: "chriStoph@Chilian.de ")
|
19
|
-
expect(user.email).to eq("christoph@chilian.de")
|
20
|
-
end
|
21
|
-
|
22
|
-
it "username should be downcased gsub'ed" do
|
23
|
-
user = create(:user, username: '@YamYam')
|
24
|
-
expect(user.username).to eq 'yamyam'
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
data/spec/support/user.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
class User < ActiveRecord::Base
|
2
|
-
|
3
|
-
sanitizes :username, with: [:downcase, ->(content) { content.gsub(/\A@/, '') }]
|
4
|
-
sanitizes :first_name, with: :strip
|
5
|
-
sanitizes :last_name, with: :upcase
|
6
|
-
|
7
|
-
sanitizes :email do |content|
|
8
|
-
content.squish.downcase
|
9
|
-
end
|
10
|
-
|
11
|
-
end
|