hobo_fields 1.4.0.pre8 → 2.0.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/generators/hobo/model/templates/model_injection.rb.erb +1 -0
- data/lib/hobo_fields/extensions/active_record/attribute_methods.rb +23 -1
- data/test/api.rdoctest +7 -1
- data/test/generators.rdoctest +3 -2
- data/test/migration_generator.rdoctest +2 -2
- data/test/prepare_testapp.rb +1 -1
- data/test/rich_types.rdoctest +5 -5
- metadata +4 -4
data/Rakefile
CHANGED
@@ -24,7 +24,7 @@ include HoboSupport::CommonTasks
|
|
24
24
|
namespace "test" do
|
25
25
|
desc "Run the doctests"
|
26
26
|
task :doctest do |t|
|
27
|
-
files=Dir['test/*.rdoctest'].map {|f| File.expand_path(f)}.join(' ')
|
27
|
+
files=Dir['test/*.rdoctest'].sort.map {|f| File.expand_path(f)}.join(' ')
|
28
28
|
exit(1) if !system("#{RUBYDOCTEST} #{files}")
|
29
29
|
end
|
30
30
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
2.0.0.pre1
|
@@ -10,9 +10,31 @@ ActiveRecord::Base.class_eval do
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
+
# Rails 3.2+
|
14
|
+
def internal_attribute_access_code(attr_name, cast_code)
|
15
|
+
access_code = "(v=@attributes[attr_name]) && #{cast_code}"
|
16
|
+
|
17
|
+
unless attr_name == primary_key
|
18
|
+
access_code.insert(0, "missing_attribute(attr_name, caller) unless @attributes.has_key?(attr_name); ")
|
19
|
+
end
|
20
|
+
|
21
|
+
# This is the Hobo hook - add a type wrapper around the field
|
22
|
+
# value if we have a special type defined
|
23
|
+
if can_wrap_with_hobo_type?(attr_name)
|
24
|
+
access_code = "val = begin; #{access_code}; end; wrapper_type = self.class.attr_type(:#{attr_name}); " +
|
25
|
+
"if HoboFields.can_wrap?(wrapper_type, val); wrapper_type.new(val); else; val; end"
|
26
|
+
end
|
27
|
+
|
28
|
+
if cache_attribute?(attr_name)
|
29
|
+
access_code = "@attributes_cache['#{attr_name}'] ||= begin; #{access_code}; end;"
|
30
|
+
end
|
31
|
+
|
32
|
+
"attr_name = '#{attr_name}'; #{access_code}"
|
33
|
+
end
|
34
|
+
|
35
|
+
# Rails 3.1 or earlier
|
13
36
|
# Define an attribute reader method. Cope with nil column.
|
14
37
|
def define_read_method(symbol, attr_name, column)
|
15
|
-
|
16
38
|
cast_code = column.type_cast_code('v') if column
|
17
39
|
access_code = cast_code ? "(v=@attributes['#{attr_name}']) && #{cast_code}" : "@attributes['#{attr_name}']"
|
18
40
|
|
data/test/api.rdoctest
CHANGED
@@ -24,7 +24,7 @@ This will generate the test, fixture and a model file like this:
|
|
24
24
|
body :text
|
25
25
|
contact_address :email_address
|
26
26
|
end
|
27
|
-
|
27
|
+
attr_accessible :title, :body, :contact_address
|
28
28
|
end
|
29
29
|
|
30
30
|
(Note: `:email_address` is an example of a "Rich Type" provided by HoboFields -- more on those later)
|
@@ -112,6 +112,8 @@ HoboFields adds a few features to your models.
|
|
112
112
|
|
113
113
|
Returns the type (i.e. class) declared for a given field or attribute
|
114
114
|
|
115
|
+
>> Advert.connection.schema_cache.clear!
|
116
|
+
>> Advert.reset_column_information
|
115
117
|
>> Advert.attr_type :title
|
116
118
|
=> String
|
117
119
|
>> Advert.attr_type :body
|
@@ -154,6 +156,7 @@ The `:required` argument to a field gives a `validates_presence_of`:
|
|
154
156
|
fields do
|
155
157
|
title :string, :required
|
156
158
|
end
|
159
|
+
attr_accessible :title
|
157
160
|
end
|
158
161
|
>> a = Advert.new
|
159
162
|
>> a.valid?
|
@@ -174,6 +177,7 @@ The `:unique` argument in a field declaration gives `validates_uniqueness_of`:
|
|
174
177
|
fields do
|
175
178
|
title :string, :unique
|
176
179
|
end
|
180
|
+
attr_accessible :title
|
177
181
|
end
|
178
182
|
>> a = Advert.new :title => "Jimbo"
|
179
183
|
>> a.valid?
|
@@ -194,6 +198,7 @@ Let's get back to the basic Advert class with no validations before we continue:
|
|
194
198
|
body :text
|
195
199
|
contact_address :email_address
|
196
200
|
end
|
201
|
+
attr_accessible :title, :body, :contact_address
|
197
202
|
end
|
198
203
|
|
199
204
|
|
@@ -226,6 +231,7 @@ You can set the type of a virtual field to a rich type, e.g.
|
|
226
231
|
>>
|
227
232
|
class Advert
|
228
233
|
attr_accessor :alternative_email, :type => :email_address
|
234
|
+
attr_accessible :alternative_email
|
229
235
|
end
|
230
236
|
|
231
237
|
By default, virtual fields are not subject to validation.
|
data/test/generators.rdoctest
CHANGED
@@ -11,7 +11,7 @@ doctest: model file exists
|
|
11
11
|
|
12
12
|
doctest: model content matches
|
13
13
|
>> File.read 'app/models/alpha/beta.rb'
|
14
|
-
=> "class Alpha::Beta < ActiveRecord::Base\n\n fields do\n one :string\n two :integer\n end\n\nend\n"
|
14
|
+
=> "class Alpha::Beta < ActiveRecord::Base\n\n fields do\n one :string\n two :integer\n end\n attr_accessible :one, :two\n\nend\n"
|
15
15
|
|
16
16
|
doctest: module file exists
|
17
17
|
>> File.exist? 'app/models/alpha.rb'
|
@@ -54,6 +54,7 @@ doctest: db file exists
|
|
54
54
|
=> true
|
55
55
|
|
56
56
|
doctest: Alpha::Beta class exists
|
57
|
+
>> Alpha::Beta.connection.schema_cache.clear!
|
58
|
+
>> Alpha::Beta.reset_column_information
|
57
59
|
>> Alpha::Beta
|
58
60
|
=> Alpha::Beta(id: integer, one: string, two: integer)
|
59
|
-
|
@@ -157,8 +157,6 @@ Let's apply that change to the database
|
|
157
157
|
|
158
158
|
### Change a type
|
159
159
|
|
160
|
-
>> Advert.attr_type :title
|
161
|
-
=> String
|
162
160
|
>>
|
163
161
|
class Advert
|
164
162
|
fields do
|
@@ -557,6 +555,8 @@ The migration generator is designed to create complete migrations even if many c
|
|
557
555
|
|
558
556
|
First let's confirm we're in a known state. One model, 'Advert', with a string 'title' and text 'body':
|
559
557
|
|
558
|
+
>> Advert.connection.schema_cache.clear!
|
559
|
+
>> Advert.reset_column_information
|
560
560
|
>> Advert.connection.tables
|
561
561
|
=> ["adverts"]
|
562
562
|
>> Advert.columns.*.name
|
data/test/prepare_testapp.rb
CHANGED
data/test/rich_types.rdoctest
CHANGED
@@ -118,6 +118,7 @@ Provides validation of correct email address format.
|
|
118
118
|
fields do
|
119
119
|
body HoboFields::Types::HtmlString
|
120
120
|
end
|
121
|
+
attr_accessible :body
|
121
122
|
end
|
122
123
|
>> article = Article.create!(:body => "</div>>>p1<p>p2</p>p3<nasty>p4</nasty>p5<script>p6<script>p7</script>p8")
|
123
124
|
# some unsafe html fragements are removed on save,
|
@@ -222,6 +223,7 @@ restrict the column type.
|
|
222
223
|
fields do
|
223
224
|
content :serialized
|
224
225
|
end
|
226
|
+
attr_accessible :content
|
225
227
|
end
|
226
228
|
|
227
229
|
>> migrate
|
@@ -234,6 +236,7 @@ restrict the column type.
|
|
234
236
|
fields do
|
235
237
|
content :serialized, :class => Hash
|
236
238
|
end
|
239
|
+
attr_accessible :content
|
237
240
|
end
|
238
241
|
|
239
242
|
>> migrate
|
@@ -241,10 +244,7 @@ restrict the column type.
|
|
241
244
|
>> Vault2.first.content
|
242
245
|
=> {:key => "in Vault"}
|
243
246
|
>> puts Vault2.count
|
244
|
-
>> v = Vault2.create!(:content => 17)
|
245
|
-
>> Vault2.count
|
246
|
-
=> 2 # Invalid type is successfully created...
|
247
|
-
>> v.reload rescue 'broken' # ActiveRecord::SerializationTypeMismatch
|
247
|
+
>> v = Vault2.create!(:content => 17) rescue 'broken' # ActiveRecord::SerializationTypeMismatch
|
248
248
|
=> 'broken'
|
249
249
|
|
250
250
|
## Enum Strings
|
@@ -315,7 +315,7 @@ ActiveRecord validation framework.
|
|
315
315
|
end
|
316
316
|
end
|
317
317
|
>> Article.attr_type :status
|
318
|
-
#<EnumString draft approved published>
|
318
|
+
=> #<EnumString draft approved published>
|
319
319
|
|
320
320
|
Sometimes it's nice to have a proper type name. Here's one way you might go about it:
|
321
321
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hobo_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0.pre1
|
5
5
|
prerelease: 6
|
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-
|
12
|
+
date: 2012-09-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: hobo_support
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - '='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
21
|
+
version: 2.0.0.pre1
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - '='
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
29
|
+
version: 2.0.0.pre1
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: rubydoctest
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|