Use params of locals pass on Rabl::Engine to be method #269

Closed
wants to merge 1 commit into
from

Projects

None yet

3 participants

@shingara
Contributor

When you pass locals params on your Rabl.render you can now use it like
instance method in your rabl view. By example if you have a view like :

object blog
attributes :content, :title

you can define your blog elements directly in you Rabl.render like :

Rabl.render(nil, 'blog/show', {
  :view_paths => 'app/view',
  :locals => { :blog => Blog.new }
})
@nesquena
Owner

@shingara Interesting concept, can you add a quick test for this functionality

@databyte
Collaborator

Hmm. Render already takes in object as a parameter so doesn't it just make sense to pass it? You opted to pass in nil and then created a locals hash to place the object in there.

Normally we pass around a single object (for instance with partial) and allow it to override the object call within the template.

I think introducing the idea of locals and then referencing a "local" within the template adds to the confusion. Especially if you use the "local" for rendering but when using the template through the web framework, it doesn't access to the same locals hash.

Shouldn't an object simply be an object? We should follow a single standard of overriding and passing in objects.

@nesquena
Owner

Yeah I think that's a good point. Was still mulling it over, I do feel that locals might be unnecessary, I rarely find myself missing a locals hash in my APIs. Perhaps if people had a compelling use case.

@shingara
Contributor

My example on commit is not the most explicit use case. To me the use case is when I want mix some data in same output. By example using other data than object or collection in my output. To do that I need generate an specific objet to my rabl output mixin this value.

object blog
attributes :content, :title
child(:current_user) {
  code (:name) { current_user.name }
}

This rabl view works fine in controller environment. But I can't generate it by Rabl.render because I can't override the current_user method.

@shingara shingara Use params of locals pass on Rabl::Engine to be method
When you pass locals params on your Rabl.render you can now use it like
instance method in your rabl view. By example if you have a view like :

```ruby
object blog
attributes :content, :title
```

you can define your blog elements directly in you Rabl.render like :

```ruby
Rabl.render(nil, 'blog/show', {
  :view_paths => 'app/view',
  :locals => { :blog => Blog.new }
})
```
8c8f3e2
@databyte
Collaborator
databyte commented Aug 6, 2012

Opting for #297 but thanks for the attempt. I'll provide you with credit for coming up with another solution. 😁

@databyte databyte closed this Aug 6, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment