hobo_fields 1.3.0.pre14 → 1.3.0.pre15
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/hobo_fields/types/email_address.rb +3 -1
- data/lib/hobo_fields/types/enum_string.rb +1 -1
- data/lib/hobo_fields/types/lifecycle_state.rb +1 -1
- data/lib/hobo_fields/types/password_string.rb +1 -1
- data/lib/hobo_fields/types/raw_html_string.rb +1 -1
- data/lib/hobo_fields/types/raw_markdown_string.rb +1 -1
- data/lib/hobo_fields/types/text.rb +2 -3
- data/lib/hobo_fields/types/textile_string.rb +1 -1
- data/lib/hobo_fields.rb +0 -8
- data/test/rich_types.rdoctest +126 -25
- metadata +7 -7
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.3.0.
|
1
|
+
1.3.0.pre15
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_support/core_ext/string/output_safety'
|
2
|
+
|
1
3
|
module HoboFields
|
2
4
|
module Types
|
3
5
|
class EmailAddress < String
|
@@ -13,7 +15,7 @@ module HoboFields
|
|
13
15
|
end
|
14
16
|
|
15
17
|
def to_html(xmldoctype = true)
|
16
|
-
self.sub('@', " at ").gsub('.', ' dot ')
|
18
|
+
ERB::Util.html_escape(self).sub('@', " at ").gsub('.', ' dot ')
|
17
19
|
end
|
18
20
|
|
19
21
|
HoboFields.register_type(:email_address, self)
|
@@ -1,13 +1,12 @@
|
|
1
|
+
require 'active_support/core_ext/string/output_safety'
|
1
2
|
module HoboFields
|
2
3
|
module Types
|
3
4
|
class Text < String
|
4
5
|
|
5
|
-
HTML_ESCAPE = { '&' => '&', '"' => '"', '>' => '>', '<' => '<' }
|
6
|
-
|
7
6
|
COLUMN_TYPE = :text
|
8
7
|
|
9
8
|
def to_html(xmldoctype = true)
|
10
|
-
|
9
|
+
ERB::Util.html_escape(self).gsub("\n", "<br#{xmldoctype ? ' /' : ''}>\n")
|
11
10
|
end
|
12
11
|
|
13
12
|
HoboFields.register_type(:text, self)
|
data/lib/hobo_fields.rb
CHANGED
@@ -44,9 +44,7 @@ module HoboFields
|
|
44
44
|
}
|
45
45
|
|
46
46
|
@field_types = PLAIN_TYPES.with_indifferent_access
|
47
|
-
|
48
47
|
@never_wrap_types = Set.new([NilClass, Hobo::Boolean, TrueClass, FalseClass])
|
49
|
-
|
50
48
|
attr_reader :field_types
|
51
49
|
|
52
50
|
def to_class(type)
|
@@ -58,12 +56,10 @@ module HoboFields
|
|
58
56
|
end
|
59
57
|
end
|
60
58
|
|
61
|
-
|
62
59
|
def to_name(type)
|
63
60
|
field_types.key(type) || ALIAS_TYPES[type]
|
64
61
|
end
|
65
62
|
|
66
|
-
|
67
63
|
def can_wrap?(type, val)
|
68
64
|
col_type = type::COLUMN_TYPE
|
69
65
|
return false if val.blank? && (col_type == :integer || col_type == :float || col_type == :decimal)
|
@@ -72,22 +68,18 @@ module HoboFields
|
|
72
68
|
(arity == 1 || arity == -1) && !@never_wrap_types.any? { |c| klass <= c }
|
73
69
|
end
|
74
70
|
|
75
|
-
|
76
71
|
def never_wrap(type)
|
77
72
|
@never_wrap_types << type
|
78
73
|
end
|
79
74
|
|
80
|
-
|
81
75
|
def register_type(name, klass)
|
82
76
|
field_types[name] = klass
|
83
77
|
end
|
84
78
|
|
85
|
-
|
86
79
|
def plain_type?(type_name)
|
87
80
|
type_name.in?(PLAIN_TYPES)
|
88
81
|
end
|
89
82
|
|
90
|
-
|
91
83
|
def standard_class(name)
|
92
84
|
class_name = STANDARD_TYPES[name]
|
93
85
|
"HoboFields::Types::#{class_name}".constantize if class_name
|
data/test/rich_types.rdoctest
CHANGED
@@ -7,6 +7,13 @@ Our test requires to prepare the testapp:
|
|
7
7
|
|
8
8
|
doctest_require: 'prepare_testapp'
|
9
9
|
|
10
|
+
>>
|
11
|
+
ActiveRecord::Migration.create_table :articles do |t|
|
12
|
+
t.text :body
|
13
|
+
t.string :status
|
14
|
+
end
|
15
|
+
>>
|
16
|
+
|
10
17
|
{.hidden}
|
11
18
|
|
12
19
|
## `to_html` method
|
@@ -26,26 +33,33 @@ This class defines the methods `to_html` to customize the way the type is render
|
|
26
33
|
# Loud text always renderd in caps.
|
27
34
|
# It's rude to shout too much so it's not allowed to be
|
28
35
|
# longer than 100 characters
|
29
|
-
|
36
|
+
>>
|
37
|
+
class LoudText < String
|
30
38
|
|
31
|
-
|
39
|
+
COLUMN_TYPE = :string
|
32
40
|
|
33
|
-
|
41
|
+
HoboFields.register_type(:loud, self)
|
34
42
|
|
35
|
-
|
36
|
-
|
37
|
-
|
43
|
+
def validate
|
44
|
+
"is too long (you shouldn't shout that much)" if length > 100
|
45
|
+
end
|
38
46
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
47
|
+
def format
|
48
|
+
# make sure we have enough exclamation marks
|
49
|
+
self =~ /!!!$/ ? self + "!!!" : self
|
50
|
+
end
|
43
51
|
|
44
|
-
|
45
|
-
|
46
|
-
|
52
|
+
def to_html(xmldoctype = true)
|
53
|
+
upcase
|
54
|
+
end
|
47
55
|
|
48
|
-
|
56
|
+
end
|
57
|
+
>>
|
58
|
+
|
59
|
+
>> LoudText.new("foO<BAa").to_html
|
60
|
+
=> "FOO<BAA"
|
61
|
+
>> LoudText.new("foO<BAa").to_html.html_safe?
|
62
|
+
=> false
|
49
63
|
|
50
64
|
If you place this class in `app/rich_types/loud_text.rb`, Hobo will load it automatically.
|
51
65
|
|
@@ -81,10 +95,41 @@ Provides validation of correct email address format.
|
|
81
95
|
>> bad.validate
|
82
96
|
=> "is invalid"
|
83
97
|
|
98
|
+
>> nasty = HoboFields::Types::EmailAddress.new("foo<nasty><nasty>@baa.com")
|
99
|
+
>> nasty.to_html
|
100
|
+
=> "foo<nasty><nasty> at baa dot com"
|
101
|
+
>> nasty.to_html.html_safe?
|
102
|
+
=> true
|
103
|
+
|
84
104
|
### `HoboFields::Types::HtmlString`
|
85
105
|
|
86
106
|
`HtmlString` provides no special behavior. The main reason for using this type is that the `to_html` method does not do any html-escaping. Use this for columns that store raw HTML in the database.
|
87
107
|
|
108
|
+
# no safety treatments are done by `to_html`.
|
109
|
+
# even if `nasty.to_html` is actually unsafe, it is marked as html_safe.
|
110
|
+
>> nasty = HoboFields::Types::HtmlString.new("p1<p>p2</p>p3<nasty>p4</nasty>p5<script>p6<script>p7</script>p8")
|
111
|
+
>> nasty.to_html
|
112
|
+
=> "p1<p>p2</p>p3<nasty>p4</nasty>p5<script>p6<script>p7</script>p8"
|
113
|
+
>> nasty.to_html.html_safe?
|
114
|
+
=> true
|
115
|
+
|
116
|
+
>>
|
117
|
+
class Article < ActiveRecord::Base
|
118
|
+
fields do
|
119
|
+
body HoboFields::Types::HtmlString
|
120
|
+
end
|
121
|
+
end
|
122
|
+
>> article = Article.create!(:body => "</div>>>p1<p>p2</p>p3<nasty>p4</nasty>p5<script>p6<script>p7</script>p8")
|
123
|
+
# some unsafe html fragements are removed on save,
|
124
|
+
# but there's no guarantees that it is well-formed
|
125
|
+
>> article.body
|
126
|
+
=> "</div>>>p1<p>p2</p>p3p4p5<script>p6p8"
|
127
|
+
>> article.body == article.body.to_html
|
128
|
+
=> true
|
129
|
+
>> article.body.to_html.html_safe?
|
130
|
+
=> true
|
131
|
+
|
132
|
+
|
88
133
|
### `HoboFields::Types::MarkdownString`
|
89
134
|
|
90
135
|
`HoboFields::Types::MarkdownString` provides a `to_html` that renders markdown syntax into html. It requires the bluecloth gem.
|
@@ -97,12 +142,17 @@ Provides validation of correct email address format.
|
|
97
142
|
And text can be *emphasised*
|
98
143
|
)
|
99
144
|
>> markdown.to_html
|
100
|
-
|
101
|
-
|
102
|
-
|
145
|
+
=> "<h1>This is a heading</h1>\n\n<p>And text can be <em>emphasised</em></p>"
|
146
|
+
>> markdown.to_html.html_safe?
|
147
|
+
=> true
|
103
148
|
|
104
|
-
|
105
|
-
|
149
|
+
# some unsafe html fragements are removed by `to_html`,
|
150
|
+
# but there's no guarantees that it is well-formed
|
151
|
+
>> markdown = HoboFields::Types::MarkdownString.new("</div>>>p1<script>p2")
|
152
|
+
>> markdown.to_html
|
153
|
+
=> "<p></div>>>p1</p>"
|
154
|
+
>> markdown.to_html.html_safe?
|
155
|
+
=> true
|
106
156
|
|
107
157
|
### `HoboFields::Types::TextileString`
|
108
158
|
|
@@ -114,7 +164,16 @@ Provides validation of correct email address format.
|
|
114
164
|
)
|
115
165
|
>> textile.to_html
|
116
166
|
=> "<p>Text can be <em>emphasised</em></p>"
|
117
|
-
>>
|
167
|
+
>> textile.to_html.html_safe?
|
168
|
+
=> true
|
169
|
+
|
170
|
+
# some unsafe html fragements are removed by `to_html`,
|
171
|
+
# but there's no guarantees that it is well-formed
|
172
|
+
>> textile = HoboFields::Types::TextileString.new("</div>>>p1<script>p2")
|
173
|
+
>> textile.to_html
|
174
|
+
=> "<p></div>>>p1</p>"
|
175
|
+
>> textile.to_html.html_safe?
|
176
|
+
=> true
|
118
177
|
|
119
178
|
### `HoboFields::Types::Text`
|
120
179
|
|
@@ -124,16 +183,25 @@ Provides validation of correct email address format.
|
|
124
183
|
|
125
184
|
Cat & Mouse)
|
126
185
|
>> text.to_html
|
127
|
-
=>
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
186
|
+
=> "Tom & Jerry<br />\n<br />\n Cat & Mouse"
|
187
|
+
>> text.to_html.html_safe?
|
188
|
+
=> true
|
189
|
+
|
190
|
+
# `to_html` always returns actually html-safe string
|
191
|
+
>> text = HoboFields::Types::Text.new("</div>>>p1<script>p2")
|
192
|
+
>> text.to_html
|
193
|
+
=> "</div>>>p1<script>p2"
|
194
|
+
>> text.to_html.html_safe?
|
195
|
+
=> true
|
132
196
|
|
133
197
|
### `HoboFields::Types::PasswordString`
|
134
198
|
|
135
199
|
`HoboFields::Types::PasswordString` provides a simple `to_html` to prevent accidental display of a password. It simply returns "`[password hidden]`". The type is also used to indicate the need for an `<input type='password'>`
|
136
200
|
|
201
|
+
>> HoboFields::Types::PasswordString.new("pass<word>").to_html
|
202
|
+
=> "[password hidden]"
|
203
|
+
>> HoboFields::Types::PasswordString.new("pass<word>").to_html.html_safe?
|
204
|
+
=> true
|
137
205
|
|
138
206
|
## Enum Strings
|
139
207
|
|
@@ -217,6 +285,11 @@ Sometimes it's nice to have a proper type name. Here's one way you might go abou
|
|
217
285
|
>> Article.attr_type :status
|
218
286
|
=> Article::Status
|
219
287
|
|
288
|
+
>> Article::Status::PUBLISHED.to_html
|
289
|
+
=> "published"
|
290
|
+
>> Article::Status::PUBLISHED.to_html.html_safe?
|
291
|
+
=> true
|
292
|
+
|
220
293
|
### Translating EnumString's
|
221
294
|
|
222
295
|
Named EnumString's may be translated. Here is an example fr.yml:
|
@@ -243,6 +316,8 @@ The translated value is available via `to_html`:
|
|
243
316
|
|
244
317
|
>> Article::Status::PUBLISHED.to_html
|
245
318
|
=> "publiés"
|
319
|
+
>> Article::Status::PUBLISHED.to_html.html_safe?
|
320
|
+
=> true
|
246
321
|
|
247
322
|
Translations only work with named EnumString's. The recommended way of naming the EnumString is to assign it to a constant, but if you do not wish to do this, you can supply the name in an option:
|
248
323
|
|
@@ -251,3 +326,29 @@ Translations only work with named EnumString's. The recommended way of naming t
|
|
251
326
|
|
252
327
|
`tableize` will be called on your name to provide the translation key.
|
253
328
|
|
329
|
+
|
330
|
+
###
|
331
|
+
|
332
|
+
### `HoboFields::Types::RawHtmlString`
|
333
|
+
|
334
|
+
# no safety treatments are done by `to_html`.
|
335
|
+
# even if `nasty.to_html` is actually unsafe, it is marked as html_safe.
|
336
|
+
>> nasty = HoboFields::Types::RawHtmlString.new("p1<p>p2</p>p3<nasty>p4</nasty>p5<script>p6<script>p7</script>p8")
|
337
|
+
>> nasty.to_html
|
338
|
+
=> "p1<p>p2</p>p3<nasty>p4</nasty>p5<script>p6<script>p7</script>p8"
|
339
|
+
>> nasty.to_html.html_safe?
|
340
|
+
=> true
|
341
|
+
|
342
|
+
### `HoboFields::Types::RawMarkdownString`
|
343
|
+
|
344
|
+
# no safety treatments are done by `to_html`.
|
345
|
+
# even if `markdown.to_html` is actually unsafe, it is marked as html_safe.
|
346
|
+
>> markdown = HoboFields::Types::RawMarkdownString.new("</div>>>p1<script>p2")
|
347
|
+
>> markdown.to_html
|
348
|
+
=> "<p></div>>>p1<script>p2</p>"
|
349
|
+
>> markdown.to_html.html_safe?
|
350
|
+
=> true
|
351
|
+
|
352
|
+
### `HoboFields::Types::SerializedObject`
|
353
|
+
|
354
|
+
# `SerializedObject` doesn't have `to_html`
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hobo_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: -
|
4
|
+
hash: -1637175966
|
5
5
|
prerelease: true
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 3
|
9
9
|
- 0
|
10
|
-
-
|
11
|
-
version: 1.3.0.
|
10
|
+
- pre15
|
11
|
+
version: 1.3.0.pre15
|
12
12
|
platform: ruby
|
13
13
|
authors:
|
14
14
|
- Tom Locke
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-
|
19
|
+
date: 2010-11-03 00:00:00 -04:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -43,13 +43,13 @@ dependencies:
|
|
43
43
|
requirements:
|
44
44
|
- - "="
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
hash: -
|
46
|
+
hash: -1637175966
|
47
47
|
segments:
|
48
48
|
- 1
|
49
49
|
- 3
|
50
50
|
- 0
|
51
|
-
-
|
52
|
-
version: 1.3.0.
|
51
|
+
- pre15
|
52
|
+
version: 1.3.0.pre15
|
53
53
|
type: :runtime
|
54
54
|
version_requirements: *id002
|
55
55
|
- !ruby/object:Gem::Dependency
|