sanitize_html_fields 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2bb035ebe582cfbf7cd155380a703c7c1b333bfe
4
+ data.tar.gz: 4538080fa7c9d2e99a42ba56375da6c98bdb9e33
5
+ SHA512:
6
+ metadata.gz: a5cd41024ea93f971e57a904ca08dbff42fe91a8d9a5fa2bf1e975175c7c282d3fc0edbdf650706649354459c2a13e536fecd32932ab6e8c15193a2dfbfc007e
7
+ data.tar.gz: 2494aeaad09c1b4d1a7479cb2361e2adb3e3107002e3ea923af6efea86d19ce33d98fe4c0c454b2ed9e713eb5d5ee7987b5bf48c0c6783034555d8ce5b821aba
@@ -0,0 +1,11 @@
1
+ /sanitize_html_fields-1.0.0.gem
2
+ /.idea
3
+ /.bundle/
4
+ /.yardoc
5
+ /Gemfile.lock
6
+ /_yardoc/
7
+ /coverage/
8
+ /doc/
9
+ /pkg/
10
+ /spec/reports/
11
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sanitize_html_fields.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Kai Straßmann
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ # SanitizeHtmlFields
2
+
3
+ This is a tiny gem that sanitizes html when an instance of ActiveRecord is saved.
4
+
5
+ The gem automatically picks up all String columns of a model that have a corresponding `$name_html` column and sanitizes them using the standard Rails `ActionView::Helpers::SanitizeHelper#sanitize` method.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'sanitize_html_fields'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install sanitize_html_fields
22
+
23
+ ## Usage
24
+ Just add sanitize_html_fields to one of your ActiveRecord model classes and you're ready to go!
25
+
26
+ ```ruby
27
+ class MyModel < ActiveRecord::Base
28
+ sanitize_html_fields
29
+ end
30
+ ```
31
+
32
+ From now on SanitizeHtmlFields will automatically sanitize all of the model's String columns that have a corresponding `$name_html` column.
33
+
34
+ Let's say your model has a 'text' column whose contents you'd like to be sanitized. In this case you need another String column named text_html.
35
+
36
+ ### Options
37
+ You can pass an options hash to sanitize_html_fields which is then used for the individual calls to `ActionView::Helpers::SanitizeHelper#sanitize`.
38
+
39
+ With an option hash you can for example allow certain tags or attributes using the `tags` and `attributes` key. See the documentation of [ActionView::Helpers::SanitizeHelper#sanitize](http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html#method-i-sanitize) for more details.
40
+
41
+ You can also use the `convert_newlines` key in the options hash to configure whether or not to convert line breaks to `<br>` tags. The default value for this option is `false`.
42
+
43
+ The `remove_excessive_whitespaces` key is used to control if excessive whitespace characters (multiple spaces, linebreaks, tabs or newlines) should be removed. The default value for this option is `true`.
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ require 'sanitize_html_fields/version'
2
+ require 'sanitize_html_fields/sanitize_html_fields'
3
+
4
+ module SanitizeHtmlFields
5
+ end
6
+
7
+ ActiveRecord::Base.send(:include, SanitizeHtmlFields)
@@ -0,0 +1,43 @@
1
+ module SanitizeHtmlFields
2
+
3
+ def self.included(base)
4
+ base.extend ActionView::Helpers::SanitizeHelper::ClassMethods
5
+ base.send :include, ActionView::Helpers::SanitizeHelper
6
+ base.send :extend, ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def sanitize_html_fields(options = {})
11
+ send :include, InstanceMethods
12
+ send :before_save, :convert_html_fields
13
+
14
+ cattr_accessor :shf_convert_newlines
15
+ self.shf_convert_newlines = options.delete(:convert_newlines) || false
16
+
17
+ cattr_accessor :shf_remove_excessive_whitespaces
18
+ self.shf_remove_excessive_whitespaces = options.delete(:remove_excessive_whitespaces) || true
19
+
20
+ cattr_accessor :shf_options
21
+ self.shf_options = options
22
+ end
23
+ end
24
+
25
+ module InstanceMethods
26
+ include ActionView::Helpers::SanitizeHelper
27
+ include SanitizeHtmlFields::ClassMethods
28
+
29
+ def convert_html_fields
30
+ options = self.class.shf_options
31
+ self.class.content_columns.each do |c|
32
+ if self.respond_to?("#{c.name}_html") && !self.send("#{c.name}").nil?
33
+
34
+ sanitized_html = sanitize(self.send("#{c.name}"), options)
35
+ sanitized_html.gsub!(/[\s\n\r\t]+\Z/, '') if self.class.shf_remove_excessive_whitespaces
36
+ sanitized_html.gsub!("\n", '<br />') if self.class.shf_convert_newlines
37
+
38
+ self.send("#{c.name}_html=", sanitized_html)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module SanitizeHtmlFields
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sanitize_html_fields/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sanitize_html_fields"
8
+ spec.version = SanitizeHtmlFields::VERSION
9
+ spec.authors = ["Kai Straßmann"]
10
+ spec.email = ["derkai@gmail.com"]
11
+
12
+ spec.summary = "This is a tiny gem that sanitizes html when a record is saved."
13
+ spec.homepage = "https://github.com/cbot/sanitize_html_fields"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency("activerecord", [">= 4.0"])
20
+ spec.add_dependency("actionview", [">= 4.0"])
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sanitize_html_fields
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kai Straßmann
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionview
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description:
70
+ email:
71
+ - derkai@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/sanitize_html_fields.rb
82
+ - lib/sanitize_html_fields/sanitize_html_fields.rb
83
+ - lib/sanitize_html_fields/version.rb
84
+ - sanitize_html_fields.gemspec
85
+ homepage: https://github.com/cbot/sanitize_html_fields
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.5.1
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: This is a tiny gem that sanitizes html when a record is saved.
109
+ test_files: []