Add tests for Issue #1467: ActiveModel resources support#20
Merged
Conversation
…port Issue JSONAPI-Resources#1467 reported that ActiveModel-based models fail with 'undefined method where' errors in v0.10.7+. Root cause: - The default JSONAPI::Resource inherits from ActiveRelationResource - ActiveRelationResource expects ActiveRecord query methods (where, order, etc.) - ActiveModel::Model doesn't provide these methods Solution: - ActiveModel-based models should use JSONAPI::BasicResource - BasicResource doesn't assume ActiveRecord and works with any model Tests added: 1. active_model_resource_test.rb - Shows BasicResource works with ActiveModel 2. active_model_broken_test.rb - Demonstrates the problem with ActiveRelationResource The tests confirm that using BasicResource is the correct approach for non-ActiveRecord models. Related: https://github.com/cerebris/jsonapi-resources/issues/1467
ActiveModel::Attributes is only available in Rails 5.2+. For Rails 5.1 compatibility, use attr_accessor instead. Changes: - Remove 'include ActiveModel::Attributes' - Replace 'attribute :id, :integer' with 'attr_accessor :id' - Add explicit initialize method This ensures the test fixtures work with Rails 5.1.7 and above.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds comprehensive tests for Issue JSONAPI-Resources#1467 and documents the correct approach for using ActiveModel-based models with jsonapi-resources.
Problem
Issue JSONAPI-Resources#1467 reported that ActiveModel-based models fail with
undefined method 'where'errors in v0.10.7+. Users were confused about why their non-ActiveRecord models stopped working.Root Cause
The default
JSONAPI::Resourceinherits fromActiveRelationResource, which expects ActiveRecord query methods like:where,order,limit,offsetActiveModel::Model doesn't provide these methods, causing failures.
Solution
Use
JSONAPI::BasicResourcefor non-ActiveRecord models:Tests Added
active_model_resource_test.rb - Demonstrates BasicResource working correctly with ActiveModel
active_model_broken_test.rb - Shows the problem with ActiveRelationResource
Testing
Documentation
This PR serves as living documentation showing:
Related
🤖 Generated with Claude Code