hobo_fields 1.3.0.pre29 → 1.3.0.pre31

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.0.pre29
1
+ 1.3.0.pre31
@@ -18,6 +18,10 @@ module Generators
18
18
  self.join_table
19
19
  end
20
20
 
21
+ def table_exists?
22
+ ActiveRecord::Migration.table_exists? table_name
23
+ end
24
+
21
25
  def field_specs
22
26
  i = 0
23
27
  foreign_keys.inject({}) do |h, v|
@@ -5,7 +5,7 @@ module HoboFields
5
5
  COLUMN_TYPE = :text
6
6
 
7
7
  def self.declared(model, name, options)
8
- model.serialize name, options.delete(:class)
8
+ model.serialize name, options.delete(:class) || Object
9
9
  end
10
10
 
11
11
  HoboFields.register_type(:serialized, self)
data/lib/hobo_fields.rb CHANGED
@@ -64,8 +64,10 @@ module HoboFields
64
64
  col_type = type::COLUMN_TYPE
65
65
  return false if val.blank? && (col_type == :integer || col_type == :float || col_type == :decimal)
66
66
  klass = Object.instance_method(:class).bind(val).call # Make sure we get the *real* class
67
- arity = type.instance_method(:initialize).arity
68
- (arity == 1 || arity == -1) && !@never_wrap_types.any? { |c| klass <= c }
67
+ init_method = type.instance_method(:initialize)
68
+ [-1,1].include?(init_method.arity) &&
69
+ init_method.owner != Object.instance_method(:initialize).owner &&
70
+ !@never_wrap_types.any? { |c| klass <= c }
69
71
  end
70
72
 
71
73
  def never_wrap(type)
@@ -2,7 +2,7 @@ require 'fileutils'
2
2
  system %(rake test:prepare_testapp)
3
3
  TESTAPP_PATH = '/tmp/hobo_fields_testapp'
4
4
  FileUtils.chdir TESTAPP_PATH
5
- require 'config/environment'
5
+ require "#{TESTAPP_PATH}/config/environment"
6
6
  require 'rails/generators'
7
7
  Rails::Generators.configure!
8
8
 
@@ -203,6 +203,49 @@ Provides validation of correct email address format.
203
203
  >> HoboFields::Types::PasswordString.new("pass<word>").to_html.html_safe?
204
204
  => true
205
205
 
206
+ ### `HoboFields::Serialized`
207
+
208
+ This type lets you store an arbitrary object as serialized text into
209
+ the database column. You can optionally pass the :class option to
210
+ restrict the column type.
211
+
212
+ >>
213
+ >>
214
+ def migrate(renames={})
215
+ up, down = Generators::Hobo::Migration::Migrator.run(renames)
216
+ ActiveRecord::Migration.class_eval(up)
217
+ ActiveRecord::Base.send(:descendants).each { |model| model.reset_column_information }
218
+ [up, down]
219
+ end
220
+ {.hidden}
221
+
222
+ >>
223
+ class Vault1 < ActiveRecord::Base
224
+ fields do
225
+ content :serialized
226
+ end
227
+ end
228
+
229
+ >> migrate
230
+ >> Vault1.create!(:content => {:key => "in Vault"})
231
+ >> Vault1.first.content
232
+ => {:key => "in Vault"}
233
+
234
+ >>
235
+ class Vault2 < ActiveRecord::Base
236
+ fields do
237
+ content :serialized, :class => Hash
238
+ end
239
+ end
240
+
241
+ >> migrate
242
+ >> Vault2.create!(:content => {:key => "in Vault"})
243
+ >> Vault2.first.content
244
+ => {:key => "in Vault"}
245
+ >> Vault2.create!(:content => 17) rescue ActiveRecord::SerializationTypeMismatch
246
+ >> Vault2.all.size
247
+ => 1 # second record not created because of type mismatch
248
+
206
249
  ## Enum Strings
207
250
 
208
251
  `HoboFields::Types::EnumString` is not a rich type that you use directly. It's a "type generator", rather like Ruby's `Struct`. It's used for the common situation in database driven apps that you want an enumerated type, but it's not worth going to the extra bother of a separate table enumerating the values. For example you could create a type to represent the status of an article:
@@ -349,6 +392,3 @@ Translations only work with named EnumString's. The recommended way of naming t
349
392
  >> markdown.to_html.html_safe?
350
393
  => true
351
394
 
352
- ### `HoboFields::Types::SerializedObject`
353
-
354
- # `SerializedObject` doesn't have `to_html`
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: hobo_fields
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: 6
5
- version: 1.3.0.pre29
5
+ version: 1.3.0.pre31
6
6
  platform: ruby
7
7
  authors:
8
8
  - Tom Locke
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-05 00:00:00 -04:00
13
+ date: 2011-03-29 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -32,7 +32,7 @@ dependencies:
32
32
  requirements:
33
33
  - - "="
34
34
  - !ruby/object:Gem::Version
35
- version: 1.3.0.pre29
35
+ version: 1.3.0.pre31
36
36
  type: :runtime
37
37
  version_requirements: *id002
38
38
  - !ruby/object:Gem::Dependency