mm-sanitize 0.1.3 → 0.1.4

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.
Files changed (4) hide show
  1. data/README.rdoc +22 -3
  2. data/Rakefile +1 -1
  3. data/lib/mm-sanitize.rb +29 -10
  4. metadata +4 -4
data/README.rdoc CHANGED
@@ -22,13 +22,32 @@ Once it's loaded into a model, configure with 'sanitize'
22
22
  sanitize :list, :of, :keys
23
23
 
24
24
  This will use the default Sanitize config, if you want to override then set the :config option:
25
-
25
+
26
26
  # Use the built-in Sanitize::Config::RESTRICTED configuration
27
27
  sanitize :title, :config => Sanitize::Config::RESTRICTED
28
-
28
+
29
29
  # Use a custom configuration
30
30
  sanitize :description, :config => {:elements => ['a', 'span']}
31
-
31
+
32
+ The original text is stored as original_#{key}, for example:
33
+
34
+ class Item
35
+ key :description, String
36
+
37
+ sanitize :description
38
+ end
39
+
40
+ item = Item.create(:description => "some <b>text</b>")
41
+ item.description => "some text"
42
+ item.original_description => "some <b>text</b>"
43
+
44
+ IMPORTANT - calling sanitize sets up original_#{key} as a copy of the existing key if it exists
45
+ If the key doesn't yet exist, it will assume it's a String field, so if you use something different then make sure to define it first.
46
+
47
+ You can disable this behaviour by setting :keep_original to false
48
+
49
+ sanitize :description, :keep_original => false
50
+
32
51
  See the Sanitize Docs (https://github.com/rgrove/sanitize) for more details on the different configuration options.
33
52
 
34
53
  == Note on Patches/Pull Requests
data/Rakefile CHANGED
@@ -21,7 +21,7 @@ spec = Gem::Specification.new do |s|
21
21
 
22
22
  # Change these as appropriate
23
23
  s.name = "mm-sanitize"
24
- s.version = "0.1.3"
24
+ s.version = "0.1.4"
25
25
  s.summary = "Tiny plugin for MongoMapper to sanitize strings before validation."
26
26
  s.author = "Richard Livsey"
27
27
  s.email = "youremail@example.com"
data/lib/mm-sanitize.rb CHANGED
@@ -9,17 +9,33 @@ module MongoMapper
9
9
  end
10
10
 
11
11
  module ClassMethods
12
- def sanitize(*keys)
12
+ def sanitize(*args)
13
13
  unless defined?(sanitize_keys)
14
14
  class_attribute :sanitize_keys
15
15
  end
16
16
 
17
- options = keys.pop if keys.last.is_a?(Hash)
18
- options ||= {}
17
+ options = {
18
+ :config => {},
19
+ :keep_original => true
20
+ }
21
+
22
+ if args.last.is_a?(Hash)
23
+ options = options.merge(args.pop)
24
+ end
19
25
 
20
26
  self.sanitize_keys ||= {}
21
- keys.each do |key|
22
- self.sanitize_keys[key] = options[:config]
27
+ args.each do |key_name|
28
+ self.sanitize_keys[key_name] = options
29
+
30
+ if options[:keep_original]
31
+ if key_definition = self.keys[key_name]
32
+ key_type = key_definition.type
33
+ else
34
+ key_type = String
35
+ end
36
+
37
+ self.key :"original_#{key_name}", key_type
38
+ end
23
39
  end
24
40
 
25
41
  self.send(:before_validation, :sanitize_attributes)
@@ -28,16 +44,19 @@ module MongoMapper
28
44
 
29
45
  module InstanceMethods
30
46
  def sanitize_attributes
31
- self.class.sanitize_keys.each do |key, config|
32
- config ||= {}
47
+ self.class.sanitize_keys.each do |key, options|
48
+
49
+ if options[:keep_original]
50
+ self["original_#{key}"] = self[key]
51
+ end
33
52
 
34
53
  if val = self[key]
35
54
  if val.is_a?(Array) || val.is_a?(Set)
36
- self[key] = val.collect{|v| ::Sanitize.clean(v, config) }
55
+ self[key] = val.collect{|v| ::Sanitize.clean(v, options[:config]) }
37
56
  elsif val.is_a?(Hash)
38
- self[key] = val.each{|k, v| val[k] = ::Sanitize.clean(v, config) if v.is_a?(String) }
57
+ self[key] = val.each{|k, v| val[k] = ::Sanitize.clean(v, options[:config]) if v.is_a?(String) }
39
58
  elsif val.is_a?(String)
40
- self[key] = ::Sanitize.clean(val, config)
59
+ self[key] = ::Sanitize.clean(val, options[:config])
41
60
  end
42
61
  end
43
62
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mm-sanitize
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 4
10
+ version: 0.1.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Richard Livsey
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-01 00:00:00 +00:00
18
+ date: 2011-03-02 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency