active_model_serializers_pg 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 749a92cbe1a5ef80dbfcf46cda82127108e0497465258e19a2023e0847b9696d
4
+ data.tar.gz: 32956f1353aa108fd173d02b2da79391ba45c286efc1e6080bb4e4927f630bf2
5
+ SHA512:
6
+ metadata.gz: f9861a254e392c60be71bb396497e2f9fb226c6c9b1054635d1bd6dcb3bbd04992e5126d084a2f80d277c1d78603e9f82fb9eb166f6d545c373576eefa8d7fc7
7
+ data.tar.gz: ac39133be71300ecea935c95f090e2282b48bd55cd00431e905e04cf32974efec50e8fa34f6b2b4ec10652ae24201eb33c55b7c0c6ef42d8731a35144d761f42
data/.gitignore ADDED
@@ -0,0 +1,25 @@
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
+ bin/
19
+ .env
20
+ .ruby-version
21
+ .ruby-gemset
22
+ gemfiles/*.lock
23
+ *.swp
24
+ *.un~
25
+ .pryrc
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --tty
data/.travis.yml ADDED
@@ -0,0 +1,21 @@
1
+ language: ruby
2
+ sudo: false
3
+ cache: bundler
4
+
5
+ rvm:
6
+ - 2.4.9
7
+ - 2.5.7
8
+ - 2.6.5
9
+
10
+ gemfile:
11
+ - gemfiles/Gemfile.activerecord-5.0.x
12
+ - gemfiles/Gemfile.activerecord-5.1.x
13
+ - gemfiles/Gemfile.activerecord-5.2.x
14
+
15
+ env: DATABASE_URL=postgres://localhost/travis RUBYOPT=-W0
16
+
17
+ before_script:
18
+ - bundle exec rake db:migrate
19
+
20
+ addons:
21
+ postgresql: '9.4'
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ unless ENV['CI']
6
+ gem 'm'
7
+ gem 'pry'
8
+ gem 'pry-highlight'
9
+ gem 'pry-byebug', platforms: [:mri]
10
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2019 Paul A. Jungwirth
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.
data/README.md ADDED
@@ -0,0 +1,111 @@
1
+ # active\_model\_serializers\_pg
2
+
3
+ This gem provides an ActiveModelSerializers adapter that can generate JSON directly in Postgres.
4
+ It was inspired by [postgres\_ext-serializers](https://github.com/DavyJonesLocker/postgres_ext-serializers) which is no longer maintained and only supports Rails 4 and AMS 0.8.
5
+ This gem adds support for Rails 5 and AMS 0.10.
6
+ In addition we provide output in [JSON:API](https://jsonapi.org/) format.
7
+ (I'd like to add normal JSON output too, so let me know if that would be helpful to you.)
8
+
9
+ Building your JSON is Postgres can reduce your response time 10-100x.
10
+ You skip instantiating thousands of Ruby objects (and garbage collecting them later),
11
+ and Postgres can generate the JSON far more quickly than AMS.
12
+
13
+ You can read lots more about this gem's approach at DockYard's blog post,
14
+ [Avoid Rails When Generating JSON responses with PostgreSQL](https://dockyard.com/blog/2014/05/27/avoid-rails-when-generating-json-responses-with-postgresql) by Dan McClain,
15
+ or [watch a YouTube video](https://www.youtube.com/watch?v=tYTw3Jshrqo) of his Postgres Open 2014 Talk [Using PostgreSQL, not Rails, to make Rails faster](http://slides.com/danmcclain/postgresopen-2014) (Slides).
16
+ Not everything is this same, but hopefully you'll still get the general idea.
17
+
18
+ This gem requires Rails 5, AMS 0.10, and Postgres 9.4+.
19
+
20
+ ## Installation
21
+
22
+ Add this line to your application's Gemfile:
23
+
24
+ gem 'active_model_serializers_pg'
25
+
26
+ And then execute:
27
+
28
+ bundle
29
+
30
+ Or install it yourself as:
31
+
32
+ gem install active_model_serializers_pg
33
+
34
+ ## Usage
35
+
36
+ You can enable in-Postgres serialization for everything by putting this in a Rails initializer:
37
+
38
+ ActiveModelSerializers.config.adapter = :json_api_pg
39
+
40
+ or use it more selectively in your controller actions by saying:
41
+
42
+ render json: @users, adapter: :json_api_pg
43
+
44
+ You could also turn it on for everything but then set `adapter: :json_api` for any actions where it doesn't work.
45
+
46
+ Note this gem also respects `ActiveModelSerializers.config.key_transform = :dash`, if you are using that.
47
+
48
+ ### Methods in Serializers and Models
49
+
50
+ If you are using methods to compute properties for your JSON responses
51
+ in your models or serializers, active\_model\_serializers\_pg will try to
52
+ discover a SQL version of this call by looking for a class method with
53
+ the same name and the suffix `__sql`. Here's an example:
54
+
55
+ ```ruby
56
+ class MyModel < ActiveRecord::Base
57
+ def full_name
58
+ "#{object.first_name} #{object.last_name}"
59
+ end
60
+
61
+ def self.full_name__sql
62
+ "first_name || ' ' || last_name"
63
+ end
64
+ end
65
+ ```
66
+
67
+ There is no instance of MyModel created so sql computed properties needs to be
68
+ a class method. Right now, this string is used as a SQL literal, so be sure to
69
+ *not* use untrusted values in the return value.
70
+
71
+ ## Developing
72
+
73
+ To work on active\_model\_serializers\_pg locally, follow these steps:
74
+
75
+ 1. Run `bundle install`, this will install (almost) all the development
76
+ dependencies.
77
+ 2. Run `gem install byebug` (not a declared dependency to not break CI).
78
+ 3. Run `bundle exec rake setup`, this will set up the `.env` file necessary to run
79
+ the tests and set up the database.
80
+ 4. Run `bundle exec rake db:create`, this will create the test database.
81
+ 5. Run `bundle exec rake db:migrate`, this will set up the database tables required
82
+ by the test.
83
+ 6. Run `bundle exec rake test:all` to run tests against all supported versions of Active Record (currently 5.0.x, 5.1.x, 5.2.x).
84
+ You can also say `BUNDLE_GEMFILE=gemfiles/Gemfile.activerecord-5.2.x bundle exec rspec spec` to run against a specific version (and select specific tests).
85
+
86
+ Commands for building/releasing/installing:
87
+
88
+ * `rake build`
89
+ * `rake install`
90
+ * `rake release`
91
+
92
+ ## Authors
93
+
94
+ Paul Jungwirth
95
+ [github](http://github.com/pjungwir)
96
+
97
+ Thanks to [Dan McClain](https://github.com/danmcclain) for writing the original postgres\_ext-serializers gem!
98
+
99
+ ## Versioning ##
100
+
101
+ This gem follows [Semantic Versioning](http://semver.org)
102
+
103
+ ## Want to help? ##
104
+
105
+ Please do! We are always looking to improve this gem.
106
+
107
+ ## Legal ##
108
+
109
+ Copyright &copy; 2019 Paul A. Jungwirth
110
+
111
+ [Licensed under the MIT license](http://www.opensource.org/licenses/mit-license.php)
data/Rakefile ADDED
@@ -0,0 +1,134 @@
1
+ require 'uri'
2
+ require 'bundler/gem_tasks'
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << 'lib'
7
+ t.libs << 'spec'
8
+ t.pattern = 'spec/**/*_spec.rb'
9
+ t.verbose = false
10
+ end
11
+
12
+ task :default => :test
13
+
14
+ task :setup do
15
+ if File.exist?('.env')
16
+ puts 'This will overwrite your existing .env file'
17
+ end
18
+
19
+ default_db_name = 'active_model_serializers_pg_test'
20
+ default_db_host = 'localhost'
21
+ default_db_port = '5432'
22
+
23
+ print "Enter your database name: [#{default_db_name}] "
24
+ db_name = STDIN.gets.chomp
25
+ print 'Enter your database user: [] '
26
+ db_user = STDIN.gets.chomp
27
+ print 'Enter your database password: [] '
28
+ db_password = STDIN.gets.chomp
29
+ print "Enter your database host: [#{default_db_host}] "
30
+ db_host = STDIN.gets.chomp
31
+ print "Enter your database port: [#{default_db_port}] "
32
+ db_port = STDIN.gets.chomp
33
+
34
+ db_name = default_db_name if db_name.empty?
35
+ db_password = ":#{URI.escape(db_password)}" unless db_password.empty?
36
+ db_ = default_db_host if db_host.empty?
37
+
38
+ db_user_at = db_user.empty? ? '' : '@'
39
+ db_port_colon = db_port.empty? ? '' : ':'
40
+
41
+ env_path = File.expand_path('./.env')
42
+ File.open(env_path, 'w') do |file|
43
+ file.puts "DATABASE_NAME=#{db_name}"
44
+ file.puts "DATABASE_PORT=#{db_port}"
45
+ file.puts "DATABASE_URL=\"postgres://#{db_user}#{db_password}#{db_user_at}#{db_host}#{db_port_colon}#{db_port}/#{db_name}\""
46
+ end
47
+
48
+ puts '.env file saved'
49
+ end
50
+
51
+ namespace :db do
52
+ task :load_db_settings do
53
+ require 'active_record'
54
+ unless ENV['DATABASE_URL']
55
+ require 'dotenv'
56
+ Dotenv.load
57
+ end
58
+ end
59
+
60
+ task :psql => :load_db_settings do
61
+ exec "psql -p #{ENV['DATABASE_PORT']} #{ENV['DATABASE_NAME']}"
62
+ end
63
+
64
+ task :drop => :load_db_settings do
65
+ %x{ dropdb -p #{ENV['DATABASE_PORT']} #{ENV['DATABASE_NAME']} }
66
+ end
67
+
68
+ task :create => :load_db_settings do
69
+ %x{ createdb -p #{ENV['DATABASE_PORT']} #{ENV['DATABASE_NAME']} }
70
+ end
71
+
72
+ task :migrate => :load_db_settings do
73
+ ActiveRecord::Base.establish_connection
74
+
75
+ ActiveRecord::Base.connection.create_table :people, force: true do |t|
76
+ t.string "first_name"
77
+ t.string "last_name"
78
+ t.datetime "created_at"
79
+ t.datetime "updated_at"
80
+ end
81
+
82
+ ActiveRecord::Base.connection.create_table :notes, force: true do |t|
83
+ t.string "name"
84
+ t.string "content"
85
+ t.datetime "created_at"
86
+ t.datetime "updated_at"
87
+ end
88
+
89
+ ActiveRecord::Base.connection.create_table :tags, force: true do |t|
90
+ t.integer "note_id"
91
+ t.string "name"
92
+ t.boolean "popular"
93
+ t.datetime "created_at"
94
+ t.datetime "updated_at"
95
+ end
96
+
97
+ ActiveRecord::Base.connection.create_table :offers, force: true do |t|
98
+ t.integer "created_by_id"
99
+ t.integer "reviewed_by_id"
100
+ t.datetime "created_at"
101
+ t.datetime "updated_at"
102
+ end
103
+
104
+ ActiveRecord::Base.connection.create_table :users, force: true do |t|
105
+ t.string "name"
106
+ t.string "mobile"
107
+ t.datetime "created_at"
108
+ t.datetime "updated_at"
109
+ end
110
+
111
+ ActiveRecord::Base.connection.create_table :addresses, force: true do |t|
112
+ t.string "district_name"
113
+ t.integer "user_id"
114
+ t.datetime "created_at"
115
+ t.datetime "updated_at"
116
+ end
117
+
118
+ puts 'Database migrated'
119
+ end
120
+ end
121
+
122
+ namespace :test do
123
+ desc 'Test against all supported ActiveRecord versions'
124
+ task :all do
125
+ # Escape current bundler environment
126
+ Bundler.with_clean_env do
127
+ # Currently only supports Active Record v5.0-v5.2
128
+ %w(5.0.x 5.1.x 5.2.x).each do |version|
129
+ sh "BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-#{version}' bundle install --quiet"
130
+ sh "BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-#{version}' bundle exec rspec spec"
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_model_serializers_pg/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_model_serializers_pg"
8
+ spec.version = ActiveModelSerializersPg::VERSION
9
+ spec.authors = ["Paul A. Jungwirth"]
10
+ spec.email = ["pj@illuminatedcomputing.com"]
11
+ spec.summary = %q{Harness the power of PostgreSQL when crafting JSON reponses}
12
+ spec.description = %q{}
13
+ spec.homepage = "https://github.com/pjungwir/active_model_serializers_pg"
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_runtime_dependency 'active_model_serializers', '~> 0.10.8'
22
+ spec.add_runtime_dependency 'activerecord', '~> 5.0'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.3'
25
+ spec.add_development_dependency 'actionpack', '> 4.0'
26
+ spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'rspec'
28
+ spec.add_development_dependency 'bourne', '~> 1.3.0'
29
+ spec.add_development_dependency 'database_cleaner'
30
+ spec.add_development_dependency 'dotenv'
31
+ if RUBY_PLATFORM =~ /java/
32
+ spec.add_development_dependency 'activerecord-jdbcpostgresql-adapter', '1.3.0.beta2'
33
+ else
34
+ spec.add_development_dependency 'pg', '~> 0.15'
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec :path => '..'
4
+
5
+ gem "activerecord", "~>5.0.0"
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec :path => '..'
4
+
5
+ gem "activerecord", "~>5.1.0"
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec :path => '..'
4
+
5
+ gem "activerecord", "~>5.2.0"