activerecord-typedstore 0.4.2 → 0.4.3

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
2
  SHA1:
3
- metadata.gz: 4576d86d981102fb975b790007a6c030d1023e80
4
- data.tar.gz: 04265638898e9a80318d931fcfbe4291026f63ee
3
+ metadata.gz: feff41b09759e6bba17c41f3b4ab635286d7852f
4
+ data.tar.gz: 8ea136dccb104e0a43ad7267d425bdc2016cf5d3
5
5
  SHA512:
6
- metadata.gz: eb80e3bfe74bf53c36a4e9c12c2aea8cc5bf612af892dccc2b9c9d52ff3863b9f797ae86634937fb5a99504e1a439520f25dc8a97bf8f1e46619eee6a368fc12
7
- data.tar.gz: 5bc7ab0513420edd67af9821ce47947f70b31873b8e74f539fd0eefc5aacb2b327a4c92834e54e183e17a590c2fdd67837e2eaee031506eb9493b4aa57966ee8
6
+ metadata.gz: 64a75b48fe0f9bd9e768b1fe5947f43ffee8eeef182057493e31958ede64d4f2f8c78292343c3cb8e74d48b605070cd953cbe0b3a96bcdd1b0d733a8294b0ae2
7
+ data.tar.gz: 4e983b5546295199cc3cbc7dcf5ce5109693bd272b7185b9d4d1af05db5538ac5443f5b05834fe911f799ed9602e2c241bc39524bbc9e0d229cd808c458f0b35
@@ -2,6 +2,7 @@ rvm:
2
2
  - 1.9.3
3
3
  - 2.0.0
4
4
  - 2.1.0
5
+ - 2.1.1
5
6
  gemfile:
6
7
  - gemfiles/Gemfile.ar-3.2
7
8
  - gemfiles/Gemfile.ar-4.0
data/README.md CHANGED
@@ -99,6 +99,57 @@ Actually the only difference is that you wont be able to query on these attribut
99
99
 
100
100
  If not, please fill an issue.
101
101
 
102
+ ## Serialization methods
103
+
104
+ Just like for store, you can use any custom coder:
105
+
106
+ ```ruby
107
+ module Base64MarshalCoder
108
+ extend self
109
+
110
+ def load(data)
111
+ return {} unless data
112
+ Marshal.load(Base64.decode64(data))
113
+ end
114
+
115
+ def dump(data)
116
+ Base64.encode64(Marshal.dump(data || {}))
117
+ end
118
+
119
+ end
120
+
121
+ typed_store :settings, coder: Base64MarshalCoder do |s|
122
+ # ...
123
+ end
124
+ ```
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
+ ```
141
+
142
+ ## HStore limitations
143
+
144
+ If you want to persist your store in a Postgres HStore, then there is some limitations imposed by the current HStore implementation in Postgres.
145
+ Since HStore can only store strings:
146
+
147
+ - `array` attributes won't work
148
+ - `any` attributes will be converted to string
149
+
150
+ If you use HStore because you need to be able to query the store from SQL, and any of these limitations are an issue for you,
151
+ than you could probably use the JSON column type, which do not suffer from these limitations and is also queriable.
152
+
102
153
  ## Contributing
103
154
 
104
155
  1. Fork it
@@ -19,6 +19,10 @@ module ActiveRecord::TypedStore
19
19
 
20
20
  delegate :as_indifferent_hash, to: 'self.class'
21
21
 
22
+ def dump(obj)
23
+ @coder.dump(obj.try(:to_hash) || {})
24
+ end
25
+
22
26
  end
23
27
 
24
28
  end
@@ -70,10 +70,6 @@ module ActiveRecord::TypedStore
70
70
  typed_store_attributes.values.select(&:accessor?).map(&:name).map(&:to_s)
71
71
  end
72
72
 
73
- def hstore?(store_attribute)
74
- columns_hash[store_attribute.to_s].try(:type) == :hstore
75
- end
76
-
77
73
  def create_time_zone_conversion_attribute?(name, column)
78
74
  column ||= typed_store_attributes[name]
79
75
  super(name, column)
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module TypedStore
3
- VERSION = '0.4.2'
3
+ VERSION = '0.4.3'
4
4
  end
5
5
  end
@@ -529,6 +529,23 @@ shared_examples 'a store' do |retain_type=true|
529
529
 
530
530
  end
531
531
 
532
+ describe 'updated defaults' do
533
+
534
+ it 'update defaults for outdated serials' do
535
+ model.save!
536
+ expect(model.settings[:brand_new]).to be_nil
537
+ new_column = ActiveRecord::TypedStore::Column.new(:brand_new, :boolean, null: false, default: true)
538
+ begin
539
+ model.class::SettingsHash.columns['brand_new'] = new_column
540
+ model.reload
541
+ expect(model.settings[:brand_new]).to be_true
542
+ ensure
543
+ model.class::SettingsHash.columns.delete('brand_new')
544
+ end
545
+ end
546
+
547
+ end
548
+
532
549
  end
533
550
 
534
551
  shared_examples 'a db backed model' do
@@ -628,7 +645,7 @@ describe PostgresqlRegularARModel do
628
645
  end if defined?(PostgresqlRegularARModel)
629
646
 
630
647
  describe PostgresHstoreTypedStoreModel do
631
- if AR_VERSION >= AR_4_0
648
+ if AR_VERSION >= AR_4_1
632
649
  pending('TODO: Rails edge HStore compatibiliy')
633
650
  else
634
651
  it_should_behave_like 'any model'
@@ -94,7 +94,7 @@ class ColumnCoder
94
94
  end
95
95
 
96
96
  def load(data)
97
- return {} unless data
97
+ return {} if data.blank?
98
98
  @coder.load(data)
99
99
  end
100
100
 
@@ -135,7 +135,7 @@ if ENV['POSTGRES']
135
135
  class PostgresHstoreTypedStoreModel < ActiveRecord::Base
136
136
  establish_connection ENV['POSTGRES_URL'] || 'test_postgresql'
137
137
  store :untyped_settings, accessors: [:title]
138
- typed_store :settings do |s|
138
+ typed_store :settings, coder: ColumnCoder.new(AsJson) do |s|
139
139
  define_store_columns(s)
140
140
  end
141
141
  end
@@ -176,6 +176,7 @@ module MarshalCoder
176
176
  extend self
177
177
 
178
178
  def load(serial)
179
+ return unless serial.present?
179
180
  Marshal.load(Base64.decode64(serial))
180
181
  end
181
182
 
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: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean Boussier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-14 00:00:00.000000000 Z
11
+ date: 2014-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord