belongs_to_hstore 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/.travis.yml +4 -0
- data/README.md +5 -1
- data/lib/belongs_to_hstore.rb +0 -11
- data/lib/belongs_to_hstore/association.rb +14 -9
- data/lib/belongs_to_hstore/version.rb +1 -1
- data/spec/belongs_to_hstore_spec.rb +31 -0
- metadata +20 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 39dc6679c57af7b7ec58fb67390f88b79e1c65d4
|
4
|
+
data.tar.gz: 87df0683e7f0c7117f5945f145e6130176ddd4a0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40f37bd2411560d6cbbdba0f434ef5145d3963657f5b8ade05724b5551856add50872d3f73cb6be345916e8ca2c48879d89c1ecf89545d61b27347f002402c20
|
7
|
+
data.tar.gz: de2357f110d067525e1a54bd6644a56816c7ab9a25ecef8d9c5ff974de19351005ea654a377bad6dca0d0852adbf8a749e5a19cdfaa354e97e64c5e3abd09ad5
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.
|
1
|
+
ruby-2.1.0
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# BelongsToHstore
|
1
|
+
# BelongsToHstore [![Build Status](https://travis-ci.org/evanlok/belongs_to_hstore.png?branch=master)](https://travis-ci.org/evanlok/belongs_to_hstore)
|
2
2
|
|
3
3
|
Create ActiveRecord belongs_to associations using postgresql hstore columns. Compatible with polymorphic associations
|
4
4
|
and supports eager loading.
|
@@ -23,6 +23,9 @@ Or install it yourself as:
|
|
23
23
|
Create an association using an hstore column:
|
24
24
|
```ruby
|
25
25
|
class Audit < ActiveRecord::Base
|
26
|
+
include BelongsToHstore::Association
|
27
|
+
serialize :properties, ActiveRecord::Coders::Hstore # Rails 3 only
|
28
|
+
|
26
29
|
belongs_to_hstore :properties, :item
|
27
30
|
end
|
28
31
|
```
|
@@ -30,6 +33,7 @@ end
|
|
30
33
|
Works for polymorphic associations too:
|
31
34
|
```ruby
|
32
35
|
class Audit < ActiveRecord::Base
|
36
|
+
include BelongsToHstore::Association
|
33
37
|
serialize :properties, ActiveRecord::Coders::Hstore # Rails 3 only
|
34
38
|
|
35
39
|
belongs_to_hstore :properties, :item, :polymorphic => true
|
data/lib/belongs_to_hstore.rb
CHANGED
@@ -3,15 +3,4 @@ require 'belongs_to_hstore/hstore_query_helper'
|
|
3
3
|
require 'belongs_to_hstore/association'
|
4
4
|
|
5
5
|
module BelongsToHstore
|
6
|
-
if defined?(Rails)
|
7
|
-
class Railtie < Rails::Railtie
|
8
|
-
initializer "belongs_to_hstore.active_record" do
|
9
|
-
ActiveSupport.on_load :active_record do
|
10
|
-
ActiveRecord::Base.send :include, BelongsToHstore::Association
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
else
|
15
|
-
ActiveRecord::Base.send :include, BelongsToHstore::Association
|
16
|
-
end
|
17
6
|
end
|
@@ -3,18 +3,27 @@ module BelongsToHstore
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
include BelongsToHstore::HstoreQueryHelper
|
5
5
|
|
6
|
+
included do
|
7
|
+
class_attribute :belongs_to_hstore_attributes, instance_accessor: false, instance_predicate: false
|
8
|
+
self.belongs_to_hstore_attributes = {}
|
9
|
+
end
|
10
|
+
|
6
11
|
module ClassMethods
|
7
12
|
def belongs_to_hstore(store_attribute, name, options={})
|
8
|
-
|
13
|
+
|
14
|
+
self.belongs_to_hstore_attributes = self.belongs_to_hstore_attributes.dup
|
15
|
+
|
9
16
|
key = options[:foreign_key] || "#{name}_id"
|
10
17
|
key_type = key.gsub(/_id$/, '_type')
|
18
|
+
|
11
19
|
store_accessor store_attribute, key.to_s
|
12
|
-
|
20
|
+
self.belongs_to_hstore_attributes[key.to_s]= true
|
13
21
|
|
14
22
|
if options[:polymorphic]
|
15
23
|
store_accessor store_attribute, key_type
|
16
|
-
|
24
|
+
self.belongs_to_hstore_attributes[key_type]= true
|
17
25
|
end
|
26
|
+
|
18
27
|
belongs_to name, options
|
19
28
|
|
20
29
|
define_singleton_method("where_#{store_attribute}") do |options|
|
@@ -22,14 +31,10 @@ module BelongsToHstore
|
|
22
31
|
end
|
23
32
|
end
|
24
33
|
|
25
|
-
def belongs_to_hstore_attributes
|
26
|
-
attrs = @belongs_to_hstore_attributes || Set.new
|
27
|
-
superclass.respond_to?(:belongs_to_hstore_attributes) ? superclass.belongs_to_hstore_attributes + attrs : attrs
|
28
|
-
end
|
29
34
|
end
|
30
35
|
|
31
36
|
def read_attribute(attr_name)
|
32
|
-
if self.class.belongs_to_hstore_attributes
|
37
|
+
if self.class.belongs_to_hstore_attributes[attr_name]
|
33
38
|
send(attr_name)
|
34
39
|
else
|
35
40
|
super
|
@@ -37,7 +42,7 @@ module BelongsToHstore
|
|
37
42
|
end
|
38
43
|
|
39
44
|
def write_attribute(attr_name, attr_value)
|
40
|
-
if self.class.belongs_to_hstore_attributes
|
45
|
+
if self.class.belongs_to_hstore_attributes[attr_name]
|
41
46
|
send("#{attr_name}=", attr_value.to_s)
|
42
47
|
else
|
43
48
|
super
|
@@ -1,18 +1,25 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
class Widget < ActiveRecord::Base
|
4
|
+
include BelongsToHstore::Association
|
5
|
+
|
4
6
|
serialize :properties, Hash
|
5
7
|
|
6
8
|
belongs_to_hstore :properties, :item
|
7
9
|
belongs_to_hstore :properties, :poly_item, :polymorphic => true
|
8
10
|
end
|
9
11
|
|
12
|
+
class ExtendedWidget < Widget
|
13
|
+
belongs_to_hstore :properties, :additonal_item, :class_name => 'Item'
|
14
|
+
end
|
15
|
+
|
10
16
|
class Item < ActiveRecord::Base
|
11
17
|
end
|
12
18
|
|
13
19
|
describe BelongsToHstore do
|
14
20
|
let(:item) { Item.create }
|
15
21
|
let(:widget) { Widget.new }
|
22
|
+
let(:extended_widget) { ExtendedWidget.new }
|
16
23
|
|
17
24
|
it 'sets properties from object' do
|
18
25
|
widget.item = item
|
@@ -43,4 +50,28 @@ describe BelongsToHstore do
|
|
43
50
|
widget.reload.poly_item.class.should == item.class
|
44
51
|
end
|
45
52
|
end
|
53
|
+
|
54
|
+
context 'subclasses' do
|
55
|
+
it 'sets/gets properties from base class' do
|
56
|
+
extended_widget.item = item
|
57
|
+
extended_widget.poly_item = item
|
58
|
+
extended_widget.properties['item_id'].should == item.id.to_s
|
59
|
+
extended_widget.properties['poly_item_id'].should == item.id.to_s
|
60
|
+
extended_widget.properties['poly_item_type'].should == item.class.to_s
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'sets/gets properties from subclass' do
|
64
|
+
extended_widget.additonal_item = item
|
65
|
+
extended_widget.properties['additonal_item_id'].should == item.id.to_s
|
66
|
+
extended_widget.additonal_item.should == item
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'does not add subclass properties to base class' do
|
70
|
+
expect{widget.additonal_item = item}.to raise_error(NameError)
|
71
|
+
expect{widget.additonal_item}.to raise_error(NameError)
|
72
|
+
Widget.belongs_to_hstore_attributes.should include('item_id')
|
73
|
+
Widget.belongs_to_hstore_attributes.should_not include('additional_item_id')
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
46
77
|
end
|
metadata
CHANGED
@@ -1,83 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: belongs_to_hstore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evan Lok
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.3'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.3'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: sqlite3
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
description: Allows hstore columns to store belongs_to association foreign keys with
|
@@ -88,10 +88,11 @@ executables: []
|
|
88
88
|
extensions: []
|
89
89
|
extra_rdoc_files: []
|
90
90
|
files:
|
91
|
-
- .gitignore
|
92
|
-
- .rspec
|
93
|
-
- .ruby-gemset
|
94
|
-
- .ruby-version
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".ruby-gemset"
|
94
|
+
- ".ruby-version"
|
95
|
+
- ".travis.yml"
|
95
96
|
- Gemfile
|
96
97
|
- LICENSE.txt
|
97
98
|
- README.md
|
@@ -115,17 +116,17 @@ require_paths:
|
|
115
116
|
- lib
|
116
117
|
required_ruby_version: !ruby/object:Gem::Requirement
|
117
118
|
requirements:
|
118
|
-
- -
|
119
|
+
- - ">="
|
119
120
|
- !ruby/object:Gem::Version
|
120
121
|
version: '0'
|
121
122
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
123
|
requirements:
|
123
|
-
- -
|
124
|
+
- - ">="
|
124
125
|
- !ruby/object:Gem::Version
|
125
126
|
version: '0'
|
126
127
|
requirements: []
|
127
128
|
rubyforge_project:
|
128
|
-
rubygems_version: 2.
|
129
|
+
rubygems_version: 2.2.1
|
129
130
|
signing_key:
|
130
131
|
specification_version: 4
|
131
132
|
summary: Allows hstore columns to store belongs_to associations
|