activerecord-postgres-json-rails4 0.3.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 +7 -0
- data/.document +5 -0
- data/.rspec +1 -0
- data/.rubocop.yml +28 -0
- data/Gemfile +20 -0
- data/LICENSE.txt +20 -0
- data/README.md +37 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/activerecord-postgres-json-rails4.gemspec +81 -0
- data/lib/activerecord-postgres-json.rb +4 -0
- data/lib/activerecord-postgres-json/activerecord.rb +88 -0
- data/lib/activerecord-postgres-json/coders.rb +53 -0
- data/spec/activerecord-postgres-json_spec.rb +103 -0
- data/spec/coder_spec.rb +13 -0
- data/spec/database.yml +4 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/support/database_setup.rb +23 -0
- metadata +197 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2098f104fe3d7e0384110a2f901aabf05d43d1d9a84144a14dced9817150b793
|
4
|
+
data.tar.gz: 2af3403c7a7462c6754b3a780fa9aca4c1a5a58948083a95474b5e99a956a015
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c842dbc0be81af9d6aa42fecd7d780103c79a25ebf0ac4be67752483f3dab3b530a80ab5dd4fe2e221c214acc7ea0c717971f546a2e877bbaa881b777ecc58b3
|
7
|
+
data.tar.gz: 53b6b6372e422c5a29973e982ee3816dec770443d184636c08374502f2690f7bc05e31d1a41993ee61c517a6ec987b47b59d9a36150d184910c1306edc611fb8
|
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
LineLength:
|
2
|
+
Max: 200
|
3
|
+
|
4
|
+
HashSyntax:
|
5
|
+
EnforcedStyle: ruby19
|
6
|
+
|
7
|
+
SingleSpaceBeforeFirstArg:
|
8
|
+
Enabled: false
|
9
|
+
|
10
|
+
Metrics/AbcSize:
|
11
|
+
Max: 26
|
12
|
+
|
13
|
+
Style/Documentation:
|
14
|
+
Enabled: false
|
15
|
+
|
16
|
+
Metrics/ClassLength:
|
17
|
+
Max: 150
|
18
|
+
|
19
|
+
Metrics/MethodLength:
|
20
|
+
Max: 20
|
21
|
+
|
22
|
+
TrailingComma:
|
23
|
+
Enabled: false
|
24
|
+
AllCops:
|
25
|
+
Exclude:
|
26
|
+
- '**/*.gemspec'
|
27
|
+
- 'spec/**/*'
|
28
|
+
- '**/Rakefile'
|
data/Gemfile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
gem 'activerecord', '>= 3.2', '< 4.2'
|
4
|
+
gem 'multi_json'
|
5
|
+
|
6
|
+
# v11 has a removed method that rspec-core < 3.4.4 uses.
|
7
|
+
# See: https://stackoverflow.com/questions/35893584/nomethoderror-undefined-method-last-comment-after-upgrading-to-rake-11
|
8
|
+
gem 'rake', '< 11.0'
|
9
|
+
|
10
|
+
group :development, :test do
|
11
|
+
gem 'rspec', '~> 2.0'
|
12
|
+
gem 'pg', '~> 0.20.0'
|
13
|
+
end
|
14
|
+
|
15
|
+
group :development do
|
16
|
+
gem 'rubocop'
|
17
|
+
gem 'rdoc'
|
18
|
+
gem 'bundler'
|
19
|
+
gem 'jeweler'
|
20
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 Roman Shterenzon
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# activerecord-postgres-json-rails4
|
2
|
+
|
3
|
+
Based off https://github.com/romanbsd/activerecord-postgres-json, including
|
4
|
+
changes from rails-4-support branch
|
5
|
+
https://github.com/nzifnab/activerecord-postgres-json/tree/rails-4-support
|
6
|
+
|
7
|
+
Gem name changed from the original to allow it to be included in gemspec
|
8
|
+
file of other gems, which doesn't allow specifying github sources with
|
9
|
+
specific branches.
|
10
|
+
|
11
|
+
A minimal JSON/JSONB column type support for ActiveRecord 3.2.x through 4.1.x
|
12
|
+
This gem adds the following support:
|
13
|
+
|
14
|
+
1. Using json/jsonb column type in migrations, e.g. `add_column :foo, :bar, :json` or `add_column :foo, :bar, :jsonb`
|
15
|
+
2. json field support in the schema definitions
|
16
|
+
3. JSON coder for using with the `serialize` class method:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
class User < ActiveRecord::Base
|
20
|
+
serialize :settings, ActiveRecord::Coders::JSON
|
21
|
+
serialize :settings, ActiveRecord::Coders::JSON.new(symbolize_keys: true) # for symbolize keys
|
22
|
+
end
|
23
|
+
|
24
|
+
User.first.settings.class # => Hash
|
25
|
+
User.first.settings[:show_popups] # => true
|
26
|
+
...
|
27
|
+
```
|
28
|
+
|
29
|
+
## Contributing to activerecord-postgres-json
|
30
|
+
|
31
|
+
See https://github.com/romanbsd/activerecord-postgres-json#contributing-to-activerecord-postgres-json
|
32
|
+
|
33
|
+
## Copyright
|
34
|
+
|
35
|
+
Copyright (c) 2014 Roman Shterenzon. See LICENSE.txt for
|
36
|
+
further details.
|
37
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts 'Run `bundle install` to install missing gems'
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification...
|
17
|
+
# see http://guides.rubygems.org/specification-reference/ for more options
|
18
|
+
gem.name = 'activerecord-postgres-json-rails4'
|
19
|
+
gem.homepage = 'http://github.com/vhl/activerecord-postgres-json-rails4'
|
20
|
+
gem.license = "MIT"
|
21
|
+
gem.summary = %Q{ActiveRecord support for PostgreSQL JSON type supporting Rails 4.0-4.1}
|
22
|
+
gem.description = gem.summary
|
23
|
+
gem.email = ['romanbsd@yahoo.com', 'nathan.benes@gmail.com']
|
24
|
+
gem.authors = ['Roman Shterenzon', 'Nathan Benes']
|
25
|
+
# dependencies defined in Gemfile
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rspec/core'
|
30
|
+
require 'rspec/core/rake_task'
|
31
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
32
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
33
|
+
end
|
34
|
+
|
35
|
+
desc "Code coverage detail"
|
36
|
+
task :simplecov do
|
37
|
+
ENV['COVERAGE'] = 'true'
|
38
|
+
Rake::Task['spec'].execute
|
39
|
+
end
|
40
|
+
|
41
|
+
task :default => :spec
|
42
|
+
|
43
|
+
require 'rdoc/task'
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ''
|
46
|
+
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = "activerecord-postgres-json-rails4 #{version}"
|
49
|
+
rdoc.rdoc_files.include('README*')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.3.0
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: activerecord-postgres-json-rails4 0.3.0 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "activerecord-postgres-json-rails4".freeze
|
9
|
+
s.version = "0.3.0"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib".freeze]
|
13
|
+
s.authors = ["Roman Shterenzon".freeze, "Nathan Benes".freeze]
|
14
|
+
s.date = "2019-04-04"
|
15
|
+
s.description = "ActiveRecord support for PostgreSQL JSON type supporting Rails 4.0-4.1".freeze
|
16
|
+
s.email = ["romanbsd@yahoo.com".freeze, "nathan.benes@gmail.com".freeze]
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE.txt",
|
19
|
+
"README.md"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
".rspec",
|
24
|
+
".rubocop.yml",
|
25
|
+
"Gemfile",
|
26
|
+
"LICENSE.txt",
|
27
|
+
"README.md",
|
28
|
+
"Rakefile",
|
29
|
+
"VERSION",
|
30
|
+
"activerecord-postgres-json-rails4.gemspec",
|
31
|
+
"lib/activerecord-postgres-json.rb",
|
32
|
+
"lib/activerecord-postgres-json/activerecord.rb",
|
33
|
+
"lib/activerecord-postgres-json/coders.rb",
|
34
|
+
"spec/activerecord-postgres-json_spec.rb",
|
35
|
+
"spec/coder_spec.rb",
|
36
|
+
"spec/database.yml",
|
37
|
+
"spec/spec_helper.rb",
|
38
|
+
"spec/support/database_setup.rb"
|
39
|
+
]
|
40
|
+
s.homepage = "http://github.com/vhl/activerecord-postgres-json-rails4".freeze
|
41
|
+
s.licenses = ["MIT".freeze]
|
42
|
+
s.rubygems_version = "2.7.7".freeze
|
43
|
+
s.summary = "ActiveRecord support for PostgreSQL JSON type supporting Rails 4.0-4.1".freeze
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
s.specification_version = 4
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
s.add_runtime_dependency(%q<activerecord>.freeze, ["< 4.2", ">= 3.2"])
|
50
|
+
s.add_runtime_dependency(%q<multi_json>.freeze, [">= 0"])
|
51
|
+
s.add_runtime_dependency(%q<rake>.freeze, ["< 11.0"])
|
52
|
+
s.add_development_dependency(%q<rspec>.freeze, ["~> 2.0"])
|
53
|
+
s.add_development_dependency(%q<pg>.freeze, ["~> 0.20.0"])
|
54
|
+
s.add_development_dependency(%q<rubocop>.freeze, [">= 0"])
|
55
|
+
s.add_development_dependency(%q<rdoc>.freeze, [">= 0"])
|
56
|
+
s.add_development_dependency(%q<bundler>.freeze, [">= 0"])
|
57
|
+
s.add_development_dependency(%q<jeweler>.freeze, [">= 0"])
|
58
|
+
else
|
59
|
+
s.add_dependency(%q<activerecord>.freeze, ["< 4.2", ">= 3.2"])
|
60
|
+
s.add_dependency(%q<multi_json>.freeze, [">= 0"])
|
61
|
+
s.add_dependency(%q<rake>.freeze, ["< 11.0"])
|
62
|
+
s.add_dependency(%q<rspec>.freeze, ["~> 2.0"])
|
63
|
+
s.add_dependency(%q<pg>.freeze, ["~> 0.20.0"])
|
64
|
+
s.add_dependency(%q<rubocop>.freeze, [">= 0"])
|
65
|
+
s.add_dependency(%q<rdoc>.freeze, [">= 0"])
|
66
|
+
s.add_dependency(%q<bundler>.freeze, [">= 0"])
|
67
|
+
s.add_dependency(%q<jeweler>.freeze, [">= 0"])
|
68
|
+
end
|
69
|
+
else
|
70
|
+
s.add_dependency(%q<activerecord>.freeze, ["< 4.2", ">= 3.2"])
|
71
|
+
s.add_dependency(%q<multi_json>.freeze, [">= 0"])
|
72
|
+
s.add_dependency(%q<rake>.freeze, ["< 11.0"])
|
73
|
+
s.add_dependency(%q<rspec>.freeze, ["~> 2.0"])
|
74
|
+
s.add_dependency(%q<pg>.freeze, ["~> 0.20.0"])
|
75
|
+
s.add_dependency(%q<rubocop>.freeze, [">= 0"])
|
76
|
+
s.add_dependency(%q<rdoc>.freeze, [">= 0"])
|
77
|
+
s.add_dependency(%q<bundler>.freeze, [">= 0"])
|
78
|
+
s.add_dependency(%q<jeweler>.freeze, [">= 0"])
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_record/connection_adapters/postgresql_adapter'
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
module ConnectionAdapters
|
6
|
+
PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:json] = { name: 'json' }
|
7
|
+
PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:jsonb] = { name: 'jsonb' }
|
8
|
+
|
9
|
+
if ::ActiveRecord.version >= Gem::Version.new("4.0.0")
|
10
|
+
# As suggested here: http://www.innovationontherun.com/fixing-unknown-oid-geography-errors-with-postgis-and-rails-4-0/
|
11
|
+
# to prevent the Rails 4.0/4.1 error of
|
12
|
+
# "unknown OID: {field}"
|
13
|
+
PostgreSQLAdapter::OID.register_type('json', PostgreSQLAdapter::OID::Identity.new)
|
14
|
+
PostgreSQLAdapter::OID.register_type('jsonb', PostgreSQLAdapter::OID::Identity.new)
|
15
|
+
end
|
16
|
+
|
17
|
+
class PostgreSQLColumn < Column
|
18
|
+
# Adds the json type for the column.
|
19
|
+
def simplified_type_with_json(field_type)
|
20
|
+
case field_type
|
21
|
+
when 'json'
|
22
|
+
:json
|
23
|
+
when 'jsonb'
|
24
|
+
:jsonb
|
25
|
+
else
|
26
|
+
simplified_type_without_json(field_type)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
alias_method_chain :simplified_type, :json
|
31
|
+
|
32
|
+
class << self
|
33
|
+
def extract_value_from_default_with_json(default)
|
34
|
+
case default
|
35
|
+
when "'{}'::json", "'{}'::jsonb"
|
36
|
+
'{}'
|
37
|
+
when "'[]'::json", "'[]'::jsonb"
|
38
|
+
'[]'
|
39
|
+
else
|
40
|
+
extract_value_from_default_without_json(default)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
alias_method_chain :extract_value_from_default, :json
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class TableDefinition
|
49
|
+
# Adds json type for migrations. So you can add columns to a table like:
|
50
|
+
# create_table :people do |t|
|
51
|
+
# ...
|
52
|
+
# t.json :info
|
53
|
+
# ...
|
54
|
+
# end
|
55
|
+
def json(*args)
|
56
|
+
options = args.extract_options!
|
57
|
+
column_names = args
|
58
|
+
column_names.each { |name| column(name, 'json', options) }
|
59
|
+
end
|
60
|
+
|
61
|
+
def jsonb(*args)
|
62
|
+
options = args.extract_options!
|
63
|
+
column_names = args
|
64
|
+
column_names.each { |name| column(name, 'jsonb', options) }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
class Table
|
69
|
+
# Adds json type for migrations. So you can add columns to a table like:
|
70
|
+
# change_table :people do |t|
|
71
|
+
# ...
|
72
|
+
# t.json :info
|
73
|
+
# ...
|
74
|
+
# end
|
75
|
+
def json(*args)
|
76
|
+
options = args.extract_options!
|
77
|
+
column_names = args
|
78
|
+
column_names.each { |name| column(name, 'json', options) }
|
79
|
+
end
|
80
|
+
|
81
|
+
def jsonb(*args)
|
82
|
+
options = args.extract_options!
|
83
|
+
column_names = args
|
84
|
+
column_names.each { |name| column(name, 'jsonb', options) }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'multi_json'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Coders
|
5
|
+
class JSON
|
6
|
+
def self.load(json)
|
7
|
+
new.load(json)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.dump(json)
|
11
|
+
new.dump(json)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(params = {})
|
15
|
+
@default = {}
|
16
|
+
return unless params.class.name == 'Hash'
|
17
|
+
@default = params[:default] if params[:default]
|
18
|
+
@symbolize_keys = params[:symbolize_keys] if params[:symbolize_keys]
|
19
|
+
end
|
20
|
+
|
21
|
+
def dump(obj)
|
22
|
+
if obj.nil?
|
23
|
+
@default.nil? ? nil : to_json(@default)
|
24
|
+
else
|
25
|
+
to_json(obj)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def load(json)
|
30
|
+
json.nil? ? @default : from_json(json)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def to_json(obj)
|
36
|
+
MultiJson.dump(obj)
|
37
|
+
end
|
38
|
+
|
39
|
+
def from_json(json)
|
40
|
+
convert_object MultiJson.load(json, symbolize_keys: @symbolize_keys)
|
41
|
+
end
|
42
|
+
|
43
|
+
def convert_object(obj)
|
44
|
+
case obj
|
45
|
+
when Array
|
46
|
+
obj.map { |member| convert_object(member) }
|
47
|
+
else
|
48
|
+
obj
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/support/database_setup')
|
3
|
+
|
4
|
+
class Post < ActiveRecord::Base
|
5
|
+
serialize :data, ActiveRecord::Coders::JSON.new(symbolize_keys: true)
|
6
|
+
end
|
7
|
+
|
8
|
+
class PostQuestions < ActiveRecord::Base
|
9
|
+
serialize :tags, ActiveRecord::Coders::JSON
|
10
|
+
end
|
11
|
+
|
12
|
+
describe 'ActiverecordPostgresJson', db: true do
|
13
|
+
before(:all) do
|
14
|
+
ActiveRecord::Schema.define do
|
15
|
+
create_table :posts, force: true do |t|
|
16
|
+
t.column :data, :jsonb
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
Post.reset_column_information
|
21
|
+
|
22
|
+
ActiveRecord::Base.connection.execute <<-SQL
|
23
|
+
CREATE INDEX index_posts_data_gin ON posts
|
24
|
+
USING gin (data);
|
25
|
+
SQL
|
26
|
+
|
27
|
+
ActiveRecord::Base.connection.execute <<-SQL
|
28
|
+
CREATE OR REPLACE VIEW post_questions as
|
29
|
+
SELECT
|
30
|
+
id,
|
31
|
+
data ->> 'title' AS title,
|
32
|
+
data #>> '{author,name}' AS author_name,
|
33
|
+
data #>> '{author,email}' AS author_email,
|
34
|
+
(data ->> 'tags')::jsonb AS tags,
|
35
|
+
(data ->> 'draft')::boolean AS draft
|
36
|
+
FROM posts
|
37
|
+
SQL
|
38
|
+
end
|
39
|
+
|
40
|
+
after(:all) do
|
41
|
+
ActiveRecord::Base.connection.execute 'DROP INDEX IF EXISTS index_posts_data_gin'
|
42
|
+
ActiveRecord::Base.connection.execute 'DROP VIEW IF EXISTS post_questions'
|
43
|
+
end
|
44
|
+
|
45
|
+
before { Post.delete_all }
|
46
|
+
|
47
|
+
let!(:hdd) do
|
48
|
+
Post.create!(data: [
|
49
|
+
{f1: 6, f2: 5, value: true, intencity: 2.0},
|
50
|
+
{f1: 9, f2: 3, value: false, intencity: 1.0}
|
51
|
+
]).reload
|
52
|
+
end
|
53
|
+
|
54
|
+
let!(:tdd) do
|
55
|
+
Post.create!(data: [
|
56
|
+
{f1: 1, f2: 2, value: false, intencity: 2.0},
|
57
|
+
{f1: 1, f2: 4, value: true, intencity: 1.0}
|
58
|
+
]).reload
|
59
|
+
end
|
60
|
+
|
61
|
+
let!(:bdd) do
|
62
|
+
Post.create!(data: {
|
63
|
+
title: 'BDD is woot',
|
64
|
+
author: { name: 'Philippe', email: 'philippe@example.com'},
|
65
|
+
tags: ['bdd', 'testing', 'woot', true],
|
66
|
+
word_count: 42
|
67
|
+
}).reload
|
68
|
+
end
|
69
|
+
|
70
|
+
let!(:foo) do
|
71
|
+
Post.create!(data: {
|
72
|
+
title: 'FOO is bar',
|
73
|
+
author: { name: 'Philippe', email: 'philippe@example.com'},
|
74
|
+
tags: ['foo', 'bar', 42],
|
75
|
+
draft: true
|
76
|
+
}).reload
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'maps fields' do
|
80
|
+
post = PostQuestions.find_by_title! 'FOO is bar'
|
81
|
+
expect(post.author_name).to eq('Philippe')
|
82
|
+
expect(post.author_email).to eq('philippe@example.com')
|
83
|
+
expect(post.tags).to eq ['foo', 'bar', 42]
|
84
|
+
expect(post).to be_draft
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'provides search as if it was a good old table' do
|
88
|
+
expect(PostQuestions.where(author_name: 'Philippe').pluck(:title)).to eq ['BDD is woot', 'FOO is bar']
|
89
|
+
expect(PostQuestions.where(draft: true).count).to eq(1)
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'when retrieve objects as array' do
|
93
|
+
expect(Post.where('data @> \'[{"f1":1}]\'').first.data)
|
94
|
+
.to eq [
|
95
|
+
{f1: 1, f2: 2, value: false, intencity: 2.0},
|
96
|
+
{f1: 1, f2: 4, value: true, intencity: 1.0}
|
97
|
+
]
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'when search in objects array' do
|
101
|
+
expect(Post.where('data @> \'[{"f1":6}]\'').count).to eq(1)
|
102
|
+
end
|
103
|
+
end
|
data/spec/coder_spec.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveRecord::Coders::JSON do
|
4
|
+
it 'converts hashes' do
|
5
|
+
res = described_class.load('{"foo":"bar", "bar":[1,2]}')
|
6
|
+
expect(res).to eq('foo' => 'bar', 'bar' => [1,2])
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'converts arrays' do
|
10
|
+
res = described_class.load('[{"foo":"bar"}, [{"bar":"baz"}], [[1,2],{"baz":"foo"}]]')
|
11
|
+
expect(res).to eq([{'foo' => 'bar'}, [{'bar' => 'baz'}], [[1,2], {'baz' => 'foo'}]])
|
12
|
+
end
|
13
|
+
end
|
data/spec/database.yml
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
|
4
|
+
require 'rspec'
|
5
|
+
require 'active_record'
|
6
|
+
require 'activerecord-postgres-json'
|
7
|
+
|
8
|
+
# Requires supporting files with custom matchers and macros, etc,
|
9
|
+
# in ./support/ and its subdirectories.
|
10
|
+
# Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
11
|
+
|
12
|
+
RSpec.configure do |config|
|
13
|
+
config.around db: true do |example|
|
14
|
+
if example.metadata[:disable_transactions]
|
15
|
+
example.call
|
16
|
+
else
|
17
|
+
ActiveRecord::Base.transaction do
|
18
|
+
begin
|
19
|
+
example.call
|
20
|
+
ensure
|
21
|
+
raise ActiveRecord::Rollback
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'pg'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
db_config = YAML.load_file(File.expand_path('../../database.yml', __FILE__))
|
6
|
+
|
7
|
+
begin
|
8
|
+
ActiveRecord::Base.establish_connection db_config['test']
|
9
|
+
ActiveRecord::Base.connection.active?
|
10
|
+
rescue Exception => e
|
11
|
+
encoding = db_config['test']['encoding'] || ENV['CHARSET'] || 'utf8'
|
12
|
+
begin
|
13
|
+
ActiveRecord::Base.establish_connection(db_config['test'].merge('database' => 'postgres', 'schema_search_path' => 'public'))
|
14
|
+
ActiveRecord::Base.connection.create_database(db_config['test']['database'], db_config['test'].merge('encoding' => encoding))
|
15
|
+
ActiveRecord::Base.establish_connection(db_config['test'])
|
16
|
+
rescue Exception => ec
|
17
|
+
$stderr.puts ec, *(ec.backtrace)
|
18
|
+
$stderr.puts "Couldn't create database for #{db_config['test'].inspect}"
|
19
|
+
end
|
20
|
+
ensure
|
21
|
+
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
22
|
+
ActiveRecord::Base.logger.formatter = ->(_, _, _, msg) { "#{msg}\n" }
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,197 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-postgres-json-rails4
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Roman Shterenzon
|
8
|
+
- Nathan Benes
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2019-04-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "<"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '4.2'
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '3.2'
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - "<"
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '4.2'
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.2'
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: multi_json
|
36
|
+
requirement: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
type: :runtime
|
42
|
+
prerelease: false
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rake
|
50
|
+
requirement: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "<"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '11.0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "<"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '11.0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.0'
|
69
|
+
type: :development
|
70
|
+
prerelease: false
|
71
|
+
version_requirements: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.0'
|
76
|
+
- !ruby/object:Gem::Dependency
|
77
|
+
name: pg
|
78
|
+
requirement: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.20.0
|
83
|
+
type: :development
|
84
|
+
prerelease: false
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.20.0
|
90
|
+
- !ruby/object:Gem::Dependency
|
91
|
+
name: rubocop
|
92
|
+
requirement: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
type: :development
|
98
|
+
prerelease: false
|
99
|
+
version_requirements: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
- !ruby/object:Gem::Dependency
|
105
|
+
name: rdoc
|
106
|
+
requirement: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
type: :development
|
112
|
+
prerelease: false
|
113
|
+
version_requirements: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
- !ruby/object:Gem::Dependency
|
119
|
+
name: bundler
|
120
|
+
requirement: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
type: :development
|
126
|
+
prerelease: false
|
127
|
+
version_requirements: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
- !ruby/object:Gem::Dependency
|
133
|
+
name: jeweler
|
134
|
+
requirement: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
type: :development
|
140
|
+
prerelease: false
|
141
|
+
version_requirements: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
description: ActiveRecord support for PostgreSQL JSON type supporting Rails 4.0-4.1
|
147
|
+
email:
|
148
|
+
- romanbsd@yahoo.com
|
149
|
+
- nathan.benes@gmail.com
|
150
|
+
executables: []
|
151
|
+
extensions: []
|
152
|
+
extra_rdoc_files:
|
153
|
+
- LICENSE.txt
|
154
|
+
- README.md
|
155
|
+
files:
|
156
|
+
- ".document"
|
157
|
+
- ".rspec"
|
158
|
+
- ".rubocop.yml"
|
159
|
+
- Gemfile
|
160
|
+
- LICENSE.txt
|
161
|
+
- README.md
|
162
|
+
- Rakefile
|
163
|
+
- VERSION
|
164
|
+
- activerecord-postgres-json-rails4.gemspec
|
165
|
+
- lib/activerecord-postgres-json.rb
|
166
|
+
- lib/activerecord-postgres-json/activerecord.rb
|
167
|
+
- lib/activerecord-postgres-json/coders.rb
|
168
|
+
- spec/activerecord-postgres-json_spec.rb
|
169
|
+
- spec/coder_spec.rb
|
170
|
+
- spec/database.yml
|
171
|
+
- spec/spec_helper.rb
|
172
|
+
- spec/support/database_setup.rb
|
173
|
+
homepage: http://github.com/vhl/activerecord-postgres-json-rails4
|
174
|
+
licenses:
|
175
|
+
- MIT
|
176
|
+
metadata: {}
|
177
|
+
post_install_message:
|
178
|
+
rdoc_options: []
|
179
|
+
require_paths:
|
180
|
+
- lib
|
181
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
182
|
+
requirements:
|
183
|
+
- - ">="
|
184
|
+
- !ruby/object:Gem::Version
|
185
|
+
version: '0'
|
186
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
187
|
+
requirements:
|
188
|
+
- - ">="
|
189
|
+
- !ruby/object:Gem::Version
|
190
|
+
version: '0'
|
191
|
+
requirements: []
|
192
|
+
rubyforge_project:
|
193
|
+
rubygems_version: 2.7.7
|
194
|
+
signing_key:
|
195
|
+
specification_version: 4
|
196
|
+
summary: ActiveRecord support for PostgreSQL JSON type supporting Rails 4.0-4.1
|
197
|
+
test_files: []
|