Ruby On Rails: of what main components (gems) does it consist? - ruby-on-rails

I'd love to dive deeper into Ruby On Rails so I can understand the magics behind it.
For this, I'd like to examine all main components (gems) of Rails one by one, e.g. ActionPack, ActiveResource, etc. For this I'd love to have a list with the main components with short explanations (I will add more explanations step by step when learning about the gems).
Feel free to edit my answer with supplementary details.

Following are the Basic rail components with their short discription::
Active Support is a compatibility library including methods that aren't necessarily specific to Rails. You'll see ActiveSupport used by non-Rails libraries because it contains such a lot of useful baseline functionality. ActiveSupport includes methods like how Rails changes words from single to plural, or CamelCase to snake_case. It also includes significantly better time and date support than the Ruby standard library.
Active Model hooks into features of your models that aren't really about the database - for instance, if you want a URL for a given model, ActiveModel helps you there. It's a thin wrapper around many different ActiveModel implementations to tell Rails how to use them. Most commonly, ActiveModel implementations are ORMs (see ActiveRecord, below), but they can also use non-relational storage like MongoDB, Redis, Memcached or even just local machine memory.
Active Record is an Object-Relational Mapper (ORM). That means that it maps between Ruby objects and tables in a SQL database. When you query from or write to the SQL 19database in Rails, you do it through ActiveRecord.
ActiveRecord also implements ActiveModel. ActiveRecord supports MySQL and SQLite, plus JDBC, Oracle, PostgreSQL and many others.
Action Pack does routing - the mapping of an incoming URL to a controller and action in Rails. It also sets up your controllers and views, and shepherds a request through its controller action and then through rendering the view. For some of it, ActionPack uses Rack. The template rendering itself is done through an external gem like Erubis for .erb templates, or Haml for .haml templates. ActionPack also handles action- or view-centered functionality like view caching.
Action Mailer is used to send out email, especially email based on templates. It works a lot like you'd hope Rails email would, with controllers, actions and "views" - which for email are text- based templates, not regular web-page templates.
Action View is a framework for handling view template lookup and rendering, and provides view helpers that assist when building HTML forms, Atom feeds and more. Template formats that Action View handles are ERB (embedded Ruby, typically used to inline short Ruby snippets inside HTML), and XML Builder.
Action Resource (ARes) connects business objects and Representational State Transfer (REST) web services. It implements object-relational mapping for REST web services to provide transparent proxying capabilities between a client (ActiveResource) and a RESTful service (which is provided by Simply RESTful routing in ActionController::Resources).

Related

Collection of text files in place of rails database

I'm currently constructing a Rails site to edit a collection of files used for configuration of various services. The files are simple plain text files. The point of the site is to provide an easy interface for editing the files as well as validate changes for the less technically incline individuals who will be editing them.
I've looked around but I can't seem to find anything on using text files instead of a database. What I do find suggests that what I'm trying to do may not be correct for rails at all. The closest thing have is this question, but the answers are less than helpful.
Is there a correct way to create an MVC for text files and not use a database at all?
Yes. Rails provides MVC with activerecord being default for models. you can keep rails views and controllers (VC part) and write your own models (M part). Imagine using mongoid ORM instead of active record. For that reason rails provides activemodel to make it easy for people to write their own models that are backed by different storage mechanism. look at https://github.com/rails/rails/tree/master/activemodel to get started on writing your own ORM that will use text files as backend instead of SQL database. Also include http://api.rubyonrails.org/classes/ActiveModel/Lint/Tests.html in your model test files to verify that your models conform activemodel api. After that, you can use rails form helpers in views and routes with your models without problem.

Correct rails place for no-db data fetching code

I'm looking for the "rails" design pattern for code that fetches data from other websites.
I have a rails controller in my app that fetches data not from the database, but from external API's or scraped from the web.
Where's the "rails" place to put this code.
For quick implementation, I just stuck it in a model, but the model doesn't interact with the database - or support standard model functionality - so that feels wrong, but my understanding of rails and ruby isn't yet solid enough to know where it should go.
The way the code works roughly is
controller calls model.fetchData args
the model uses HTTParty or similar to make the call
processes data
passes it back to the controller
Any advice?
Broadly-speaking I think there are two possible ways to do this:
Create a plain ruby class to contain the methods for making requests to the API(s) and processing responses from it(them). You can include the HTTParty module in this class with include HTTParty. The usual place to put this code is in lib/ (make sure that wherever you put it, the path is in autoload_paths).
If you're doing anything really complex, or the API itself is complex, you might want to consider creating a separate gem to handle interaction with the API(s). The term for this type of gem is an "API wrapper" -- if you look around, you'll see there are lots of them out there for popular services (Twitter, LinkedIn, Flickr, etc.)
Notice I haven't mentioned activerecord. If you're not going to be saving anything to the DB, I don't see any need to even create any activerecord models. You can get by with just controllers and views, and then (if needed) pick and choose components from activemodel (validations, internationalization, etc.) to make your ruby API wrapper class feel more like a Rails model. For example, one thing that I've done in an app I'm working on is to apply validations to query strings before actually making requests to an external API, which is a bit like running validations on database queries before querying a DB. See this article by Yehuda Katz for more on how to make plain ruby objects feel like activerecord models.
Hope that helps. I answered another question very similar to this one just yesterday, you might want to have a look at that answer as well: Using rails to consume web services/apis

Rails app with non-HTTP access

Hypothetical question (at the moment!)
Suppose I have a great idea for an application. It acts on data which can be well-represented by tables in a relational database, using interlinked objects which represent those tables. It supports a well-defined API for interacting with (Creating, Reading, Updating, Deleting) those objects, and viewing information about them.
In short, it's a perfect fit for Rails... except it doesn't want to be a web-app. Perhaps it wants a Command Line interface; or an OS-native dialog-based interface; or perhaps it wants to present itself as a resource to other apps. Whatever - it just isn't designed to present itself over HTTP.
These questions suggest it's certainly possible, but both approach the problem from the point of view of adapting an existing web-app to have an additional, non-web, interface.
I'm interested in knowing what the best way to create such an app would be. Would you be best to rails new non_web_app, in order to get the skeleton built "for free", then write some "normal" Ruby code that requires config/environment - but then you have a lot of web-centric cruft that you don't need? Or would it be better to roll up your sleeves and build it from whole cloth, taking just the libraries you need and manually writing any required configuration?
If the latter, what exactly is needed to make a Rails app, but without the web bits?
If you want to access the Rails ORM to develop a CRUD non-web application, just include ActiveRecord in your own Ruby script; you will avoid using a lot of Rails modules you probably don't need (routing, template generator, ...) Here is an example of how to do it.
If you prefer to have the full Rails stack, do not run your Rails web app in an application server (WEBrick, Passenger, Mongrel, ...) to avoid any HTTP exposure, and interact with your application using tasks or the rails console.
I would avoid taking Rails too far off the rails. If I were doing this and felt that the gains of rails w/o the web stuff I'd do the following:
rails new non_web_app
and ignore the webbish cruft and use rails to generate models. In this way you get the tight, comfortable database behavior and can tie various gems in as you want to augment those models. I'd not bother implementing views, of course, and I'd consider implementing controllers in which the various render bits are removed and to use you instantiate an instance of the controller and call the action directly. This means the controller represents your API into your business logic still but the "views" it now "renders" are simply the return of the data output.
Then you could simply strip out the bits you do not need...the public directory, the view structure under app, config/routes.rb, etc. You'll need to test those changes incrementally and make sure that removing some now extraneous bit doesn't throw the Rails world into chaos.
Rails is for Web apps. That means HTTP. Now, you could package a Web app so that it runs on the desktop instead, or you could use ActiveRecord with a desktop application framework like Monkeybars.

Is Rails 3 modular like Merb and Ramaze

Will Rails 3 be a modular framework like Merb or Ramaze?
By this I mean, can we use any persistence framework (DataMapper or Sequel) and jQuery in Rails 3 application (via the command line for example)?
Or the default stack (ActiveRecord, Prototype) is still enforced?
(Sorry, I'm pretty new to Ruby/Rails community).
Thanks.
Rails 3 is much more modular than previous versions. Although it still defaults to ActiveRecord and Prototype, it has an ActiveModel API which means that ActiveRecord can be swapped out for a compatible ORM that implements the API.
Rails 3 also embraces the principles of Unobtrusive JavaScript and to this end the view helpers no longer output inline JavaScript mixed up with HTML. Instead, HTML5 data attributes are used and there are Prototype and jQuery "drivers" for hooking into those and adding behaviour to elements.
The whole ActionController stack is also much more modular enabling you to cherry pick just the parts you need and the common (non-HTTP specific) controller functionality is now shared by ActionMailer too.
Rails merged with Merb in late 2008 and the result is Rails 3
http://yehudakatz.com/2008/12/23/rails-and-merb-merge/
http://weblog.rubyonrails.org/2008/12/23/merb-gets-merged-into-rails-3

What is new in rails 3?

Couldn't find any compiled info on new things in rails 3. Nor release date.
From http://mcphersonz.wordpress.com/2009/05/13/key-features-for-rails-v2-2-rails-v2-3-and-the-upcoming-rails-v3-0/
Lock up all the unicorns. You can’t please everyone all the time, so don’t focus on the unicorns.
No holy cows. Nothing is sacred in rails & everything is up for debate. Don’t look any anything we have & feel like that is the way it has to be.
Major Themes:
New routes. Faster, route by subdomain, user agents, etc. Rack to other rack machinery.
XSS protection (cross side injection). By default all output in views will be escaped. No more use of <%= h something %>. Assumed by default.
Javascript goes Unobtrusive & Agnostic. Much less JS is injected into page & instead unobtrusive JS is used to achieve same effect.
More Agnosticism.
Action ORM is a slim proxy wrapper for ORM tools that allows for easy swapping of ORM implementation,
Generators (script/generator) will for example allow you to specify that rspec is used, so script/generate model will create a rspec test file for model.
Refactoring
Abstract Controller takes similarities between for example ActionController & ActionMailer and combines them into one code base.
Cherry picking from ActiveSupport allowing you to pick parts that are used instead of pulling in the entire library.
Increased performance / speed with callbacks.

Resources