views:

547

answers:

2

Hi,

I've a Sinatra app which would be used by different clients. I wish to show the client's Company Logo and a custom layout.erb for each client.

The code base is essentially same for everyone. All I need is a way to easily maintain a different set of files in the 'Public' directory and 'layout.erb', and when deploying to the remote server, automatically copy only the particular client's files.

Any pointers?

Thanks.

A: 

Just found a way solve this problem. It is possible to redefine routes to files in public folders. In fact, Sinatra first checks whether a 'get' request has a corresponding route, and if it doesn't, it goes to the 'public' folder for static content. So for any client-specific file I need, I use a specifc path like '/client/logo.gif' kind of URL. I created a route for such files and applied my custom logic there:

get '/client/logo.gif' do
  #custom logic..
  redirect "#{where_client_files_are}/logo.gif"
end
Jasim
+1  A: 

One possible way would be to have a view and public directory per client and set the proper :views and :public options for each request

get '/:client/...' do
  set :views, File.dirname(__FILE__) + "/views/#{params[:client]}"
  set :public, File.dirname(__FILE__) + "/public/#{params[:client]}"

  # Your code
end

Edit based on comment :

Set your public folder during the config block. Now add one subfolder to your public folder for each client. All you have to do to access the specific file is to modify your view to get /#{params[:client]}/logo.png instead of /logo.png

Yoann Le Touche
Yoann, I wasn't aware of that possibility, thank you! I'd like to however add multiple public folders. A set of files are common to all clients, and only few differ. I'd like to add both to the routes. Any ideas? Thanks!
Jasim
You can set a common public folder, and in it some subfolders with the specific files. Instead of changing the :public options each time you just request the file in the subfolder based on the client
Yoann Le Touche