active_type 0.4.5 → 0.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +30 -24
  5. data/CHANGELOG.md +73 -2
  6. data/README.md +56 -11
  7. data/Rakefile +22 -1
  8. data/active_type.gemspec +2 -1
  9. data/gemfiles/Gemfile.3.2.mysql2 +1 -0
  10. data/gemfiles/Gemfile.3.2.mysql2.lock +4 -2
  11. data/gemfiles/Gemfile.3.2.sqlite3 +1 -0
  12. data/gemfiles/Gemfile.3.2.sqlite3.lock +4 -2
  13. data/gemfiles/Gemfile.4.0.sqlite3 +1 -0
  14. data/gemfiles/Gemfile.4.0.sqlite3.lock +4 -2
  15. data/gemfiles/Gemfile.4.1.sqlite3 +1 -0
  16. data/gemfiles/Gemfile.4.1.sqlite3.lock +4 -2
  17. data/gemfiles/Gemfile.4.2.1.mysql2 +1 -0
  18. data/gemfiles/Gemfile.4.2.1.mysql2.lock +4 -2
  19. data/gemfiles/Gemfile.4.2.1.pg +1 -0
  20. data/gemfiles/Gemfile.4.2.1.pg.lock +4 -2
  21. data/gemfiles/Gemfile.4.2.1.sqlite3 +1 -0
  22. data/gemfiles/Gemfile.4.2.1.sqlite3.lock +4 -2
  23. data/gemfiles/Gemfile.5.0.0.mysql2.lock +56 -0
  24. data/gemfiles/Gemfile.5.0.0.pg.lock +56 -0
  25. data/gemfiles/Gemfile.5.0.0.sqlite3 +8 -0
  26. data/gemfiles/Gemfile.5.0.0.sqlite3.lock +56 -0
  27. data/gemfiles/Gemfile.5.1.0.mysql2 +8 -0
  28. data/gemfiles/Gemfile.5.1.0.mysql2.lock +56 -0
  29. data/gemfiles/Gemfile.5.1.0.pg +8 -0
  30. data/gemfiles/Gemfile.5.1.0.pg.lock +56 -0
  31. data/gemfiles/Gemfile.5.1.0.sqlite3 +8 -0
  32. data/gemfiles/Gemfile.5.1.0.sqlite3.lock +56 -0
  33. data/lib/active_type/extended_record/inheritance.rb +41 -6
  34. data/lib/active_type/nested_attributes/association.rb +13 -4
  35. data/lib/active_type/nested_attributes/builder.rb +3 -3
  36. data/lib/active_type/nested_attributes/nests_many_association.rb +5 -1
  37. data/lib/active_type/nested_attributes/nests_one_association.rb +3 -2
  38. data/lib/active_type/no_table.rb +129 -42
  39. data/lib/active_type/type_caster.rb +66 -25
  40. data/lib/active_type/util.rb +21 -6
  41. data/lib/active_type/version.rb +1 -1
  42. data/lib/active_type/virtual_attributes.rb +23 -1
  43. data/lib/active_type.rb +13 -3
  44. metadata +16 -55
  45. data/spec/active_type/extended_record/single_table_inheritance_spec.rb +0 -62
  46. data/spec/active_type/extended_record_spec.rb +0 -233
  47. data/spec/active_type/nested_attributes_spec.rb +0 -700
  48. data/spec/active_type/object_spec.rb +0 -400
  49. data/spec/active_type/record_spec.rb +0 -236
  50. data/spec/active_type/util_spec.rb +0 -128
  51. data/spec/integration/holidays_spec.rb +0 -102
  52. data/spec/integration/shape_spec.rb +0 -110
  53. data/spec/integration/sign_in_spec.rb +0 -101
  54. data/spec/integration/sign_up_spec.rb +0 -102
  55. data/spec/shared_examples/accessors.rb +0 -41
  56. data/spec/shared_examples/belongs_to.rb +0 -17
  57. data/spec/shared_examples/coercible_columns.rb +0 -228
  58. data/spec/shared_examples/constructor.rb +0 -30
  59. data/spec/shared_examples/defaults.rb +0 -60
  60. data/spec/shared_examples/dirty_tracking.rb +0 -40
  61. data/spec/shared_examples/dupable.rb +0 -31
  62. data/spec/shared_examples/mass_assignment.rb +0 -26
  63. data/spec/spec_helper.rb +0 -27
  64. data/spec/support/database.rb +0 -55
  65. data/spec/support/database.sample.yml +0 -3
  66. data/spec/support/error_on.rb +0 -12
  67. data/spec/support/i18n.rb +0 -1
  68. data/spec/support/protected_params.rb +0 -20
  69. data/spec/support/time_zone.rb +0 -1
@@ -1,81 +1,167 @@
1
1
  module ActiveType
2
2
 
3
- module NoTable
3
+ if ActiveRecord::VERSION::MAJOR < 5
4
4
 
5
- extend ActiveSupport::Concern
5
+ module NoTable
6
6
 
7
+ extend ActiveSupport::Concern
7
8
 
8
- module ClassMethods
9
+ module ClassMethods
10
+
11
+ def column_types
12
+ {}
13
+ end
14
+
15
+ def columns
16
+ []
17
+ end
18
+
19
+ def primary_key
20
+ nil
21
+ end
22
+
23
+ def destroy(*)
24
+ new
25
+ end
26
+
27
+ def destroy_all(*)
28
+ []
29
+ end
30
+
31
+ def find_by_sql(*)
32
+ []
33
+ end
9
34
 
10
- def primary_key
11
- nil
12
35
  end
13
36
 
14
- def column_types
15
- {}
37
+ def id
38
+ nil
16
39
  end
17
40
 
18
- def columns
41
+ def attribute_names
19
42
  []
20
43
  end
21
44
 
22
- def destroy(*)
23
- new
45
+ def transaction(&block)
46
+ @_current_transaction_records ||= []
47
+ yield
24
48
  end
25
49
 
26
- def destroy_all(*)
27
- []
50
+ def destroy
51
+ @destroyed = true
52
+ freeze
28
53
  end
29
54
 
55
+ def reload
56
+ self
57
+ end
30
58
 
31
- def find_by_sql(*)
32
- []
59
+
60
+ private
61
+
62
+ def create(*)
63
+ true
33
64
  end
34
65
 
35
- end
66
+ def update(*)
67
+ true
68
+ end
36
69
 
37
- def id
38
- nil
39
- end
70
+ if ActiveRecord::Base.private_method_defined?(:create_record)
71
+ def create_record(*)
72
+ true
73
+ end
74
+
75
+ def update_record(*)
76
+ true
77
+ end
78
+ else
79
+ def _create_record(*)
80
+ @new_record = false
81
+ true
82
+ end
83
+
84
+ def _update_record(*)
85
+ true
86
+ end
87
+ end
40
88
 
41
- def attribute_names
42
- []
43
89
  end
44
90
 
45
- def transaction(&block)
46
- @_current_transaction_records ||= []
47
- yield
48
- end
91
+ else
49
92
 
50
- def destroy
51
- @destroyed = true
52
- freeze
53
- end
93
+ # Rails 5+
54
94
 
55
- def reload
56
- self
57
- end
95
+ module NoTable
58
96
 
97
+ extend ActiveSupport::Concern
59
98
 
60
- private
99
+ class DummySchemaCache
61
100
 
62
- def create(*)
63
- true
64
- end
101
+ def columns_hash(table_name)
102
+ {}
103
+ end
65
104
 
66
- def update(*)
67
- true
68
- end
105
+ def data_source_exists?(table_name)
106
+ false
107
+ end
69
108
 
70
- if ActiveRecord::Base.private_method_defined?(:create_record)
71
- def create_record(*)
109
+ def clear_data_source_cache!(table_name)
110
+ end
111
+
112
+ end
113
+
114
+ class DummyConnection < ActiveRecord::ConnectionAdapters::AbstractAdapter
115
+
116
+ attr_reader :schema_cache
117
+
118
+ def initialize(*)
119
+ super
120
+ @schema_cache = DummySchemaCache.new
121
+ end
122
+
123
+ end
124
+
125
+ module ClassMethods
126
+
127
+ def connection
128
+ @connection ||= DummyConnection.new(nil)
129
+ end
130
+
131
+ def destroy(*)
132
+ new
133
+ end
134
+
135
+ def destroy_all(*)
136
+ []
137
+ end
138
+
139
+ def find_by_sql(*)
140
+ []
141
+ end
142
+
143
+ end
144
+
145
+ def destroy
146
+ @destroyed = true
147
+ freeze
148
+ end
149
+
150
+ def reload
151
+ self
152
+ end
153
+
154
+
155
+ private
156
+
157
+ def create(*)
72
158
  true
73
159
  end
74
160
 
75
- def update_record(*)
161
+ def update(*)
76
162
  true
77
163
  end
78
- else
164
+
79
165
  def _create_record(*)
80
166
  @new_record = false
81
167
  true
@@ -84,6 +170,7 @@ module ActiveType
84
170
  def _update_record(*)
85
171
  true
86
172
  end
173
+
87
174
  end
88
175
 
89
176
  end
@@ -1,11 +1,13 @@
1
1
  module ActiveType
2
2
  class TypeCaster
3
3
 
4
- def self.get(type, connection)
4
+ def self.get(type)
5
5
  native_caster = if ActiveRecord::VERSION::STRING < '4.2'
6
6
  NativeCasters::DelegateToColumn.new(type)
7
+ elsif ActiveRecord::VERSION::STRING < '5'
8
+ NativeCasters::DelegateToRails4Type.new(type)
7
9
  else
8
- NativeCasters::DelegateToType.new(type, connection)
10
+ NativeCasters::DelegateToRails5Type.new(type)
9
11
  end
10
12
  new(type, native_caster)
11
13
  end
@@ -20,17 +22,9 @@ module ActiveType
20
22
  # outside the classes that have that responsibility.
21
23
  case @type
22
24
  when :integer
23
- if value == ''
24
- nil
25
- else
26
- native_type_cast_from_user(value)
27
- end
25
+ cast_integer(value)
28
26
  when :timestamp, :datetime
29
- time = native_type_cast_from_user(value)
30
- if time && ActiveRecord::Base.time_zone_aware_attributes
31
- time = ActiveSupport::TimeWithZone.new(nil, Time.zone, time)
32
- end
33
- time
27
+ cast_time(value)
34
28
  else
35
29
  native_type_cast_from_user(value)
36
30
  end
@@ -40,10 +34,40 @@ module ActiveType
40
34
  @native_caster.type_cast_from_user(value)
41
35
  end
42
36
 
37
+ private
38
+
39
+ def cast_integer(value)
40
+ if value == ''
41
+ nil
42
+ else
43
+ native_type_cast_from_user(value)
44
+ end
45
+ end
46
+
47
+ def cast_time(value)
48
+ time = nil
49
+ if ActiveRecord::Base.time_zone_aware_attributes
50
+ if value.is_a?(String)
51
+ time = Time.zone.parse(value) rescue nil
52
+ end
53
+ time ||= native_type_cast_from_user(value)
54
+ if time
55
+ if value.is_a?(String) && value !~ /[+\-Z][\d:]*$/
56
+ # time was given without an explicit zone, so assume it was given in Time.zone
57
+ ActiveSupport::TimeWithZone.new(nil, Time.zone, time)
58
+ else
59
+ time.in_time_zone rescue time
60
+ end
61
+ end
62
+ else
63
+ native_type_cast_from_user(value)
64
+ end
65
+ end
66
+
43
67
  module NativeCasters
44
68
 
45
69
  # Adapter for Rails 3.0 - 4.1.
46
- # In these versions, casting logic lives in ActiveRecord::ConnectionAdapters::Colum
70
+ # In these versions, casting logic lives in ActiveRecord::ConnectionAdapters::Column
47
71
  class DelegateToColumn
48
72
 
49
73
  def initialize(type)
@@ -62,23 +86,14 @@ module ActiveType
62
86
 
63
87
  # Adapter for Rails 4.2+.
64
88
  # In these versions, casting logic lives in subclasses of ActiveRecord::Type::Value
65
- class DelegateToType
89
+ class DelegateToRails4Type
66
90
 
67
- def initialize(type, connection)
91
+ def initialize(type)
68
92
  # The specified type (e.g. "string") may not necessary match the
69
93
  # native type ("varchar") expected by the connection adapter.
70
94
  # PostgreSQL is one of these. Perform a translation if the adapter
71
95
  # supports it (but don't turn a mysql boolean into a tinyint).
72
- if !type.nil? && !(type == :boolean) && connection.respond_to?(:native_database_types)
73
- native_type = connection.native_database_types[type.to_sym]
74
- if native_type && native_type[:name]
75
- type = native_type[:name]
76
- else
77
- # unknown type, we just dont cast
78
- type = nil
79
- end
80
- end
81
- @active_record_type = connection.lookup_cast_type(type)
96
+ @active_record_type = ActiveRecord::ConnectionAdapters::AbstractAdapter.new(nil).lookup_cast_type(type)
82
97
  end
83
98
 
84
99
  def type_cast_from_user(value)
@@ -87,6 +102,32 @@ module ActiveType
87
102
 
88
103
  end
89
104
 
105
+ # Adapter for Rails 5+.
106
+ # In these versions, casting logic lives in subclasses of ActiveRecord::Type::Value
107
+ class DelegateToRails5Type
108
+
109
+ def initialize(type)
110
+ @active_record_type = lookup(type)
111
+ end
112
+
113
+ def type_cast_from_user(value)
114
+ @active_record_type.cast(value)
115
+ end
116
+
117
+ private
118
+
119
+ def lookup(type)
120
+ if type.respond_to?(:cast)
121
+ type
122
+ else
123
+ ActiveRecord::Type.lookup(type, adapter: nil)
124
+ end
125
+ rescue ::ArgumentError
126
+ ActiveRecord::Type::Value.new
127
+ end
128
+
129
+ end
130
+
90
131
  end
91
132
 
92
133
  end
@@ -20,21 +20,40 @@ module ActiveType
20
20
  def cast_record(record, klass)
21
21
  # record.becomes(klass).dup
22
22
  klass.new do |casted|
23
+ using_single_table_inheritance = using_single_table_inheritance?(klass, casted)
24
+
23
25
  # Rails 3.2, 4.2
24
26
  casted.instance_variable_set(:@attributes, record.instance_variable_get(:@attributes))
25
27
  # Rails 3.2
26
- record.instance_variable_set(:@attributes_cache, record.instance_variable_get(:@attributes_cache))
28
+ casted.instance_variable_set(:@attributes_cache, record.instance_variable_get(:@attributes_cache))
27
29
  # Rails 4.2
28
30
  casted.instance_variable_set(:@changed_attributes, record.instance_variable_get(:@changed_attributes))
31
+ # Rails 5.0
32
+ casted.instance_variable_set(:@mutation_tracker, record.instance_variable_get(:@mutation_tracker))
29
33
  # Rails 3.2, 4.2
30
34
  casted.instance_variable_set(:@new_record, record.new_record?)
31
35
  # Rails 3.2, 4.2
32
36
  casted.instance_variable_set(:@destroyed, record.destroyed?)
33
37
  # Rails 3.2, 4.2
34
- casted.instance_variable_set(:@errors, record.errors)
38
+ errors = record.errors
39
+ if errors.kind_of? ActiveModel::Errors
40
+ errors = errors.dup
41
+ # otherwise attributes defined in ActiveType::Record
42
+ # won't be visible to `errors.add`
43
+ errors.instance_variable_set(:@base, casted)
44
+ end
45
+ casted.instance_variable_set(:@errors, errors)
46
+
47
+ casted[klass.inheritance_column] = klass.sti_name if using_single_table_inheritance
35
48
  end
36
49
  end
37
50
 
51
+ # Backport for Rails 3.2
52
+ def using_single_table_inheritance?(klass, record)
53
+ inheritance_column = klass.inheritance_column
54
+ record[inheritance_column].present? && record.has_attribute?(inheritance_column)
55
+ end
56
+
38
57
  def cast_relation(relation, klass)
39
58
  scoped(klass).merge(scoped(relation))
40
59
  end
@@ -42,8 +61,4 @@ module ActiveType
42
61
  extend self
43
62
 
44
63
  end
45
-
46
- # Make Util methods available under the `ActiveType` namespace
47
- # like `ActiveType.cast(...)`
48
- extend Util
49
64
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveType
2
- VERSION = '0.4.5'
2
+ VERSION = '0.7.5'
3
3
  end
@@ -46,7 +46,7 @@ module ActiveType
46
46
  private
47
47
 
48
48
  def add_virtual_column(name, type, options)
49
- type_caster = TypeCaster.get(type, @owner.connection)
49
+ type_caster = TypeCaster.get(type)
50
50
  column = VirtualColumn.new(name, type_caster, options.slice(:default))
51
51
  @owner.virtual_columns_hash = @owner.virtual_columns_hash.merge(name.to_s => column)
52
52
  end
@@ -181,6 +181,28 @@ module ActiveType
181
181
  virtual_attributes[name] = value
182
182
  end
183
183
 
184
+ # Returns the contents of the record as a nicely formatted string.
185
+ def inspect
186
+ inspection = attributes.collect do |name, value|
187
+ "#{name}: #{VirtualAttributes.attribute_for_inspect(value)}"
188
+ end.sort.compact.join(", ")
189
+ "#<#{self.class} #{inspection}>"
190
+ end
191
+
192
+ def self.attribute_for_inspect(value)
193
+ if value.is_a?(String) && value.length > 50
194
+ "#{value[0, 50]}...".inspect
195
+ elsif value.is_a?(Date) || value.is_a?(Time)
196
+ %("#{value.to_s(:db)}")
197
+ elsif value.is_a?(Array) && value.size > 10
198
+ inspected = value.first(10).inspect
199
+ %(#{inspected[0...-1]}, ...])
200
+ else
201
+ value.inspect
202
+ end
203
+ end
204
+
205
+
184
206
  module ClassMethods
185
207
 
186
208
  def _virtual_column(name)
data/lib/active_type.rb CHANGED
@@ -1,10 +1,8 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'active_type/version'
4
+
4
5
  require 'active_record'
5
- require 'active_type/util'
6
- require 'active_type/record'
7
- require 'active_type/object'
8
6
 
9
7
  if ActiveRecord::VERSION::STRING == '4.2.0'
10
8
  raise(<<-MESSAGE.strip_heredoc)
@@ -12,3 +10,15 @@ if ActiveRecord::VERSION::STRING == '4.2.0'
12
10
  For details see https://github.com/makandra/active_type/issues/31
13
11
  MESSAGE
14
12
  end
13
+
14
+ module ActiveType
15
+ extend ActiveSupport::Autoload
16
+
17
+ autoload :Object
18
+ autoload :Record
19
+ autoload :Util
20
+
21
+ # Make Util methods available under the `ActiveType` namespace
22
+ # like `ActiveType.cast(...)`
23
+ extend Util
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_type
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Kraze
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-02-01 00:00:00.000000000 Z
12
+ date: 2017-12-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -60,6 +60,7 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
+ - ".rspec"
63
64
  - ".ruby-version"
64
65
  - ".travis.yml"
65
66
  - CHANGELOG.md
@@ -81,6 +82,16 @@ files:
81
82
  - gemfiles/Gemfile.4.2.1.pg.lock
82
83
  - gemfiles/Gemfile.4.2.1.sqlite3
83
84
  - gemfiles/Gemfile.4.2.1.sqlite3.lock
85
+ - gemfiles/Gemfile.5.0.0.mysql2.lock
86
+ - gemfiles/Gemfile.5.0.0.pg.lock
87
+ - gemfiles/Gemfile.5.0.0.sqlite3
88
+ - gemfiles/Gemfile.5.0.0.sqlite3.lock
89
+ - gemfiles/Gemfile.5.1.0.mysql2
90
+ - gemfiles/Gemfile.5.1.0.mysql2.lock
91
+ - gemfiles/Gemfile.5.1.0.pg
92
+ - gemfiles/Gemfile.5.1.0.pg.lock
93
+ - gemfiles/Gemfile.5.1.0.sqlite3
94
+ - gemfiles/Gemfile.5.1.0.sqlite3.lock
84
95
  - lib/active_type.rb
85
96
  - lib/active_type/extended_record.rb
86
97
  - lib/active_type/extended_record/inheritance.rb
@@ -96,31 +107,6 @@ files:
96
107
  - lib/active_type/util.rb
97
108
  - lib/active_type/version.rb
98
109
  - lib/active_type/virtual_attributes.rb
99
- - spec/active_type/extended_record/single_table_inheritance_spec.rb
100
- - spec/active_type/extended_record_spec.rb
101
- - spec/active_type/nested_attributes_spec.rb
102
- - spec/active_type/object_spec.rb
103
- - spec/active_type/record_spec.rb
104
- - spec/active_type/util_spec.rb
105
- - spec/integration/holidays_spec.rb
106
- - spec/integration/shape_spec.rb
107
- - spec/integration/sign_in_spec.rb
108
- - spec/integration/sign_up_spec.rb
109
- - spec/shared_examples/accessors.rb
110
- - spec/shared_examples/belongs_to.rb
111
- - spec/shared_examples/coercible_columns.rb
112
- - spec/shared_examples/constructor.rb
113
- - spec/shared_examples/defaults.rb
114
- - spec/shared_examples/dirty_tracking.rb
115
- - spec/shared_examples/dupable.rb
116
- - spec/shared_examples/mass_assignment.rb
117
- - spec/spec_helper.rb
118
- - spec/support/database.rb
119
- - spec/support/database.sample.yml
120
- - spec/support/error_on.rb
121
- - spec/support/i18n.rb
122
- - spec/support/protected_params.rb
123
- - spec/support/time_zone.rb
124
110
  homepage: https://github.com/makandra/active_type
125
111
  licenses:
126
112
  - MIT
@@ -133,7 +119,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
133
119
  requirements:
134
120
  - - ">="
135
121
  - !ruby/object:Gem::Version
136
- version: '0'
122
+ version: 1.9.3
137
123
  required_rubygems_version: !ruby/object:Gem::Requirement
138
124
  requirements:
139
125
  - - ">="
@@ -141,33 +127,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
127
  version: '0'
142
128
  requirements: []
143
129
  rubyforge_project:
144
- rubygems_version: 2.4.8
130
+ rubygems_version: 2.4.5.1
145
131
  signing_key:
146
132
  specification_version: 4
147
133
  summary: Make any Ruby object quack like ActiveRecord
148
- test_files:
149
- - spec/active_type/extended_record/single_table_inheritance_spec.rb
150
- - spec/active_type/extended_record_spec.rb
151
- - spec/active_type/nested_attributes_spec.rb
152
- - spec/active_type/object_spec.rb
153
- - spec/active_type/record_spec.rb
154
- - spec/active_type/util_spec.rb
155
- - spec/integration/holidays_spec.rb
156
- - spec/integration/shape_spec.rb
157
- - spec/integration/sign_in_spec.rb
158
- - spec/integration/sign_up_spec.rb
159
- - spec/shared_examples/accessors.rb
160
- - spec/shared_examples/belongs_to.rb
161
- - spec/shared_examples/coercible_columns.rb
162
- - spec/shared_examples/constructor.rb
163
- - spec/shared_examples/defaults.rb
164
- - spec/shared_examples/dirty_tracking.rb
165
- - spec/shared_examples/dupable.rb
166
- - spec/shared_examples/mass_assignment.rb
167
- - spec/spec_helper.rb
168
- - spec/support/database.rb
169
- - spec/support/database.sample.yml
170
- - spec/support/error_on.rb
171
- - spec/support/i18n.rb
172
- - spec/support/protected_params.rb
173
- - spec/support/time_zone.rb
134
+ test_files: []
@@ -1,62 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module STISpec
4
-
5
- class Parent < ActiveRecord::Base
6
- self.table_name = 'sti_records'
7
- end
8
-
9
- class Child < Parent
10
- end
11
-
12
- class ExtendedChild < ActiveType::Record[Child]
13
- end
14
-
15
- class ExtendedExtendedChild < ActiveType::Record[ExtendedChild]
16
- end
17
-
18
- end
19
-
20
-
21
- describe 'ActiveType::Record[STIModel]' do
22
-
23
- describe 'persistence' do
24
-
25
- def should_save_and_load(save_as, load_as)
26
- record = save_as.new(:persisted_string => "string")
27
- expect(record.save).to eq(true)
28
-
29
- reloaded_child = load_as.find(record.id)
30
- expect(reloaded_child.persisted_string).to eq("string")
31
- expect(reloaded_child).to be_a(load_as)
32
- end
33
-
34
- it 'can save and load the active type record' do
35
-
36
- should_save_and_load(STISpec::ExtendedChild, STISpec::ExtendedChild)
37
- end
38
-
39
- it 'can save as base and load as active type record' do
40
- should_save_and_load(STISpec::Child, STISpec::ExtendedChild)
41
- end
42
-
43
- it 'can save as active type and load as base record' do
44
- should_save_and_load(STISpec::ExtendedChild, STISpec::Child)
45
- end
46
-
47
- it 'can load via the base class and convert to active type record' do
48
- record = STISpec::ExtendedChild.new(:persisted_string => "string")
49
- expect(record.save).to eq(true)
50
-
51
- reloaded_child = STISpec::Child.find(record.id).becomes(STISpec::ExtendedChild)
52
- expect(reloaded_child.persisted_string).to eq("string")
53
- expect(reloaded_child).to be_a(STISpec::ExtendedChild)
54
- end
55
-
56
- it 'can save classes further down the inheritance tree' do
57
- should_save_and_load(STISpec::ExtendedExtendedChild, STISpec::ExtendedExtendedChild)
58
- end
59
-
60
- end
61
-
62
- end