text_sanitizer 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -7
- data/lib/text_sanitizer/text_sanitizer.rb +1 -0
- data/lib/text_sanitizer/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# TextSanitizer
|
2
2
|
|
3
3
|
I found myself repeating the same text formatting methods over multiple models
|
4
4
|
and realized this wasn't dry, so I made a plugin.
|
@@ -9,7 +9,7 @@ This plugin is useful if you:
|
|
9
9
|
* like to have consistently formatted data in your DB
|
10
10
|
* are going to display the text fields as is (or with .html_safe) in your app and want to make it safer (this uses the [Sanitize](https://github.com/rgrove/sanitize) gem by Ryan Grove)
|
11
11
|
|
12
|
-
|
12
|
+
## How to use
|
13
13
|
|
14
14
|
Install as a gem with:
|
15
15
|
|
@@ -30,11 +30,11 @@ end
|
|
30
30
|
|
31
31
|
This will convert:
|
32
32
|
|
33
|
-
*
|
34
|
-
*
|
35
|
-
*
|
33
|
+
* sanitize_text: `<script>body!</script>` into `body!`
|
34
|
+
* downcase_text: `EMAIL@TEST.COM` into `email@test.com`
|
35
|
+
* capitalize_text: `john SMITH` into `John Smith`
|
36
36
|
|
37
|
-
|
37
|
+
## Custom sanitizers
|
38
38
|
|
39
39
|
You can also add your own sanitizers. To do so, use the following syntax in your model:
|
40
40
|
|
@@ -67,7 +67,7 @@ before_save :upcase_text
|
|
67
67
|
|
68
68
|
def upcase_text
|
69
69
|
[:name, :title].each do |text_field|
|
70
|
-
self.send "#{text_field}
|
70
|
+
self.send "#{text_field}#", read_attribute(text_field).upcase
|
71
71
|
end
|
72
72
|
end
|
73
73
|
```
|
@@ -20,6 +20,7 @@ module TextSanitizer
|
|
20
20
|
attr_name = "text_fields_to_#{op}"
|
21
21
|
self.send hook, "#{op}_text_fields" # defines the hook
|
22
22
|
self.send :mattr_accessor, attr_name # defines the accessor
|
23
|
+
return unless self.table_exists? # exits if the table hasn't been defined, useful for new installs
|
23
24
|
attributes = args.map do |attr| # checks that the attributes exist
|
24
25
|
raise "Can't #{op} attribute #{attr}: doesn't exist." unless
|
25
26
|
attr.to_s.in? self.column_names
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: text_sanitizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06
|
12
|
+
date: 2012-08-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|