activerecord-typedstore 1.1.3 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: ab5f09b64721a41a7eae679a4ae848813504913d1d87815c035352da6b3134eb
4
- data.tar.gz: bc9d48e95b48f9ad9d5b7e02f1edd9a1ce1004e8c93bfb6d18fc4d944d63c161
2
+ SHA1:
3
+ metadata.gz: aa72e9f075af5501961f90eac06098f00a6c2f6a
4
+ data.tar.gz: a32a4b9b052cd8174756e9adf3062e5fd002da7b
5
5
  SHA512:
6
- metadata.gz: bbd034206f96e1063bdb078417b685148346edeeb36968068bd54a2a8e95321ec56d5aa69b50851238607a862a01cba1a1770ac541e283eba12735c11dd84f5a
7
- data.tar.gz: f04cdc9a39338b8b0fcc164da82f4b472acbf7e59543e6f8ebf5b51300e5bf55f4cb01d602a7dfc083833fef38069ed468049a49be3b6ff45b84dac7e0f86952
6
+ metadata.gz: 131bed9f1753d59907ad67582491a8b3dec85b597b15a3b458eecdf1ee33292bb75be81657e8778b2b49d3b7462aeff5e3671d411f6ea9def139cf1444738ee6
7
+ data.tar.gz: d3ec0ae6531e13aa6a7217ee202f8d1342425a259217fe7d3b5e7c190987786becd6fcfea9e087a93442476918e5fe93010a6d9fc5aa7f3fd7c988529c6fb543
@@ -11,6 +11,8 @@ gemfile:
11
11
  env:
12
12
  - TIMEZONE_AWARE=1 POSTGRES=1 MYSQL=1
13
13
  - TIMEZONE_AWARE=0 POSTGRES=1 MYSQL=1
14
+ - TIMEZONE_AWARE=1 POSTGRES=1 POSTGRES_JSON=1
15
+ - TIMEZONE_AWARE=0 POSTGRES=1 POSTGRES_JSON=1
14
16
 
15
17
  addons:
16
18
  postgresql: 9.3
data/README.md CHANGED
@@ -95,7 +95,7 @@ end
95
95
  ```
96
96
 
97
97
  Type casting rules and attribute behavior are exactly the same as a for real database columns.
98
- Actually the only difference is that you wont be able to query on these attributes (unless you use Postgres JSON or HStore types) and that you don't need to do a migration to add / remove an attribute.
98
+ Actually the only difference is that you wont be able to query on these attributes (unless you use JSON or Postgres HStore types) and that you don't need to do a migration to add / remove an attribute.
99
99
 
100
100
  If not, please fill an issue.
101
101
 
@@ -123,21 +123,7 @@ typed_store :settings, coder: Base64MarshalCoder do |s|
123
123
  end
124
124
  ```
125
125
 
126
- If you want to use Postgres HStore or JSON column types, then you need a very simple coder:
127
- ```ruby
128
- module DumbCoder
129
- extend self
130
-
131
- def load(data)
132
- data || {}
133
- end
134
-
135
- def dump(data)
136
- data || {}
137
- end
138
-
139
- end
140
- ```
126
+ If you want to use JSON column or Postgres HStore types, then you can pass in `ActiveRecord::TypedStore::IdentityCoder` as the coder.
141
127
 
142
128
  ## HStore limitations
143
129
 
@@ -29,7 +29,7 @@ module ActiveRecord::TypedStore
29
29
  delegate :keys, to: :@fields
30
30
 
31
31
  NO_DEFAULT_GIVEN = Object.new
32
- [:string, :text, :integer, :float, :datetime, :date, :boolean, :decimal, :any].each do |type|
32
+ [:string, :text, :integer, :float, :time, :datetime, :date, :boolean, :decimal, :any].each do |type|
33
33
  define_method(type) do |name, **options|
34
34
  @fields[name] = Field.new(name, type, options)
35
35
  end
@@ -39,6 +39,7 @@ module ActiveRecord::TypedStore
39
39
  string: ::ActiveRecord::Type::String,
40
40
  float: ::ActiveRecord::Type::Float,
41
41
  date: ::ActiveRecord::Type::Date,
42
+ time: ::ActiveRecord::Type::Time,
42
43
  datetime: ::ActiveRecord::Type::DateTime,
43
44
  decimal: ::ActiveRecord::Type::Decimal,
44
45
  any: ::ActiveRecord::Type::Value,
@@ -0,0 +1,13 @@
1
+ module ActiveRecord::TypedStore
2
+ module IdentityCoder
3
+ extend self
4
+
5
+ def load(data)
6
+ data || {}
7
+ end
8
+
9
+ def dump(data)
10
+ data || {}
11
+ end
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module TypedStore
3
- VERSION = '1.1.3'
3
+ VERSION = '1.2.0'
4
4
  end
5
5
  end
@@ -426,6 +426,58 @@ shared_examples 'any model' do
426
426
 
427
427
  end
428
428
 
429
+ describe 'time attributes' do
430
+ let(:time) { Time.new(1984, 6, 8, 13, 57, 12) }
431
+ let(:time_string) { '1984-06-08 13:57:12' }
432
+ let(:time) { time_string.respond_to?(:in_time_zone) ? time_string.in_time_zone : Time.parse(time_string) }
433
+
434
+ context "with ActiveRecord #{ActiveRecord::VERSION::STRING}" do
435
+ if AR_VERSION < AR_4_0
436
+ it 'has the defined default as initial value' do
437
+ model.save
438
+ expect(model.published_at).to be == time
439
+ end
440
+
441
+ it 'properly cast assigned value to time' do
442
+ model.remind_at = time_string
443
+ expect(model.remind_at).to be == time
444
+ end
445
+ it 'properly cast assigned value to time on save' do
446
+ model.remind_at = time_string
447
+ model.save
448
+ model.reload
449
+ expect(model.remind_at).to be == time
450
+ end
451
+ it 'retreive a Time instance' do
452
+ model.update(published_at: time)
453
+ expect(model.reload.published_at).to be == time
454
+ end
455
+ else
456
+ it 'has the defined default as initial value' do
457
+ model.save
458
+ expect(model.reload.published_at).to be == time
459
+ end
460
+
461
+ it 'retreive a time instance' do
462
+ model.update(published_at: time)
463
+ expect(model.reload.published_at).to be == time
464
+ end
465
+
466
+ if ActiveRecord::Base.time_zone_aware_attributes
467
+ it 'properly cast assigned value to time' do
468
+ model.remind_at = time_string
469
+ expect(model.remind_at).to be == time
470
+ end
471
+ else
472
+ it 'properly cast assigned value to time' do
473
+ model.remind_at = time_string
474
+ expect(model.remind_at).to be == time
475
+ end
476
+ end
477
+ end
478
+ end
479
+ end
480
+
429
481
  describe 'datetime attributes' do
430
482
 
431
483
  let(:datetime) { DateTime.new(1984, 6, 8, 13, 57, 12) }
@@ -513,7 +565,7 @@ shared_examples 'any model' do
513
565
 
514
566
  end
515
567
 
516
- shared_examples 'a store' do |retain_type=true|
568
+ shared_examples 'a store' do |retain_type = true, settings_type = :text|
517
569
  let(:model) { described_class.new }
518
570
 
519
571
  describe "without connection" do
@@ -537,7 +589,7 @@ shared_examples 'a store' do |retain_type=true|
537
589
  describe 'model.typed_stores' do
538
590
  it "can access keys" do
539
591
  stores = model.class.typed_stores
540
- expect(stores[:settings].keys).to eq [:no_default, :name, :email, :cell_phone, :public, :enabled, :age, :max_length, :rate, :price, :published_on, :remind_on, :published_at, :remind_at, :total_price, :shipping_cost, :grades, :tags, :nickname, :author, :source, :signup, :country]
592
+ expect(stores[:settings].keys).to eq [:no_default, :name, :email, :cell_phone, :public, :enabled, :age, :max_length, :rate, :price, :published_on, :remind_on, :published_at_time, :remind_at_time, :published_at, :remind_at, :total_price, :shipping_cost, :grades, :tags, :nickname, :author, :source, :signup, :country]
541
593
  end
542
594
 
543
595
  it "can access keys even when accessors are not defined" do
@@ -617,7 +669,7 @@ shared_examples 'a store' do |retain_type=true|
617
669
  end
618
670
 
619
671
  it 'delegates internal methods to the underlying type' do
620
- expect(model.class.type_for_attribute("settings").type).to eq :text
672
+ expect(model.class.type_for_attribute("settings").type).to eq settings_type
621
673
  end
622
674
  end
623
675
 
@@ -855,7 +907,7 @@ end if defined?(PostgresHstoreTypedStoreModel)
855
907
 
856
908
  describe PostgresJsonTypedStoreModel do
857
909
  it_should_behave_like 'any model'
858
- it_should_behave_like 'a store'
910
+ it_should_behave_like 'a store', true, :json
859
911
  it_should_behave_like 'a model supporting arrays'
860
912
  end if defined?(PostgresJsonTypedStoreModel)
861
913
 
@@ -34,6 +34,9 @@ def define_columns(t)
34
34
  t.date :published_on, default: '1984-06-08', null: false
35
35
  t.date :remind_on
36
36
 
37
+ t.time :published_at_time, default: '1984-06-08 13:57:12', null: false
38
+ t.time :remind_at_time
39
+
37
40
  t.datetime :published_at, default: '1984-06-08 13:57:12', null: false
38
41
  t.datetime :remind_at
39
42
 
@@ -100,8 +103,7 @@ class CreateAllTables < MigrationClass
100
103
  recreate_table(:postgres_hstore_typed_store_models) { |t| t.hstore :settings; t.text :untyped_settings }
101
104
 
102
105
  if ENV['POSTGRES_JSON']
103
- execute "create extension if not exists json"
104
- recreate_table(:postgres_json_typed_store_models) { |t| t.json :settings; t.text :untyped_settings }
106
+ recreate_table(:postgres_json_typed_store_models) { |t| t.json :settings; t.text :explicit_settings; t.text :partial_settings; t.text :untyped_settings }
105
107
  end
106
108
  end
107
109
  end
@@ -180,9 +182,9 @@ if ENV['POSTGRES']
180
182
  establish_connection ENV['POSTGRES_URL'] || :test_postgresql
181
183
  store :untyped_settings, accessors: [:title]
182
184
 
183
- define_store_with_attributes(coder: false)
184
- define_store_with_no_attributes(coder: false)
185
- define_store_with_partial_attributes(coder: false)
185
+ define_store_with_attributes(coder: ColumnCoder.new(AsJson))
186
+ define_store_with_no_attributes(coder: ColumnCoder.new(AsJson))
187
+ define_store_with_partial_attributes(coder: ColumnCoder.new(AsJson))
186
188
  end
187
189
 
188
190
  else
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-typedstore
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean Boussier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-01 00:00:00.000000000 Z
11
+ date: 2018-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -165,6 +165,7 @@ files:
165
165
  - lib/active_record/typed_store/dsl.rb
166
166
  - lib/active_record/typed_store/extension.rb
167
167
  - lib/active_record/typed_store/field.rb
168
+ - lib/active_record/typed_store/identity_coder.rb
168
169
  - lib/active_record/typed_store/type.rb
169
170
  - lib/active_record/typed_store/typed_hash.rb
170
171
  - lib/active_record/typed_store/version.rb
@@ -194,7 +195,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
195
  version: '0'
195
196
  requirements: []
196
197
  rubyforge_project:
197
- rubygems_version: 2.7.3
198
+ rubygems_version: 2.5.1
198
199
  signing_key:
199
200
  specification_version: 4
200
201
  summary: Add type casting and full method attributes support to АctiveRecord store