Skip to content
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

"partial" and "extends" support in Sinatra #50

Closed
ismasan opened this issue Jun 29, 2011 · 7 comments
Closed

"partial" and "extends" support in Sinatra #50

ismasan opened this issue Jun 29, 2011 · 7 comments

Comments

@ismasan
Copy link

ismasan commented Jun 29, 2011

partial() and extends() don't work in Sinatra because the internal method fetch_source() relies on either Rails or Padrino to resolve the path for the views directory. I had to monkey-patch that method in my Sinatra app:

module Rabl
  module Helpers
    def fetch_source(file, options={})
      file_path = Dir[File.join(APP_ROOT, 'views', file + "*.rabl")].first
      File.read(file_path) if file_path
    end
  end
end

.. Where APP_ROOT is a constant I defined in my app.

Is there a better way?

@nesquena
Copy link
Owner

Yeah you are absolutely right. I never investigated the best way to build the file path for a template in a sinatra app. I know there is a way to get the view_paths in the app with settings.views but I didn't have access to that in the helpers module. I always use RABL alongside Padrino / Rails so it wasn't an issue for me. If anybody has any suggestions or patches, I would be curious.

One option is to support a proc inside rabl configuration:

Rabl.configure do |config|
  config.fetch_source = lambda do |file, options|
    file_path = Dir[File.join(APP_ROOT, 'views', file + "*.rabl")].first
    File.read(file_path) if file_path
  end
end

What do you guys think?

@ismasan
Copy link
Author

ismasan commented Jun 29, 2011

Maybe just:

Rabl.configure do |config|
    config.views_path = 'my_own_custom_path'
end

@nesquena
Copy link
Owner

Actually yeah that is much more reasonable, I think that would be the best bet.

@nesquena
Copy link
Owner

This should be fixed for Sinatra (has been for a while), finally closing.

@delphaber
Copy link

Hi. I think this solution does not work no more. :(

NoMethodError: undefined method `views_path=' for #Rabl::Configuration:0xb04dad4

@nesquena
Copy link
Owner

nesquena commented Mar 5, 2012

Not sure you should have to set anything, here's where its calculated: https://github.com/nesquena/rabl/blob/master/lib/rabl/partials.rb#L52

@delphaber
Copy link

I'm using subfolders. I've fixed using another tip from another user in this github
Hope this could help someone else :)

views/activities/index.rabl:
collection @activities

extends 'show', {:view_path => 'views/activities/'}

This should load views/activities/show.rabl for each activity in @activities

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

No branches or pull requests

3 participants