Help with endpoint for a non-ActiveRecord model without a database table
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 20/100
Research direction
Start with the CalendarEvent model example and Api::V3::CalendarEventsController, then trace how JSONAPI::ResourceSerializer handles the mapped ActiveModel array. Reproduce the 204 No Content response and inspect the resource and serializer behavior for non-ActiveRecord models. Done means identifying a supported serialization approach or documenting the required integration changes.
Written by the indexing model from the issue text.
Description
I have a non-ActiveRercord model without a database table I am trying to create an API endpoint for. The data for this model is populated dynamically with custom SQL and provides generic relations to real ActiveRecords.
Because of all the magic happening with JSONAPI Resources I am having a hard time serializing the array of ActiveModels successfully. Am wondering what the best approach for this might be? I've tried some monkey patching without success and would appreciate guidance
model example
class CalendarEvent
include ActiveModel::Model
attr_accessor(
:id,
:title,
# etc etc
)
def self.modelize(record)
return CalendarEvent.new(record)
end
def self.records(person_id, start_date, end_date, options = {})
sql = "select 'some custom sql' from various_table where serious_magic_happens"
records = ActiveRecord::Base.connection.execute(sql)
records = records.map{ |r| self.modelize(r) } # returns an Array of CalendarEvent::Class
end
end
controller attempt
class Api::V3::CalendarEventsController < Api::V3::ApiController
def index
records = CalendarEvent.records(1686, '2017-07-01', '2017-07-31')
resources = records.map { |r| Api::V3::CalendarEventResource.new(r, nil) }
JSONAPI::ResourceSerializer.new(Api::V3::CalendarEventResource).serialize_to_hash(resources)
end
end
The above controller returns a 204 No Content even though I am passing serialize_to_hash an array ... am I misunderstanding how this should work ? Or is there a better way?
Thanks!
- Dominant language
- Ruby
- Stars
- 2.3k
- Forks
- 546
- PR merge metrics
- No merged PRs in 30d
Getting set up
We have not checked this project's setup files yet. Start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from JSONAPI-Resources/jsonapi-resources
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
-
Difficulty 3/5 1-2 days Newbie friendliness 50/100
JSONAPI-Resources/jsonapi-resources#1488 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
JSONAPI-Resources/jsonapi-resources#1479 · 2 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 30/100
JSONAPI-Resources/jsonapi-resources#1477 · 3 comments · 1 reaction ·
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
All issues in JSONAPI-Resources/jsonapi-resources
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
solana-foundation/pay-kit#341 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
TheOdinProject/curriculum#31427 · 3 comments ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
cyclestreets/cyclescape#1107 · 1 reaction ·
-
Difficulty 1/5 Under an hour Newbie friendliness 95/100
Maintainers usually reply within 1 day