JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Help with endpoint for a non-ActiveRecord model without a database table

Offen
#1,092 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Ruby
Sterne
2.3k
Forks
546
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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!

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem Beispiel des CalendarEvent-Modells und Api::V3::CalendarEventsController und verfolge anschließend, wie JSONAPI::ResourceSerializer das zugeordnete ActiveModel-Array verarbeitet. Reproduziere die 204 No Content-Antwort und untersuche das Verhalten von Resource und Serializer bei Nicht-ActiveRecord-Modellen. Als erledigt gilt die Identifizierung eines unterstützten Serialisierungsansatzes oder die Dokumentation der erforderlichen Integrationsänderungen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
rails, ruby
Bereich
api, backend
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
20/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.