I'm trying to get a better handle on Rack middleware, and I've just learned that you can view all of the installed middlewares for a Rails app using rails middleware. In a fresh Rails app, that's about 25 different middlewares.
I'd really like to examine the source code of these different middlewares, but I can't find out where they live in the app. Have they all been compiled to executable, so the only way I can read the source is on Github? If so, how can I find those repos? Thanks!
For the ActionDispatch, ActiveRecord, Rails, ActiveSupport, and Testing middlewares, they'll be in rails/rails, the Rack stuff will be in rack/rack, and WebConsole will be in rails/web-console
They're not compiled into an executable, no. Each gem is downloaded based on the contents of your Gemfile and gets semi-magically loaded via your environment and config.ru.
Related
I have a Sinatra app and I'd like to start building new functionality in Rails while still supporting the existing Sinatra functionality. I've tried the following strategies:
sinatra's rackup routes some requests to rails and some to sinatra
sinatra's rackup includes rails
rails' rackup includes sinatra.
Many of my searches resulted in rails 3, not 4. Additionally, does Rails have to generate the db versus using one that Sinatra was using (in this case, the Sequel gem to access Sqlite3.) In general the errors I was getting were about gems and paths. (Although I did rebundle and try different versions of paths.)
Any suggestions on the best way to use Rails 4 while still supporting an existing Sinatra app?
I don't think the Rails/Rack integration code has changed very much between Rails 3 and 4, so you should be fine. The Rails on Rack Guide explains in more detail that you can make a config.ru file for a Rails application that looks like:
require ::File.expand_path('../config/environment', __FILE__)
use Rack::Debugger
use Rack::ContentLength
run Rails.application
and then running rackup config.ru will start a rack server running your rails app.
The answers to this question point out that if you run Rails and Sinatra from Rack, rather than mounting your Sinatra app in Rails' routes.rb file, requests to your Sinatra app won't go through Rails at all. The answers also show that in your config.ru you should be able to do this to support both your Sinatra and Rails apps:
map "/" do
run RailsApp::Application
end
map "/url1" do
run SinatraApp1
end
You'll have to modify the routes and application names to match your needs and your applications, of course.
I'd recommend getting your apps running through one config.ru first, and then ask another question about your databases, explaining in more detail what you'd like the database setup to be and what the exact error messages you're getting are.
Rails does not need to create a database or even use one directly. To generate a new Rails app without ActiveRecord, run rails new APP_PATH --skip-active-record. Then, instead of using a database directly from the Rails app, send requests to the Sinatra app and have the Sinatra app control everything database-related.
I want to store the session data in database using the ActiveRecord::SessionStore module. I have been searching for quite sometime for this without success. Either i am not using the proper search terms or i am being blind to something very obvious.
I have used this statement require ActiveRecord::SessionStore::Session in my code to enable session handling with active record. It conks out with the error uninitialized constant ActiveRecord::ActionDispatch . I assume that i have install the actiondispatch module. Am I correct?
Please bear in mind that this is my first shot with Ruby-Sinatra. I am coming from PHP.
So, what should i use to make Sinatra use database-based sessions using ActiveRecord?
I ended up solving the uninitialized constant ActiveRecord::ActionDispatch error by adding the actionpack gem to my application's Gemfile and requiring action_dispatch. You might also have to require 'logger'
Sinatra is a Rack based application and allows addition of middleware. Middlewares can be plugged into any Rack based framework.
Search on references of config.ru in the Sinatra Book
so you would create a config.ru file in the root of your app.
and put something like this
require 'my_app'
use ActiveRecord::SessionStore
run MyApp
I am using Ruby on Rails 3 and I would like to implement some Rack middleware.
I know (but maybe I am wrong!) that, before of version 3, there was the Rails Metal "system" to handle those. But now?!
Where in my Rails application I have to locate files to use for middleware purposes (before of version 3, if I am not wrong, the folder was named 'metal')? then, how I must state them in the application.rb file?
Check out the Rails on Rack page on Rails Guides.
Your own middlewares can be stored in lib and can be required at the top of application.rb.
Checkout the following blog post, that may answer your question:
http://tektastic.com/2010/07/rails3-rack-and-where-did-my-metal-go.html
Check out this http://www.engineyard.com/blog/2010/rails-and-merb-merge-plugin-api-part-3-of-6/ for how to create a "Metal" controller
http://www.ruby-on-rails-outsourcing.com/articles/2010/05/28/how-to-create-your-own-rack-middleware-class-in-ruby-on-rails/ may be helpful for you.
It basically suggests to put your class in /lib/class_name.rb and then instruct it to be used like
Rails::Initializer.run do |config|
config.middleware.use "ClassName"
...
I believe you are right there is no more "metal" option in rails 3 http://tektastic.com/2010/07/rails3-rack-and-where-did-my-metal-go.html you have to use a rack middleware instead. I'm unsure if this causes a performance hit or not.
I'm working on creating my first Rails 3 engine and I'm currently getting the following error message
is a Railtie/Engine and cannot be installed as plugin (RuntimeError)
To give you a bit more background I'm not doing the Engine as a gem, but I've place the Engine files in the vendor/plugins folder. I know there is an issue with loading when in the plugins folder, but I'm not clear on how to solve the problem.
My rails/init.rb file in the Engine has the following code
require 'rails'
module RailsApp
module MyEngine
class Engine < Rails::Engine
config.after_initialize do
RailsApp::GameType.register do |game_type|
game_type.name = "TreasureIsland"
game_type.version = "0.1"
game_type.thumbnail = "teasure_island.jpg"
end
end
end
end
end
Suggestions?
I think I remember reading that Railties would not work in plugins directory, because they are intended to be loaded at a different point in the application boot process. I would recommend creating a gem using something like Jeweler, which does alot of the work for you. The purpose of the Railtie/Engine is to have a reusable component that can be included in multiple rails apps. Once you have a gem created, you can point to the local gem path within your Gemfile. This allows you to see changes in your engine code inside your rails app without having to build and reinstall the gem every time you make a change to the engine code. Of course you would want to point bundler to the installed gem in production. I would recommend putting it on github and using that URL in your Gemfile in production.
Bundler local gem example:
#Gemfile
gem "my_engine", :require => "my_engine", :path => "/my_engines/my_engine"
Check out the Modern Rubyist's website. He has a good series on creating Railties and Engines. There may have been some changes to Rails since this was written, but I think most of it is still relevant. It helped me out a good bit when I was learning how to write Engines with Rails 3.
http://www.themodestrubyist.com/2010/03/01/rails-3-plugins---part-1---the-big-picture/
http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/
http://www.themodestrubyist.com/2010/03/16/rails-3-plugins---part-3---rake-tasks-generators-initializers-oh-my/
http://www.themodestrubyist.com/2010/03/22/rails-3-plugins---part-4---more-on-generators/
John, I believe engines (which are typically gems) vs plugins (which live in vendor) are loaded at different points in the rails initialization process.
Engines actually have a bit more flexibility, they can hook deeper into rails. In addition, packaging as a gem has a lot of advantages: easier to share across apps, easier to maintain in a separate code repo, easier version control.
I'm creating my first rails engine right now and created a useful starting point and walk-through for getting started:
http://keithschacht.com/creating-a-rails-3-engine-plugin-gem/
I just look up at rails sources and find folder named "dispatches". There is four file in it. I want to know purpose of this files. I know I use this files on my production server, but I never used to think of their purpose. I know there is something about attaching Rails app to Apache server. On my production server rails appname command add this files to public folder automatically. Can I set up this behavior on my development machine?
The rails dispatcher is the entry point for a rails application and is used to bootstrap the environment.
They have a long history and in a lot of ways they are almost obsolete. In days gone by rails apps used to be powered using cgi or fastcgi, which was a way for the webserver to communicate with a rails process. The boot process would be initiated by dispatch.fcgi or dispatch.cgi. Nowadays people are more likely to use apache/nginx+passenger or apache/nginx+mongrel/thin. (Does anyone still use lighttpd?)
I'm a little fuzzy on how dispatch.rb is used, but I think it's used by upstream rails servers such as mongrel/thin to bootstrap the rails process. However, now that rails is rack compatible I'm not entirely sure if that has changed.
You don't need to pay the dispatch.* files any attention.
I hope this helps.