Skip to content

Y25-318 Publish sanger-jsonapi-resources gem from master #4

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
58 changes: 8 additions & 50 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,26 @@ name: CI

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

jobs:
tests:
runs-on: ubuntu-latest
services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: password
POSTGRES_DB: test
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
strategy:
fail-fast: false
matrix:
ruby:
- 2.6
- 2.7
- '3.0'
- 3.1
- 3.2
- '3.4'
- '3.3'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Traction Service is on ruby 3.4.4 so maybe worth adding ruby 3.4?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot! Done 👍

- '3.2'
rails:
- 7.0.4
- 6.1.7
- 6.0.6
- 5.2.8.1
- 5.1.7
- '7.1'
- '7.0'
- '8.0.2'
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
env:
RAILS_VERSION: ${{ matrix.rails }}
DATABASE_URL: ${{ matrix.database_url }}
Expand All @@ -73,7 +32,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
11 changes: 4 additions & 7 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ version = ENV['RAILS_VERSION'] || 'default'

platforms :ruby do
gem 'pg'

if version.start_with?('4.2', '5.0')
gem 'sqlite3', '~> 1.3.13'
else
gem 'sqlite3', '~> 1.4'
end
gem 'mysql2'
gem 'sqlite3'
gem 'csv'
end

case version
Expand All @@ -26,4 +23,4 @@ when 'default'
gem 'railties', '>= 6.0'
else
gem 'railties', "~> #{version}"
end
end
10 changes: 5 additions & 5 deletions jsonapi-resources.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'jsonapi/resources/version'

Gem::Specification.new do |spec|
spec.name = 'jsonapi-resources'
spec.name = 'sanger-jsonapi-resources'
spec.version = JSONAPI::Resources::VERSION
spec.authors = ['Dan Gebhardt', 'Larry Gebhardt']
spec.email = ['dan@cerebris.com', 'larry@cerebris.com']
spec.authors = ['PSD Team - Wellcome Trust Sanger Institute']
spec.email = ['psd@sanger.ac.uk']
spec.summary = 'Easily support JSON API in Rails.'
spec.description = 'A resource-centric approach to implementing the controllers, routes, and serializers needed to support the JSON API spec.'
spec.homepage = 'https://github.com/cerebris/jsonapi-resources'
spec.description = 'Forked from jsonapi-resources. A resource-centric approach to implementing the controllers, routes, and serializers needed to support the JSON API spec.'
spec.homepage = 'https://github.com/sanger/jsonapi-resources'
spec.license = 'MIT'

spec.files = Dir.glob("{bin,lib}/**/*") + %w(LICENSE.txt README.md)
Expand Down
1 change: 1 addition & 0 deletions lib/generators/jsonapi/controller_generator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'rails/generators'
module Jsonapi
class ControllerGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
Expand Down
1 change: 1 addition & 0 deletions lib/generators/jsonapi/resource_generator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'rails/generators'
module Jsonapi
class ResourceGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
Expand Down
12 changes: 8 additions & 4 deletions lib/jsonapi/active_relation_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -666,10 +666,14 @@ def find_related_polymorphic_fragments(source_fragments, relationship, options,
end

relation_position = relation_positions[row[2].downcase.pluralize]
model_fields = relation_position[:model_fields]
cache_field = relation_position[:cache_field]
cache_offset = relation_position[:cache_offset]
field_offset = relation_position[:field_offset]
if relation_position
model_fields = relation_position[:model_fields]
cache_field = relation_position[:cache_field]
cache_offset = relation_position[:cache_offset]
field_offset = relation_position[:field_offset]
else
next # Skip processing if relation_position is nil
end

if cache_field
related_fragments[rid].cache = cast_to_attribute_type(row[cache_offset], cache_field[:type])
Expand Down
10 changes: 5 additions & 5 deletions lib/jsonapi/acts_as_resource_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'csv'

require_relative 'compatibility_helper'
module JSONAPI
module ActsAsResourceController
MEDIA_TYPE_MATCHER = /.+".+"[^,]*|[^,]+/
Expand Down Expand Up @@ -63,16 +63,16 @@ def index_related_resources

def get_related_resource
# :nocov:
ActiveSupport::Deprecation.warn "In #{self.class.name} you exposed a `get_related_resource`"\
" action. Please use `show_related_resource` instead."
JSONAPI::CompatibilityHelper.deprecation_warn("In #{self.class.name} you exposed a `get_related_resource`"\
" action. Please use `show_related_resource` instead.")
show_related_resource
# :nocov:
end

def get_related_resources
# :nocov:
ActiveSupport::Deprecation.warn "In #{self.class.name} you exposed a `get_related_resources`"\
" action. Please use `index_related_resources` instead."
JSONAPI::CompatibilityHelper.deprecation_warn("In #{self.class.name} you exposed a `get_related_resources`"\
" action. Please use `index_related_resources` instead.")
index_related_resources
# :nocov:
end
Expand Down
9 changes: 5 additions & 4 deletions lib/jsonapi/basic_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'jsonapi/callbacks'
require 'jsonapi/configuration'

require_relative 'compatibility_helper'
module JSONAPI
class BasicResource
include Callbacks
Expand Down Expand Up @@ -547,7 +547,7 @@ def attribute(attribute_name, options = {})
check_reserved_attribute_name(attr)

if (attr == :id) && (options[:format].nil?)
ActiveSupport::Deprecation.warn('Id without format is no longer supported. Please remove ids from attributes, or specify a format.')
JSONAPI::CompatibilityHelper.deprecation_warn('Id without format is deprecated. Please specify a format for the id attribute.')
end

check_duplicate_attribute_name(attr) if options[:format].nil?
Expand Down Expand Up @@ -609,11 +609,12 @@ def has_one(*attrs)
end

def belongs_to(*attrs)
ActiveSupport::Deprecation.warn "In #{name} you exposed a `has_one` relationship "\

JSONAPI::CompatibilityHelper.deprecation_warn( "In #{name} you exposed a `has_one` relationship "\
" using the `belongs_to` class method. We think `has_one`" \
" is more appropriate. If you know what you're doing," \
" and don't want to see this warning again, override the" \
" `belongs_to` class method on your resource."
" `belongs_to` class method on your resource.")
_add_relationship(Relationship::ToOne, *attrs)
end

Expand Down
29 changes: 29 additions & 0 deletions lib/jsonapi/compatibility_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

# JSONAPI::CompatibilityHelper
#
# This module provides a version-safe method for issuing deprecation warnings
# that works across multiple versions of Rails (7.x, 8.x, etc).
#
# Usage:
# JSONAPI::CompatibilityHelper.deprecation_warn("Your deprecation message")
#
# The method will use the public `warn` method if available, otherwise it will
# use `send(:warn, ...)` to maintain compatibility with Rails 8+ where `warn`
# is private.
#
# Example:
# JSONAPI::CompatibilityHelper.deprecation_warn("This feature is deprecated.")

module JSONAPI
module CompatibilityHelper
def deprecation_warn(message)
if ActiveSupport::Deprecation.respond_to?(:warn) && ActiveSupport::Deprecation.public_method_defined?(:warn)
ActiveSupport::Deprecation.warn(message)
else
ActiveSupport::Deprecation.send(:warn, message)
end
end
module_function :deprecation_warn
end
end
10 changes: 5 additions & 5 deletions lib/jsonapi/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'jsonapi/formatter'
require 'jsonapi/processor'
require 'concurrent'

require_relative 'compatibility_helper'
module JSONAPI
class Configuration
attr_reader :json_key_format,
Expand Down Expand Up @@ -227,7 +227,7 @@ def exception_class_allowed?(e)
end

def default_processor_klass=(default_processor_klass)
ActiveSupport::Deprecation.warn('`default_processor_klass` has been replaced by `default_processor_klass_name`.')
JSONAPI::CompatibilityHelper.deprecation_warn('`default_processor_klass` has been replaced by `default_processor_klass_name`.')
@default_processor_klass = default_processor_klass
end

Expand All @@ -241,18 +241,18 @@ def default_processor_klass_name=(default_processor_klass_name)
end

def allow_include=(allow_include)
ActiveSupport::Deprecation.warn('`allow_include` has been replaced by `default_allow_include_to_one` and `default_allow_include_to_many` options.')
JSONAPI::CompatibilityHelper.deprecation_warn('`allow_include` has been replaced by `default_allow_include_to_one` and `default_allow_include_to_many` options.')
@default_allow_include_to_one = allow_include
@default_allow_include_to_many = allow_include
end

def whitelist_all_exceptions=(allow_all_exceptions)
ActiveSupport::Deprecation.warn('`whitelist_all_exceptions` has been replaced by `allow_all_exceptions`')
JSONAPI::CompatibilityHelper.deprecation_warn('`whitelist_all_exceptions` has been replaced by `allow_all_exceptions`')
@allow_all_exceptions = allow_all_exceptions
end

def exception_class_whitelist=(exception_class_allowlist)
ActiveSupport::Deprecation.warn('`exception_class_whitelist` has been replaced by `exception_class_allowlist`')
JSONAPI::CompatibilityHelper.deprecation_warn('`exception_class_whitelist` has been replaced by `exception_class_allowlist`')
@exception_class_allowlist = exception_class_allowlist
end

Expand Down
4 changes: 2 additions & 2 deletions lib/jsonapi/relationship.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

require_relative 'compatibility_helper'
module JSONAPI
class Relationship
attr_reader :acts_as_set, :foreign_key, :options, :name,
Expand All @@ -21,7 +21,7 @@ def initialize(name, options = {})
@polymorphic = options.fetch(:polymorphic, false) == true
@polymorphic_types = options[:polymorphic_types]
if options[:polymorphic_relations]
ActiveSupport::Deprecation.warn('Use polymorphic_types instead of polymorphic_relations')
JSONAPI::CompatibilityHelper.deprecation_warn('Use polymorphic_types instead of polymorphic_relations')
@polymorphic_types ||= options[:polymorphic_relations]
end

Expand Down
2 changes: 1 addition & 1 deletion lib/jsonapi/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ module JSONAPI
class Resource < ActiveRelationResource
root_resource
end
end
end
2 changes: 1 addition & 1 deletion lib/jsonapi/resources/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module JSONAPI
module Resources
VERSION = '0.11.0.beta1'
VERSION = '0.2.0'
end
end
7 changes: 7 additions & 0 deletions lib/sanger-jsonapi-resources.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# As we are packaging 'sanger-jsonapi-resources' as a separate gem, RubyGems expects
# the main file to be 'lib/sanger-jsonapi-resources.rb' to match the gem name.
# Without this file, requiring the gem or Rails autoloading would fail, even if the internal code is unchanged.
# This file exists to ensure compatibility with RubyGems and Bundler.
# The easiest solution is to use this wrapper file, which simply requires the original 'jsonapi-resources' code,
# so all internal references and modules remain unchanged and compatible.
require_relative 'jsonapi-resources'
15 changes: 9 additions & 6 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
end

create_table :posts, force: true do |t|
t.string :title, length: 255
t.string :title, limit: 255
t.text :body
t.integer :author_id
t.integer :parent_post_id
Expand Down Expand Up @@ -85,17 +85,20 @@
end

create_table :posts_tags, force: true do |t|
t.references :post, :tag, index: true
t.references :post, index:true
t.references :tag, index:true
end
add_index :posts_tags, [:post_id, :tag_id], unique: true

create_table :special_post_tags, force: true do |t|
t.references :post, :tag, index: true
t.references :post, index: true
t.references :tag, index: true
end
add_index :special_post_tags, [:post_id, :tag_id], unique: true

create_table :comments_tags, force: true do |t|
t.references :comment, :tag, index: true
t.references :comment, index: true
t.references :tag, index: true
end

create_table :iso_currencies, id: false, force: true do |t|
Expand Down Expand Up @@ -324,8 +327,8 @@

create_table :related_things, force: true do |t|
t.string :name
t.references :from, references: :thing
t.references :to, references: :thing
t.references :from, foreign_key: { to_table: :things }
t.references :to, foreign_key: { to_table: :things }

t.timestamps null: false
end
Expand Down
Loading