JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Delegate is actually an alias

Offen
#1,089 0 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

When working with the gem and trying to look at how to hide underlying models I came across the delegate option which is available on a resource.

Using this option I expected the same result as with the definitions provided in both ruby and rails. When I delegate a method to something, it calls the method being delegated on that something.

An example:

class Bar
  attr_accessor :name

  def initialize
    @name = "Test"
  end
end

class Foo
  extend Forwardable
  attr_accessor :bar

  def initialize
    @bar = Bar.new
  end

  def_delegator :@bar, :name
end

Foo.new.name            # => "Test"
foo = Foo.new
foo.bar.name = "Demo"
foo.name                # => "Demo"

This passes the delegate method along to the object @bar.

Now looking at the way it is defined in JSONAPI::Resource we have the following option:

class Bar
  attr_accessor :name

  def initialize
    @name = "Test"
  end
end

class Foo
  attr_accessor :bar

  def initialize
    @bar = Bar.new
  end
end

class FooResource < JSONAPI::Resource
  attributes :name, delegate: :bar
end

FooResource.new(Foo.new, {}).name  # => <Bar:0x007fd288c69708 @name="Test">

As you can see when calling name on the resource it gives me back the object Bar instead of calling the method name on the Bar object. This is more in line with how alias works instead of how delegate works.

It might be an idea to make this clearer in the documentation to prevent any confusion, and it maybe an idea in the future to modify this behaviour to be the same as how ruby and rails handle this.

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 bei JSONAPI::Resource und der Delegate-Behandlung der Option attributes und vergleiche anschließend ihr Verhalten mit den Ruby- und Rails-Beispielen zur Delegation im Issue. Als erledigt gilt die Aufgabe, wenn die Delegate-Option entweder die Methode wie beschrieben an das delegierte Objekt weiterleitet oder ihr aliasähnliches Verhalten eindeutig dokumentiert ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
rails, ruby
Bereich
api
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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