Comments (7)
Hi @jonlhouse,
don't you think it would be simpler to use end_of_association_chain.decorate
and super.decorate
?
from draper.
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.
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.
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.
@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.
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.
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)
- Create unique context for each item in a collection?
- Draper is incompatible with Ruby 3 HOT 5
- Draper changes the behaviour of url_for when using STI models
- Sending problematic method names (open, format) to decorated rails objects sends to Kernel (conflicts with ransack)
- undefined method `auto_link' for spec with type :decorator
- Generators don't work. HOT 3
- Draper::UninferrableDecoratorError (Could not infer a decorator for Document.) HOT 3
- CI: A test MAY fail with NameError: uninitialized constant ActionMailer::Base
- Release new version HOT 7
- Breaks respond_with in Rails API apps HOT 2
- ArgumentError using helpers with Ruby 3 HOT 4
- Mongo error -> Uninitialized constant ActiveRecord Did you mean? ActiveModel HOT 1
- Usage with `Turbo::Broadcastable` HOT 1
- undefined method `attributes' for nil:NilClass
- Automatically use decorated associations in delegate methods HOT 1
- Unexpected behavior when `frozen?` method is defined in model HOT 1
- Scaffold generator: Invalid path when using --model-name=Record
- Draper still actively maintained? HOT 10
- Rails - STI bug for ActiveRecord_Relation, works fine for a single record
- issue with using draper outside of controller/view context within a rake task or (active)job
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from draper.