JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Delegate is actually an alias

Aperta
#1,089 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Ruby
Stelle
2.3k
Fork
546
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia da JSONAPI::Resource e dalla gestione della delega dell'opzione attributes, quindi confronta il suo comportamento con gli esempi di delega di Ruby e Rails nell'issue. Il lavoro è completato quando l'opzione delegate inoltra il metodo all'oggetto delegato come descritto, oppure quando il suo comportamento simile a un alias è documentato chiaramente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
rails, ruby
Ambito
api
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.