Skip to content

Conversation

@seenanair
Copy link

@seenanair seenanair commented Jun 18, 2025

Closes sanger/General-Backlog-Items#569

All Submissions:

This pull request creates a gem of version (0.2.0) from sanger-jsonapi-resources master branch (https://github.com/sanger/jsonapi-resources) ensuring compatibility with Rails version 7 and 8 and Ruby versions (3.3 and 3.2).
This pull request includes several updates to improve compatibility with newer Rails versions, simplify the codebase, and enhance testing and database configurations. The most notable changes involve introducing a compatibility helper for deprecation warnings, updating database configurations, and refining test setups. Below is a categorized summary of the most important changes:

Compatibility Enhancements:

  • Added JSONAPI::CompatibilityHelper module to provide a version-safe method for issuing deprecation warnings across Rails versions. Updated all deprecation warnings in lib/jsonapi files to use this helper instead of ActiveSupport::Deprecation.warn.

       Problem : Throws error _NoMethodError: private method `warn'
       Reason : The visibility of ActiveSupport::Deprecation.warn has changed across Rails versions
    

Database Configuration Updates:

  • Updated test/fixtures/active_record.rb to replace length with limit for string columns
    Ref: (https://guides.rubyonrails.org/active_record_migrations.html#column-modifiers)

  • Added explicit foreign_key definitions for references to improve database schema compatibility.

     e.g :
         t.references :from, references: :thing
                    changed to
         t.references :to, foreign_key: { to_table: :things }
    
     Reason: Rails migrations support specifying different target tables via foreign_key: { to_table: … } . This syntax is fully  supported and stable in Rails 6.x, Rails 7.x, and Rails 8.x.
     Official documentation to confirm this pattern: ✅ Rails Guides (Rails 7.1.1)
     Solution: Define explicit foreign_key
    
  • Split t.references calls into separate lines for clarity and added explicit indexing for composite keys.

    e.g :
      create_table :posts_tags, force: true do |t|
          t.references :post, :tag, index: true
      end
                changed to
    create_table :posts_tags, force: true do |t|
        t.references :post, index:true
        t.references :tag, index:true
      end
    
     Reason: Currently, Rails does not automatically create composite indexes for multiple foreign keys, so explicit indexing is necessary.
     https://guides.rubyonrails.org/active_record_migrations.html#creating-a-new-table
     Solution : Define each t.references on its own line, and add explicit composite indexes for combined foreign keys as  recommended in the Rails Guides
    

CI Workflow Updates:

  • Simplified .github/workflows/ruby.yml and updating Ruby and Rails version matrices to focus on newer versions.

Test Setup Refinements:

  • Adjusted test/test_helper.rb to silence deprecation warnings conditionally based on Rails version and updated fixture paths for compatibility.

Codebase Simplifications:

  • Added require 'rails/generators' to lib/generators/jsonapi/controller_generator.rb and lib/generators/jsonapi/resource_generator.rb for better clarity and dependency management.

       Problem : Throws error _NameError: uninitialized constant Rails::Generators_
       Reason : Adding require 'rails/generators' ensures the Rails generator framework is loaded before this custom generator runs
    
  • Added a conditional check to skip processing in find_related_polymorphic_fragments if relation_position is nil.

       Problem - Throws error while loading Traction pools page
         Exception logged: undefined method '[]' for nil
        Internal Server Error: undefined method '[]' for nil //Project/jsonapi-resources/lib/jsonapi/active_relation_resource.rb:670:in 'block in JSONAPI::ActiveRelationResource.find_related_polymorphic_fragments'
        /Project/jsonapi-resources/lib/jsonapi/active_relation_resource.rb:651:in 'Array#each'
        //Project/jsonapi-resources/lib/jsonapi/active_relation_resource.rb:651:in 'JSONAPI::ActiveRelationResource.find_related_polymorphic_fragments'
    

@seenanair seenanair changed the title Y25-318 Publish gem as sanger-jsonapi-resources from master Y25-318 Publish sanger-jsonapi-resources gem from master Jun 18, 2025
@seenanair seenanair marked this pull request as draft June 18, 2025 10:50
@seenanair seenanair marked this pull request as ready for review June 18, 2025 12:33
Copy link

@stevieing stevieing left a comment

Choose a reason for hiding this comment

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

I don't understand most of it so as long as it works I'm good.

1 issue to resolve with ruby versions.

- '3.0'
- 3.1
- 3.2
- '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 👍

@seenanair
Copy link
Author

seenanair commented Jun 18, 2025

I don't understand most of it so as long as it works I'm good.

1 issue to resolve with ruby versions.

Thanks for the review. Most of the changes are just to make things work with the current Rails and Ruby versions—updating the schema (ActiveRecord), tweaking the test_helper, and changing method which is no longer supported in Rails 8 (ActiveSupport::Deprecation.warn)

@seenanair seenanair requested a review from stevieing June 18, 2025 14:09
@seenanair seenanair merged commit 98a6958 into master Jun 30, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Y25-318 - As PSD we need to stabilise the JSON API resources repository and release it as a gem to be used by Sequencescape and Traction Service

2 participants