text_sanitizer 0.0.5 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/text_sanitizer/text_sanitizer.rb +17 -6
- data/lib/text_sanitizer/version.rb +1 -1
- metadata +1 -1
@@ -6,6 +6,9 @@ module TextSanitizer
|
|
6
6
|
self.class.send :define_method, "#{op}_text" do |*args| # defines the class shorthand
|
7
7
|
validate_and_set_text_fields_for op, hook, args
|
8
8
|
end
|
9
|
+
self.class.send :define_method, "#{op}_formatter" do |*args| # defines the class shorthand
|
10
|
+
create_formatted_attribute op, args
|
11
|
+
end
|
9
12
|
send :define_method, "#{op}_text_fields" do # defines the instance shorthand
|
10
13
|
attrs = self.send "text_fields_to_#{op}"
|
11
14
|
do_for_text_fields attrs, op
|
@@ -16,6 +19,14 @@ module TextSanitizer
|
|
16
19
|
end
|
17
20
|
|
18
21
|
private
|
22
|
+
def create_formatted_attribute op, args
|
23
|
+
args.map do |attr|
|
24
|
+
send :define_method, "formatted_#{attr}" do
|
25
|
+
send op, send(attr)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
19
30
|
def validate_and_set_text_fields_for op, hook, args
|
20
31
|
attr_name = "text_fields_to_#{op}"
|
21
32
|
self.send hook, "#{op}_text_fields" # defines the hook
|
@@ -52,15 +63,15 @@ module TextSanitizer
|
|
52
63
|
text.gsub(/\b\w+\b/){$&.capitalize}
|
53
64
|
end
|
54
65
|
|
55
|
-
def convert_newlines_to_html_for text
|
56
|
-
paragraphs = text.split(/\r\n/)
|
57
|
-
paragraphs.map { |p| p.present? ? "<p>#{p}</p>" : "<br>" }.join('')
|
58
|
-
end
|
59
|
-
|
60
66
|
def downcase text
|
61
67
|
text.downcase
|
62
68
|
end
|
63
69
|
|
70
|
+
def newlines_to_html text
|
71
|
+
paragraphs = text.split(/\r\n/)
|
72
|
+
paragraphs.map { |p| p.present? ? "<p>#{p}</p>" : "<br>" }.join('')
|
73
|
+
end
|
74
|
+
|
64
75
|
def sanitize html
|
65
76
|
Sanitize.clean(
|
66
77
|
html,
|
@@ -76,7 +87,7 @@ end
|
|
76
87
|
ActiveRecord::Base.class_eval do
|
77
88
|
include TextSanitizer
|
78
89
|
register_sanitizer :capitalize
|
79
|
-
register_sanitizer :
|
90
|
+
register_sanitizer :newlines_to_html
|
80
91
|
register_sanitizer :downcase
|
81
92
|
register_sanitizer :sanitize
|
82
93
|
end
|