-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated automatic lookup of representers for entities #73
Conversation
Feedback welcome, this was just a first thought |
This looks excellent, I remember I wanted to do something similar but then didn't feel like it that day, so thanks a lot of going through this 😉 A few things.
|
|
Can we finalise that and merge it in for 1.0? |
Yeah sorry, I've been meaning to get back to this. Will finish it this week. |
@apotonick I've added some more tests with deeper namespaces and I cleaned up a bug those tests revealed on ruby 1.9.3 related to lookups with |
Extremely cool! Thanks so much! 🍻 I might extract your code and use it for defaults in Trailblazer, soon. |
Awesome, just checked out trailblazer and it seems pretty cool. I'll have to follow it 👍 |
subject.for(:json, V2::Singer.new, 'v1/singers').must_equal V2::SingerRepresenter So if the controller is subject.for(:json, Inner::Singer.new, 'v1/singers').must_equal V1::Inner::SingerRepresenter Here, it gives priority to the controller's namespace subject.for(:json, Outer::Singer.new, 'v1/singers').must_equal Outer::SingerRepresenter So, I guess it tries to find I generally love the idea that it sticks to the model's namespace as long as possible, allowing true polymorphism. We need to check, though, how the controller's namespace gets prepended (or not). If you could summarise the rules in a few bullet points, we're good to go. Thanks so much, I really like this!!! 🎆 |
Wait, I think the 2nd case is exactly what I like! subject.for(:json, Inner::Singer.new, 'v1/singers').must_equal V1::Inner::SingerRepresenter So, your rules seem to be:
Might re-arrange the tests a bit... |
And in the process of merging this, we should also take a look at how to deal with multiple media types. represents :xml, :json
What I'm trying to say is: making |
We should probably provide a hook where users can insert format-specific names into the representer name segments - this way we can try and find best practices before hardcoding anything into roar-rails. def process_representer_name(model, segments, format)
segments << format # will result in SongRepresenter::XML
end |
Sweet, this looks like you guys have the right pattern figured out! |
I'm also seeing related problem in This is my code (simplified for example). As you can see, it's wrapped inside a module V1
class Controller < ApplicationController
def update
singular = User.find(params[:id]).extend(UserRepresenter)
consume! singular
end
end
end This is my request: Content-Type: application/hal+json
PATCH /users/1
{
"name": "Bob the builder"
} This is the exception raised on the line where I call NameError: uninitialized constant V1::V1 As you can see, it seems like it'd doubling up on the namespace. |
I have narrowed the issue to model_name = model.class.name.underscore
if namespace = controller_path.namespace
model_name = "#{namespace}/#{model_name}"
end The case is that the The problem is that the code doesn't expect the model to be namespaces, only the controller. |
Possible solution for #72