Help with endpoint for a non-ActiveRecord model without a database table
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 20/100
Hướng nghiên cứu
Bắt đầu với ví dụ về model CalendarEvent và Api::V3::CalendarEventsController, sau đó theo dõi cách JSONAPI::ResourceSerializer xử lý mảng ActiveModel đã được ánh xạ. Tái hiện response 204 No Content và kiểm tra hành vi của resource và serializer đối với các model không phải ActiveRecord. Hoàn tất khi xác định được một phương pháp serialization được hỗ trợ hoặc ghi lại các thay đổi tích hợp cần thiết.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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!
- Ngôn ngữ chính
- Ruby
- Star
- 2.3k
- Fork
- 546
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của JSONAPI-Resources/jsonapi-resources
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
-
ArgumentError with Rails 8.1 Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 50/100
JSONAPI-Resources/jsonapi-resources#1488 · 1 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 42/100
JSONAPI-Resources/jsonapi-resources#1479 · 2 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 30/100
JSONAPI-Resources/jsonapi-resources#1477 · 3 bình luận · 1 reaction ·
-
Fields filtering with include Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
Tất cả issue của JSONAPI-Resources/jsonapi-resources
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
riscv/riscv-unified-db#2626 ·
-
Component: GLib
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
ds-drift
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
we-promise/sure#3693 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
simp/pupmod-simp-simp#395 ·