activerecord-jsonb-associations 0.1.0 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c81885988f07c3229320ba64f85c6f2cb31f9467
4
- data.tar.gz: 697fdde245ae31c5f568a7d7ee130c297c418fcc
2
+ SHA256:
3
+ metadata.gz: 6dda2c511ebc60b30aa81c8cea90f006e42b5d3105052f6604c417af3e919c69
4
+ data.tar.gz: 02d8a8f20e4cc088d782449a65e08f2cee828d871fae75e220e110d1d4ef6d5a
5
5
  SHA512:
6
- metadata.gz: e790b5b644fa87e1628377fed0a45fd10f8201d2e65b7396e5274a822f3da222e26343bc353c38896e56e68280f5fd7aa39b64ef032d44cd26bca16980ac15af
7
- data.tar.gz: 62d99c36a88e8dad04ac17984b2755da343676577078c4b831ee3cbf84b5167c15792c24fe7baf50860481bda69cef760ed6f3c926bf75cb74227d137f926a7a
6
+ metadata.gz: 692303b45595a928a36cdb6d192786955c0b232006fa18fc349051e2ac81bd613e223068ed67b60f3e3752a1f2a797899a9fc99118dd60bb089f34d54b80bb82
7
+ data.tar.gz: a978d95bcb59ae742df403a0967774fcf412c5df09a751ffa80c5aede9a79950efcf87493fb5a739b08e241e4d9198a396960c7c048b884785b3bfe1883e1865
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # activerecord-json-associations
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/activerecord-jsonb-associations.svg)](https://badge.fury.io/rb/activerecord-jsonb-associations)
4
+
3
5
  Use PostgreSQL JSONB fields to store association information of your models.
4
6
 
5
7
  This gem was created as a solution to this [task](http://cultofmartians.com/tasks/active-record-jsonb-associations.html) from [EvilMartians](http://evilmartians.com).
@@ -9,7 +9,7 @@ module ActiveRecord
9
9
 
10
10
  join_keys = reflection.join_keys
11
11
  key = join_keys.key
12
- value = transform_value(owner[reflection.join_keys.foreign_key])
12
+ value = transform_value(owner[join_keys.foreign_key])
13
13
 
14
14
  if reflection.options.key?(:foreign_store)
15
15
  apply_jsonb_scope(
@@ -18,6 +18,7 @@ module ActiveRecord
18
18
  key, value
19
19
  )
20
20
  elsif reflection && reflection.options.key?(:store)
21
+ return super if reflection.belongs_to?
21
22
  pluralized_key = key.pluralize
22
23
 
23
24
  apply_jsonb_scope(
@@ -35,6 +35,15 @@ module ActiveRecord
35
35
  #{reflection.options[:store]}['#{foreign_key}']
36
36
  end
37
37
 
38
+ def _read_attribute(attr_name)
39
+ key = attr_name.to_s
40
+ if key.ends_with?('_id') && #{reflection.options[:store]}.keys.include?(key)
41
+ #{reflection.options[:store]}[key]
42
+ else
43
+ super
44
+ end
45
+ end
46
+
38
47
  def [](key)
39
48
  key = key.to_s
40
49
  if key.ends_with?('_id') &&
@@ -44,6 +53,16 @@ module ActiveRecord
44
53
  super
45
54
  end
46
55
  end
56
+
57
+ def []=(key, value)
58
+ key = key.to_s
59
+ if key.ends_with?('_id') &&
60
+ #{reflection.options[:store]}.keys.include?(key)
61
+ #{reflection.options[:store]}[key] = value
62
+ else
63
+ super
64
+ end
65
+ end
47
66
  CODE
48
67
  end
49
68
  end
@@ -10,7 +10,12 @@ module ActiveRecord
10
10
  table, table[reflection.options[:foreign_store]],
11
11
  key, foreign_table, foreign_key
12
12
  )
13
- elsif reflection.options.key?(:store)
13
+ elsif reflection.options.key?(:store) && reflection.belongs_to?
14
+ build_eq_constraint(
15
+ foreign_table, foreign_table[reflection.options[:store]],
16
+ foreign_key, table, key
17
+ )
18
+ elsif reflection.options.key?(:store) # && reflection.has_one?
14
19
  build_contains_constraint(
15
20
  table, table[reflection.options[:store]],
16
21
  key.pluralize, foreign_table, foreign_key
@@ -1,7 +1,7 @@
1
1
  module ActiveRecord
2
2
  module JSONB
3
3
  module Associations
4
- VERSION = '0.1.0'.freeze
4
+ VERSION = '0.2.0'.freeze
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-jsonb-associations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yury Lebedev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-22 00:00:00.000000000 Z
11
+ date: 2018-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -89,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  version: '0'
90
90
  requirements: []
91
91
  rubyforge_project:
92
- rubygems_version: 2.6.11
92
+ rubygems_version: 2.7.6
93
93
  signing_key:
94
94
  specification_version: 4
95
95
  summary: Gem for storing association information using PostgreSQL JSONB columns