GithubHelp home page GithubHelp logo

Use with Inherited Resources about draper HOT 7 CLOSED

drapergem avatar drapergem commented on July 20, 2024
Use with Inherited Resources

from draper.

Comments (7)

angelim avatar angelim commented on July 20, 2024

Hi @jonlhouse,
don't you think it would be simpler to use end_of_association_chain.decorate and super.decorate ?

from draper.

jbbarth avatar jbbarth commented on July 20, 2024

Actually end_of_association_chain.decorate is not enough since InheritedResources uses the real class as the resource, not the decorator class. I'm not sure it would be a good idea to override InheritedResources' resource_name, it might be hard to extract a default behaviour and keep the ability to override resource name in a specific controller. But I'm not a specialist of InheritedResources internals...

Just FYI, @jonlhouse's method works great, but I wanted to set variables the same way InheritedResources does (so that I have @projects automatically set by ProjectsController#collection for instance). I changed a bit collection and resource definitions to match InheritedResources internals in the code above :

    def collection
      get_collection_ivar || set_collection_ivar(decorate_resource_or_collection(end_of_association_chain))
    end

    def resource
      get_resource_ivar || set_resource_ivar(decorate_resource_or_collection(super))
    end

Hope this helps, thanks for Draper anyway, it's really awesome ! :)

from draper.

angelim avatar angelim commented on July 20, 2024

The expected behavior for active_record_relation.decorate is to return a DecoratedEnumerableProxy for that collection. Maybe there's stil an issue pending related to Decorator loading, preventing the correct use of model.decorate and relation.decorate. I didn't understand what you were saying about overriding resource_name.
I recommended that approach because thats exactly what I'm successfully using in one of my projects, however I've changed draper a bit on my fork. Even so, I don't see why you wouldn't be able use it, given the Decorator class was previously loaded and the decorate accessors methods are created in the target model class. It's certainly cleaner.

from draper.

jbbarth avatar jbbarth commented on July 20, 2024

You're right, I didn't understand at first what you said.. You can safely ignore the first part of my comment, sorry :)

EDIT: s/write/right/

from draper.

jonlhouse avatar jonlhouse commented on July 20, 2024

@angelim I don't think you can do a model.decorate unless you monkey-patch ActiveRecord::Base or specify it in each model class.

My example above was simplified, my project code does sorting, scoping and other stuff on the collection/resource. I omitted it for clarity.

from draper.

jbbarth avatar jbbarth commented on July 20, 2024

In fact it works as long as you have loaded the correct decorator :

>> Server.first.decorate
NoMethodError: undefined method `decorate' for #<Server:0x9f59674>
>> require 'server_decorator'
=> true
>> Server.first.decorate
=> #<ServerDecorator:0x93759c0>

So you can try to require "#{resource_class.name.downcase}_decorator" first (rescuing LoadErrors), then use this syntax. But maybe error handling wouldn't be as easy as in the first proposal then...

from draper.

steveklabnik avatar steveklabnik commented on July 20, 2024

Since this is more of a how-to than an actual Issue, I'm giving it a close. Please re-open if I'm wrong about that!

from draper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.