reload rails.root/app/resources/* per request - ruby

I have a rails app with a folder rails.root/app/resources where I keep some library code.
This library code is used in a rack app that I mount in routes.rb in my Rails application
# rails.root/app/resources/file_resource.rb
# routes.rb
mount DAV4Rack::Handler.new(
:root => Rails.root.to_s,
:resource_class => FileResource
), :at => '/', :constraints => {:subdomain => "w"}
How can I make the FileResource reload on each request in development?
I tried autoload_paths, reload_plugin, none seem to work.
I think it has something to do with the code in routes.rb.
If I make a file rails.root/app/resources/my_helper.rb and use MyHelper.test() inside FileResource, the MyHelper gets reloaded.
No, I don't want to move this in 'lib' folder.

I am currently using the following hack, if anyone has a better solution please do share!
# config/environments/development.rb
root = config.root
config.to_prepare do
load "#{root}/app/resources/file_resource.rb"
end

Related

Rack/Sinatra Method Not Allowed

I am developing a simple web app using Sinatra and using rack as the middleware and hence have a config.ru.
To run the application I use shotgun config.ru.
I have no problem when the application does a GET request. But my app has a couple of POST requests, and when I submit a form via POST method, I get this strange error:
Method Not Allowed
Following is the content of my config.ru:
require "rack"
require 'rack/contrib/try_static'
require File.expand_path("app", File.dirname(__FILE__))
use Rack::TryStatic, :root => File.join(App::SETTINGS.source, App::SETTINGS.site.config['destination']), :urls => %w[/]
run App
Any idea what could resolve the issue?
Thank You
The following will not respond to posts:
get '/hi' do
"Hello World!"
end
It is quite possible that you will need to do something like this:
post '/hi' do
# do post stuff
end
I solved the issue.
It was a problem with rack.
I replaced
use Rack::TryStatic,
:root => File.join(App::SETTINGS.source, App::SETTINGS.site.config['destination']),
:urls => %w[/]
with:
use Rack::Static,
:urls => ["/#{App::SETTINGS.site.config['destination']}"],
:root => File.join(App::SETTINGS.source, App::SETTINGS.site.config['destination'])

Ruby Rack Heroku: Serving Static Files

I have followed the Heroku guide on deploying static files using Ruby Rack (https://devcenter.heroku.com/articles/static-sites-ruby), but I was unable to access any HTML file in \public apart from index.html. Namely, localhost:9292/test.html still maps to index.html. (All my style and js files serve correctly).
Below is my config.ru file. I know what's wrong, but not sure about a valid solution?
use Rack::Static, :urls => ["/images", "/js", "/css"], :root => "public"
run lambda { |env| [
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/index.html', File::RDONLY) ] }
For your config.ru file, try:
use Rack::Static,
:urls => ["/images", "/js", "/css"],
:root => "public"
run Rack::File.new("public")
You could also replace the last line with run Rack::Directory.new("public") which would serve up all your files, but would give a file browser like interface if someone went to the url of a directory (like the root directory)
I have no Ruby experience and I found this boilerplate project helpful when trying to deploy a static website to Heroku:
heroku-static-site
Particularly, make sure you also have Gemfile and Gemfile.lock files besides the config.ru.
Author's project structure hosts everything in the root directory but you can easily move everything to public/ and correct the config.ru as #looby suggested.

Rails namespaces with only numbers

I'm building an api in rails, and I have been told that I need to scope the site so it has versions.
Something like /1.0/do_something.
From what I've read, I do this by making a routes.rb that looks like the following:
API::Application.routes.draw do
scope :path => "1.0", :module => "1.0" do
post "do_something" => "controller#method"
# Everything else. Glob is saved in params[:r]
match '*r', :to => 'errors#e404'
end
end
But when I rake routes, I get the following error:
rake aborted!
missing :action
What am I doing wrong?
You cannot have a module called "1.0" for obvious reasons. Consider calling this V1 instead.

rack-offline in sinatra

I am trying to setup rack-offline in Sinatra, but I am having trouble setting it up. In rails it is prettty easy, but have no found any examples in Sinatra...
Basically, in your config.ru, map /application.manifest to Rack::Offline. (If you're not familiar with using config.ru with your Sinatra application, check out this part of Sinatra docs.) Here's an example, which caches all the files under directory public:
require 'your-app'
require 'rack/offline'
map "/application.manifest" do
offline = Rack::Offline.new :cache => true, :root => "public" do
# Cache all files under the directory public
Dir[File.join(settings.public, "**/*")].each do |file|
cache file.sub(File.join(settings.public, ""), "")
end
# All other files should be downloaded
network '/'
end
run offline
end
map "/" do
run Sinatra::Application
end
Remember to set manifest="/application.manifest" in your html tag and you should be good to go. You should take a look at rack-offline's README for more documentation and explanation of how it works.

Cannot load 'paperclip/storage/ftp' when using paperclipftp in Rails 3

I've just installed paperclip 2.3.3 and paperclipftp 0.1.0.
Paperclip was working fine, the attachments were saving and everything was great.
Enter paperclipftp.
I've included both gems in my Gemfile, installed it with bundle and made sure all dependencies were satisfied. I've also double checked that all my ftp info is correct and the server is working fine.
When I try to attach a file using ftp:
has_attached_file :photo,
:styles => {
:small => "204x159#",
:original => "460X370#"
},
:storage => :ftp,
:path => "/:attachment/:attachment/:id/:style/:filename",
:url => "http://kickassserver.com/_myfolder/:attachment/:attachment/:id/:style/:filename"
I get the following error:
Paperclip::StorageMethodNotFound in SetupsController#create
Cannot load 'paperclip/storage/ftp'
I'm thinking that paperclipftp isn't actually being loaded by my app. Is there a way I can check to see that it's actually being loaded, or has anyone else experienced this?
Thanks,
Matt
I have ruby 1.9.2p180 and the problem is that the timeout class being loaded.
Only add,
require 'timeout'
to the application.rb and this will fix your error.

Resources