belongs_to_hstore 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cf262a9e10ec75ac8d7e1a153833ef61f07545de
4
+ data.tar.gz: a3e34610c95272553f3bbb2d7a31a2ed8b46783c
5
+ SHA512:
6
+ metadata.gz: d2e28a32db4fc0078d5cb99f043768bf76022760ec20b894cfb7de8b9a1679c88679e07ee8c39a6ab956c54b0aaa2e83dedec73ccfe77b21e8639b3160202ec3
7
+ data.tar.gz: 27e19494aba2c3d9053132154dc2c8ee4899c18f34b98631e290f4d1c37929723c7089ac8c16dfa02b150b3b00efc721212026a30b256f31c4030d29333089fe
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1 @@
1
+ belongs_to_hstore
@@ -0,0 +1 @@
1
+ ruby-2.0.0-p247
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in belongs_to_hstore.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Evan Lok
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ # BelongsToHstore
2
+
3
+ Create ActiveRecord belongs_to associations using postgresql hstore columns. Compatible with polymorphic associations
4
+ and supports eager loading.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'activerecord-postgres-hstore' # Rails 3 only
11
+ gem 'belongs_to_hstore'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install belongs_to_hstore
20
+
21
+ ## Usage
22
+
23
+ Create an association using an hstore column:
24
+ ```ruby
25
+ class Audit < ActiveRecord::Base
26
+ belongs_to_hstore :properties, :item
27
+ end
28
+ ```
29
+
30
+ Works for polymorphic associations too:
31
+ ```ruby
32
+ class Audit < ActiveRecord::Base
33
+ serialize :properties, ActiveRecord::Coders::Hstore # Rails 3 only
34
+
35
+ belongs_to_hstore :properties, :item, :polymorphic => true
36
+ end
37
+ ```
38
+
39
+ Eager load hstore associations to eliminate N+1 querying:
40
+ ```ruby
41
+ Audit.includes(:item).all
42
+ ```
43
+
44
+ Use hstore query helpers to find records:
45
+ ```ruby
46
+ Audit.where_properties(:item_id => 123)
47
+ Audit.where_properties(:item_id => [123, 456, 789])
48
+ ```
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'belongs_to_hstore/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "belongs_to_hstore"
8
+ spec.version = BelongsToHstore::VERSION
9
+ spec.authors = ["Evan Lok"]
10
+ spec.email = ["elok45@gmail.com"]
11
+ spec.description = %q{Allows hstore columns to store belongs_to association foreign keys with the same functionality as the default belongs_to}
12
+ spec.summary = %q{Allows hstore columns to store belongs_to associations}
13
+ spec.homepage = "https://github.com/evanlok/belongs_to_hstore"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activerecord", ">= 3.1"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "sqlite3"
27
+ end
@@ -0,0 +1,17 @@
1
+ require 'belongs_to_hstore/version'
2
+ require 'belongs_to_hstore/hstore_query_helper'
3
+ require 'belongs_to_hstore/association'
4
+
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
+ end
@@ -0,0 +1,47 @@
1
+ module BelongsToHstore
2
+ module Association
3
+ extend ActiveSupport::Concern
4
+ include BelongsToHstore::HstoreQueryHelper
5
+
6
+ module ClassMethods
7
+ def belongs_to_hstore(store_attribute, name, options={})
8
+ @belongs_to_hstore_attributes ||= Set.new
9
+ key = options[:foreign_key] || "#{name}_id"
10
+ key_type = key.gsub(/_id$/, '_type')
11
+ store_accessor store_attribute, key.to_s
12
+ @belongs_to_hstore_attributes.add(key.to_s)
13
+
14
+ if options[:polymorphic]
15
+ store_accessor store_attribute, key_type
16
+ @belongs_to_hstore_attributes.add(key_type)
17
+ end
18
+ belongs_to name, options
19
+
20
+ define_singleton_method("where_#{store_attribute}") do |options|
21
+ where_hstore(store_attribute, options)
22
+ end
23
+ end
24
+
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
+ end
30
+
31
+ def read_attribute(attr_name)
32
+ if self.class.belongs_to_hstore_attributes.include?(attr_name.to_s)
33
+ send(attr_name)
34
+ else
35
+ super
36
+ end
37
+ end
38
+
39
+ def write_attribute(attr_name, attr_value)
40
+ if self.class.belongs_to_hstore_attributes.include?(attr_name.to_s)
41
+ send("#{attr_name}=", attr_value.to_s)
42
+ else
43
+ super
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,24 @@
1
+ module BelongsToHstore
2
+ module HstoreQueryHelper
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def where_hstore(hstore_attribute, opts)
7
+ opts.inject(self) do |scope, opt|
8
+ if opt[1].is_a?(Array)
9
+ values = opt[1].map { |val| val.respond_to?(:id) ? val.id.to_s : val.to_s }
10
+ scope.where("#{table_name}.#{hstore_attribute} -> '#{opt[0]}' IN (?)", values)
11
+ else
12
+ if opt[1].nil?
13
+ value = 'NULL'
14
+ else
15
+ value = opt[1].respond_to?(:id) ? opt[1].id : opt[1]
16
+ end
17
+
18
+ scope.where("#{table_name}.#{hstore_attribute} @> '#{opt[0]} => #{value}'")
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module BelongsToHstore
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ class Widget < ActiveRecord::Base
4
+ serialize :properties, Hash
5
+
6
+ belongs_to_hstore :properties, :item
7
+ belongs_to_hstore :properties, :poly_item, :polymorphic => true
8
+ end
9
+
10
+ class Item < ActiveRecord::Base
11
+ end
12
+
13
+ describe BelongsToHstore do
14
+ let(:item) { Item.create }
15
+ let(:widget) { Widget.new }
16
+
17
+ it 'sets properties from object' do
18
+ widget.item = item
19
+ widget.item_id.should == item.id.to_s
20
+ widget.properties['item_id'].should == item.id.to_s
21
+ end
22
+
23
+ it 'returns associated object' do
24
+ widget = Widget.create(:item => item)
25
+ widget.reload.item.should == item
26
+ end
27
+
28
+ it 'sets properties from setter method' do
29
+ widget.item_id = item.id
30
+ widget.properties['item_id'].should == item.id
31
+ widget.item.should == item
32
+ end
33
+
34
+ context 'polymorphic' do
35
+ it 'sets type and id from object' do
36
+ widget.poly_item = item
37
+ widget.properties['poly_item_id'].should == item.id.to_s
38
+ widget.properties['poly_item_type'].should == item.class.to_s
39
+ end
40
+
41
+ it 'returns correct type' do
42
+ widget = Widget.create(:poly_item => item)
43
+ widget.reload.poly_item.class.should == item.class
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,14 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :widgets do |t|
5
+ t.string :name
6
+ t.string :properties
7
+ t.timestamps
8
+ end
9
+
10
+ create_table :items do |t|
11
+ t.string :name
12
+ t.timestamps
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ Dir[File.expand_path("spec/support/**/*.rb")].each { |f| require f }
8
+ require 'belongs_to_hstore'
9
+
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+
15
+ # Run specs in random order to surface order dependencies. If you find an
16
+ # order dependency and want to debug it, you can fix the order by providing
17
+ # the seed, which is printed after each run.
18
+ # --seed 1234
19
+ config.order = 'random'
20
+ end
@@ -0,0 +1,4 @@
1
+ require 'active_record'
2
+
3
+ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
4
+ load File.expand_path("spec/db/schema.rb")
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: belongs_to_hstore
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Evan Lok
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Allows hstore columns to store belongs_to association foreign keys with
84
+ the same functionality as the default belongs_to
85
+ email:
86
+ - elok45@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - .rspec
93
+ - .ruby-gemset
94
+ - .ruby-version
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - belongs_to_hstore.gemspec
100
+ - lib/belongs_to_hstore.rb
101
+ - lib/belongs_to_hstore/association.rb
102
+ - lib/belongs_to_hstore/hstore_query_helper.rb
103
+ - lib/belongs_to_hstore/version.rb
104
+ - spec/belongs_to_hstore_spec.rb
105
+ - spec/db/schema.rb
106
+ - spec/spec_helper.rb
107
+ - spec/support/active_record.rb
108
+ homepage: https://github.com/evanlok/belongs_to_hstore
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.0.3
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Allows hstore columns to store belongs_to associations
132
+ test_files:
133
+ - spec/belongs_to_hstore_spec.rb
134
+ - spec/db/schema.rb
135
+ - spec/spec_helper.rb
136
+ - spec/support/active_record.rb