Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Partial and extend issues #272

Closed
krainboltgreene opened this Issue Jun 25, 2012 · 12 comments

Comments

Projects
None yet
3 participants

So I've got two issues with extends and partial. First it seems neither of them respects the namespacing. For instance consider this routing:

namespace :api do
  namespace :v1 do
    resource :accounts
  end
end

RABL figures out where the views are normally, but partials only look in view_path directory.

Second issue is that a partial child wont show up. Consider the following code:

# app/views/api/v1/accounts/show.json.rabl
object false

partial "api/v1/application/request", object: false

child @account do
  attribute :id
  attribute :name
  attribute :email
  attribute :created_at
  attribute :updated_at
end

And this partial:

# app/views/api/v1/application/_request.json.rabl
child @current_account.access_token => :request do
  attribute :limit
  attribute :created_at
  node :account do |access_token|
    Rails.application.routes.url_helpers.api_v1_account_path access_token.account
  end
end

Here's the return:

{
  "account" : {
    "id" : 1,
    "created_at" : "2012-06-25T04:53:46Z",
    "email" : "bdavis@oyope.name",
    "updated_at" : "2012-06-25T04:53:46Z",
    "name" : "Kelly Carpenter"
  }
}

Now consider this view:

object false

child @current_account.access_token => :request do
  attribute :limit
  attribute :created_at
  node :account do |access_token|
    Rails.application.routes.url_helpers.api_v1_account_path access_token.account
  end
end

child @account do
  attribute :id
  attribute :name
  attribute :email
  attribute :created_at
  attribute :updated_at
end

and the response:

{
  "account" : {
    "id" : 1,
    "created_at" : "2012-06-25T04:53:46Z",
    "email" : "bdavis@oyope.name",
    "updated_at" : "2012-06-25T04:53:46Z",
    "name" : "Kelly Carpenter"
  },
  "request" : {
    "account" : "/api/v1/accounts/1",
    "created_at" : "2012-06-25T04:53:46Z",
    "limit" : 485
  }
}

I should note this is entirely reproducible and the application it's happening in is open source. (Albiet the latest code isn't pushed to github.

Owner

nesquena commented Jun 25, 2012

For your issue, out of curiosity, if you switched from partial to extends:

# app/views/api/v1/accounts/show.json.rabl
object false

extends "api/v1/application/request"

child @account do
  attribute :id
  attribute :name
  attribute :email
  attribute :created_at
  attribute :updated_at
end

I believe you would get the desired behavior of the child inherited in the parent template. Can you try that and tell me if it does not behave as you would expect?

Same result.

On Mon, Jun 25, 2012 at 3:13 AM, Nathan Esquenazi
reply@reply.github.com
wrote:

For your issue, out of curiosity, if you switched from partial to extends:

# app/views/api/v1/accounts/show.json.rabl
object false

extends "api/v1/application/request"

child @account do
 attribute :id
 attribute :name
 attribute :email
 attribute :created_at
 attribute :updated_at
end

I believe you would get the desired behavior of the child inherited in the parent template. Can you try that and tell me if it does not behave as you would expect?


Reply to this email directly or view it on GitHub:
#272 (comment)

Kurtis Rainbolt-Greene, Hacker
Difference Engineers, LLC
643 Magazine St. #102
New Orleans, LA 70130

Owner

nesquena commented Jun 25, 2012

Are you using Rails 3?

Here's my Gemfile:

source 'https://rubygems.org'

ruby '1.9.3'

gem 'rails', '3.2.6'
gem 'rails-api', '0.0.2'
gem 'unicorn', '4.3.1'
gem 'rabl', '0.6.13'
gem 'authority', '2.0.1'
gem 'configatron', '2.9.1'
gem 'sqlite3', '1.3.6', group: [:development, :test]
# gem 'pg', '0.13.2', group: [:production, :staging]
gem 'roleable', '0.2.1'
# gem 'redis-rails', '3.2.3'
# gem 'dalli', '2.0.5'
# gem 'tire', '0.4.2'
# gem 'draper', '0.14.0'

gem 'multi_json', '1.3.6'
gem 'oj', '1.2.9'
gem 'bcrypt-ruby', '3.0.1', require: 'bcrypt'
# gem 'squeel', ''

gem 'factory_girl_rails', '3.4.0', group: [:development, :test]
gem 'forgery', '0.5.0', group: [:development, :test]
gem 'ruby-prof', '0.11.2', group: [:development, :test]

group :production do
  # gem 'newrelic_rpm', '3.3.4.1'
end

group :development do
  gem 'rails_best_practices', '1.9.1', require: false
  gem 'email_spy', '1.6.0'
  # gem 'heroku', '2.26.2', require: false
  gem 'foreman', '0.47.0', require: false
  gem 'pry-rails', '0.1.6'
end

group :test do
  gem 'test-unit', '2.5.0'
  gem 'database_cleaner', '0.8.0'
end
Collaborator

databyte commented Jun 26, 2012

Since it's open source, where's the project and can you push this problem to a new branch?

Curious also on why you had to dig into Rails.application to access the helpers - were the helpers simply not available without doing so?

@databyte Pushing to here: https://github.com/krainboltgreene/manacurve-api

And yes, the helpers weren't available :(

Looks like the helpers are available, I guess it's a legacy piece of code when it wasn't working for whatever reason.

Collaborator

databyte commented Jun 28, 2012

@nesquena - when using partial, there seems to be a different way of it handling the return data than say - extends. I may need to find you on freenode/chat one day to go over some of the little details.

As I traced through partial vs extends, partial did in fact load properly and setup properly. It built out the correct output and the return from partial (which returned from object_to_hash). Then the hash just disappeared. When looking at the main template's Engine, as it was evaluating the next child - the Builder#compile_hash didn't have a reference to the contents of the other builder's results in @_result.

Anyway, @krainboltgreene - since I couldn't get partial to return properly and I didn't want to spend all night looking into that - I went with extends. Unfortunately, you also uncovered a bug in extends. I'm pushing to master the fix so you'll need to point to that.

Here's the final show.json.rabl and _request.json.rabl from my fork

Collaborator

databyte commented Jun 28, 2012

Forgot to mention the issue in the commit itself but that should fix it. Closing. Reopen if you still have problems.

@databyte databyte closed this Jun 28, 2012

Thanks a lot @databyte, I'll pop open issues as I continue to use rabl.

Owner

nesquena commented Jun 28, 2012

Thanks for fixing that @databyte good catch! Pushed out 0.6.14 with those two fixes you made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment