Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine implicit relationships #1446

Open
wants to merge 30 commits into
base: v0-11-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
be970f4
deps: Add MySQL to CI workflow (#1398)
bf4 Jan 25, 2023
896459c
fix: Reliably quote columns/tables (#1400)
bf4 Feb 2, 2023
1da2cb7
Add docker testing setup (#1403)
lgebhardt Mar 3, 2023
f2bcba9
fix: test the adapter-specific query ordering (#1402)
bf4 Mar 10, 2023
01ba9a0
V0.11 refactor resource classes to modules (#1406)
lgebhardt Sep 19, 2023
c70bd4c
Add frozen_string_literal: true
lgebhardt Sep 19, 2023
2f69096
Restore missing requires
lgebhardt Sep 19, 2023
ac46d89
fix: warnings (#1401)
bf4 Sep 19, 2023
8932a6d
Restore `use_related_resource_records_for_joins` for v0_10 (#1412)
lgebhardt Sep 26, 2023
bdce601
Bump jsonapi-resources to 0.11.0.beta2
lgebhardt Sep 26, 2023
81e4ecf
fix: more reliable check of module is included (#1418)
bf4 Oct 27, 2023
26d280a
Fix tests for V0.11 and Rails 7.1 (#1420)
lgebhardt Nov 1, 2023
1bdacf1
Namespace references to Rails using `::Rails` to avoid conflicts with…
lgebhardt Nov 16, 2023
ef0551d
chore: remove sorted_set dependency (#1423)
bf4 Jan 10, 2024
040f980
Make SortedSet for identity arrays optional (#1427)
lgebhardt Jan 11, 2024
cd1a529
Store the resource_klass and id in an array for efficiency (#1428)
lgebhardt Jan 13, 2024
0bbbc0b
Rework ResourceIdentity <=> operator (#1430)
lgebhardt Jan 16, 2024
2668b6b
V0 11 dev performance (#1431)
lgebhardt Jan 16, 2024
9f436b5
fix: allow multiple resource relation retrieval methods (#1425)
bf4 Jan 17, 2024
76ef777
refactor: separate polymorphic functions (#1433)
bf4 Jan 19, 2024
f193a35
feat: teach JR in tests to parse the response (#1437)
bf4 Jan 22, 2024
51c9592
chore: address deprecations (#1436)
bf4 Jan 22, 2024
311b1fe
fix: format model polymorphic type from resource object type (#1435)
bf4 Jan 22, 2024
e4c9707
fix: railtie to use correct load hook (#1438)
bf4 Jan 24, 2024
ae9017b
fix: more flexible polymorphic types lookup (#1434)
bf4 Jan 25, 2024
c529c23
Update the testing matrix (#1442)
lgebhardt Jan 26, 2024
f75acdb
Polymorphic types override per relationship (#1440)
lgebhardt Jan 26, 2024
07888a9
Fix issue with relationship sorts due to missing join_manager (#1443)
lgebhardt Jan 26, 2024
cccd332
Define relationships more consistently
lgebhardt Jan 29, 2024
7084ffa
Rework implicit relationships
lgebhardt Jan 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .docker/ruby_versions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3.0.6
3.1.4
3.2.3
3.3.0
3 changes: 3 additions & 0 deletions .docker/scripts/test_all
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

xargs -L 1 ./test_ruby < ruby_versions.txt
10 changes: 10 additions & 0 deletions .docker/scripts/test_mysql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

cd /src
bundle update;

RUBY_VERSION=$(ruby -v);

echo "Testing With MySQL and Ruby $RUBY_VERSION";
export DATABASE_URL="mysql2://test:password@mysql:3306/test"
bundle exec rake test;
10 changes: 10 additions & 0 deletions .docker/scripts/test_postgresql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

cd /src
bundle update;

RUBY_VERSION=$(ruby -v);

echo "Testing With PostgreSQL and Ruby $RUBY_VERSION";
export DATABASE_URL="postgresql://postgres:password@postgres:5432/test"
bundle exec rake test;
7 changes: 7 additions & 0 deletions .docker/scripts/test_ruby
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

rbenv global $1;

../test_postgresql
../test_mysql
../test_sqlite
10 changes: 10 additions & 0 deletions .docker/scripts/test_sqlite
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

cd /src
bundle update;

RUBY_VERSION=$(ruby -v);

echo "Testing With SQLite and Ruby $RUBY_VERSION";
export DATABASE_URL="sqlite3:test_db"
bundle exec rake test;
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Set update schedule for GitHub Actions

version: 2
updates:

- package-ecosystem: "github-actions"
directory: "/"
schedule:
# Check for updates to GitHub Actions every weekday
interval: "daily"
61 changes: 25 additions & 36 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@ name: CI

on:
push:
branches: [ 'master', 'release-0-8', 'release-0-9', 'release-0-10' ]
branches: [ 'master', 'release-0-8', 'release-0-9', 'release-0-10', 'v0-11-dev' ]
pull_request:
branches: ['**']

jobs:
tests:
runs-on: ubuntu-latest
services:
mysql:
image: mysql
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test
ports:
- 3306:3306
options: >-
--health-cmd "mysqladmin ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
postgres:
image: postgres
env:
Expand All @@ -26,43 +38,21 @@ jobs:
fail-fast: false
matrix:
ruby:
- 2.6
- 2.7
- '3.3'
- '3.2'
- '3.1'
- '3.0'
- 3.1
- 3.2
rails:
- 7.0.4
- 6.1.7
- 6.0.6
- 5.2.8.1
- 5.1.7
- '7.1'
- '7.0'
- '6.1'
database_url:
- postgresql://postgres:password@localhost:5432/test
- sqlite3:test_db
exclude:
- ruby: 3.2
rails: 6.0.6
- ruby: 3.2
rails: 5.2.8.1
- ruby: 3.2
rails: 5.1.7
- ruby: 3.1
rails: 6.0.6
- ruby: 3.1
rails: 5.2.8.1
- ruby: 3.1
rails: 5.1.7
- ruby: '3.0'
rails: 6.0.6
- ruby: '3.0'
rails: 5.2.8.1
- ruby: '3.0'
rails: 5.1.7
- ruby: 2.6
rails: 7.0.4
- database_url: postgresql://postgres:password@localhost:5432/test
rails: 5.1.7
- postgresql://postgres:password@localhost:5432/test
- mysql2://root:[email protected]:3306/test
# exclude:
# - ruby: '3.1'
# rails: '6.0'
env:
RAILS_VERSION: ${{ matrix.rails }}
DATABASE_URL: ${{ matrix.database_url }}
Expand All @@ -73,7 +63,6 @@ jobs:
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- name: Install dependencies
run: bundle install --jobs 4 --retry 3
bundler-cache: true
- name: Run tests
run: bundle exec rake test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ test_db
test_db-journal
.idea
*.iml
*.override.yml
36 changes: 36 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
FROM buildpack-deps:bullseye

# Install rbenv
RUN git clone https://github.com/sstephenson/rbenv.git /root/.rbenv
RUN git clone https://github.com/sstephenson/ruby-build.git /root/.rbenv/plugins/ruby-build
RUN /root/.rbenv/plugins/ruby-build/install.sh
ENV PATH /root/.rbenv/bin:/root/.rbenv/shims:$PATH
RUN echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh # or /etc/profile
RUN echo 'eval "$(rbenv init -)"' >> .bashrc

# Install supported ruby versions
RUN echo 'gem: --no-document' >> ~/.gemrc

COPY .docker/ruby_versions.txt /
RUN xargs -I % sh -c 'rbenv install %; rbenv global %; gem install bundler' < ruby_versions.txt
RUN rbenv rehash

# COPY just enough to bundle. This allows for most code changes without needing to reinstall all gems
RUN mkdir src
COPY Gemfile jsonapi-resources.gemspec Rakefile ./src/
COPY lib/jsonapi/resources/version.rb ./src/lib/jsonapi/resources/
# This will run bundle install for each ruby version and leave the global version set as the last one.
# So we can control the default ruby version with the order in the ruby_versions.txt file, with last being the default
RUN xargs -I % sh -c 'cd src; rbenv global %; bundle install' < /ruby_versions.txt

# Scripts
COPY .docker/scripts/* /
RUN chmod +x /test_*

# COPY in the rest of the project
COPY lib/ ./src/lib
COPY locales/ ./src/locales
COPY test/ ./src/test
RUN ls -la

CMD ["/test_all"]
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ version = ENV['RAILS_VERSION'] || 'default'

platforms :ruby do
gem 'pg'
gem 'mysql2'

if version.start_with?('4.2', '5.0')
gem 'sqlite3', '~> 1.3.13'
Expand All @@ -26,4 +27,4 @@ when 'default'
gem 'railties', '>= 6.0'
else
gem 'railties', "~> #{version}"
end
end
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2014-2021 Cerebris Corporation
Copyright (c) 2014-2023 Cerebris Corporation

MIT License

Expand Down
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,43 @@ gem install jsonapi-resources

**For further usage see the [v0.10 alpha Guide](http://jsonapi-resources.com/v0.10/guide/)**

## Development

There is a docker compose setup that can be used for testing your code against the supported versions of ruby and
against PostgreSQL, MYSQL, and SQLite databases.

First build the docker image:
```bash
docker compose build
```
Be sure to rebuild after making code changes. It should be fast after the initial build.

### Running the tests

The default command will run everything (it may take a while):
```bash
docker compose run tests
```

To test just one database against the latest ruby:
```bash
docker compose run tests ./test_mysql
docker compose run tests ./test_postgresql
docker compose run tests ./test_sqlite
```

To test a version of ruby against all databases:
```bash
docker compose run tests ./test_ruby 2.7.7
```

The tests by default run against the latest rails version. To override that you can set the RAILS_VERSION environment
variable:

```bash
docker compose run -e RAILS_VERSION=6.1.1 tests ./test_postgresql
```

## Contributing

1. Submit an issue describing any new features you wish it add or the bug you intend to fix
Expand Down Expand Up @@ -73,4 +110,4 @@ and **paste the content into the issue description or attach as a file**:

## License

Copyright 2014-2021 Cerebris Corporation. MIT License (see LICENSE for details).
Copyright 2014-2023 Cerebris Corporation. MIT License (see LICENSE for details).
49 changes: 49 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
version: "3.9"

networks:
back:

services:
postgres:
image: postgres:latest
ports:
- "5432"
networks:
back:
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_DB=test
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
interval: 2s
retries: 15

mysql:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
ports:
- 3306
networks:
- back
environment:
- MYSQL_DATABASE=test
- MYSQL_USER=test
- MYSQL_PASSWORD=password
- MYSQL_ROOT_PASSWORD=password
healthcheck:
test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
interval: 2s
retries: 15

tests:
image: jr_tests:latest
build:
context: .
dockerfile: Dockerfile
networks:
- back
depends_on:
postgres:
condition: service_healthy
mysql:
condition: service_healthy
3 changes: 3 additions & 0 deletions jsonapi-resources.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'pry'
spec.add_development_dependency 'concurrent-ruby-ext'
spec.add_development_dependency 'database_cleaner'
spec.add_development_dependency 'hashie'
spec.add_development_dependency 'sorted_set'
spec.add_development_dependency 'memory_profiler'
spec.add_dependency 'activerecord', '>= 5.1'
spec.add_dependency 'railties', '>= 5.1'
spec.add_dependency 'concurrent-ruby'
Expand Down
6 changes: 4 additions & 2 deletions lib/generators/jsonapi/controller_generator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Jsonapi
class ControllerGenerator < Rails::Generators::NamedBase
# frozen_string_literal: true

module JSONAPI
class ControllerGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)

def create_resource
Expand Down
4 changes: 3 additions & 1 deletion lib/generators/jsonapi/resource_generator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

module Jsonapi
class ResourceGenerator < Rails::Generators::NamedBase
class ResourceGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)

def create_resource
Expand Down
12 changes: 9 additions & 3 deletions lib/jsonapi-resources.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# frozen_string_literal: true

require 'jsonapi/resources/railtie'
require 'jsonapi/utils/polymorphic_types_lookup'
require 'jsonapi/naive_cache'
require 'jsonapi/compiled_json'
require 'jsonapi/basic_resource'
require 'jsonapi/active_relation_resource'
require 'jsonapi/relation_retrieval'
require 'jsonapi/active_relation_retrieval'
require 'jsonapi/active_relation_retrieval_v09'
require 'jsonapi/active_relation_retrieval_v10'
require 'jsonapi/resource_common'
require 'jsonapi/resource'
require 'jsonapi/simple_resource'
require 'jsonapi/cached_response_fragment'
require 'jsonapi/response_document'
require 'jsonapi/acts_as_resource_controller'
if Rails::VERSION::MAJOR >= 6
if ::Rails::VERSION::MAJOR >= 6
ActiveSupport.on_load(:action_controller_base) do
require 'jsonapi/resource_controller'
end
Expand Down Expand Up @@ -37,6 +42,7 @@
require 'jsonapi/link_builder'
require 'jsonapi/active_relation/adapters/join_left_active_record_adapter'
require 'jsonapi/active_relation/join_manager'
require 'jsonapi/active_relation/join_manager_v10'
require 'jsonapi/resource_identity'
require 'jsonapi/resource_fragment'
require 'jsonapi/resource_tree'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module JSONAPI
module ActiveRelation
module Adapters
Expand All @@ -7,7 +9,7 @@ module JoinLeftActiveRecordAdapter
# example Post.joins(:comments).joins_left(comments: :author) will join the comments table twice,
# once inner and once left in 5.2, but only as inner in earlier versions.
def joins_left(*columns)
if Rails::VERSION::MAJOR >= 6 || (Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2)
if ::Rails::VERSION::MAJOR >= 6 || (::Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2)
left_joins(columns)
else
join_dependency = ActiveRecord::Associations::JoinDependency.new(self, columns, [])
Expand All @@ -23,4 +25,4 @@ def joins_left(*columns)
end
end
end
end
end
Loading
Loading