Gemified Plugins Architecture\Design Patterns in Ruby 2.0 - ruby-on-rails

In framework-like ruby projects, how is it possible to package a plugins as gems and then use\unuse them inside the project, Most popular living example is Rails, but I seeking the abstracted concepts\practices\design patterns that can aid in building such project architecture.
Rails sourcecode is TOO big to navigate without a map.
Research Log:
- In Rails, Railities are assumed to be a good place to start exploring how plugin architecture is implemented.
- In Ruby 1.8, there was a C extension called mixology that provides mix\unmix constructs.
- In Ruby 1.9.*, It is believed that Rails used alias_method_chain
- In Ruby 2.0 prepend keyword is meant to be useful in solving such problem (I assume!)

One of the best introductions into topic I have seen so far was a Railscast where Ryan extracts some piece of his app into a gem:
http://railscasts.com/episodes/301-extracting-a-ruby-gem
Generally there are few rules to follow to make Your code easy extractable:
Extract common logic to modules or even better separate service objects
Following SOLID principles will make Your units lean and isolated as much as possible
ALWAYS namespace Your concerns, service objects, libraries etc
Test those units completely isolated, don't rely on details like database
(Controversial) Do dependency injections, You won't regret this when testing and simply when designing APIs between Your objects
Avoid using Rails helpers as much as possible if You intend Your code to be reusable outside a Rails context
I hope these suggestions will put You on Your own Rails :)

Related

Benefits of batman.js in angular.js?

I am planning to start a new complex project with ruby on rails, and I am currently thinking of which frontend-framework to use. I have never used one before.
Reading the docs, batman.js becomes really interesting. I would love to do things like "#post.comments" on the client side to get all comments for a given post - with nearly zero configuration. I am afraid of duplicated code if using a mvc-framework in the frontend and guess batman.js will reduce it. But in comparison to angular, batman does not gain from much support in the community (see Google Trends for example).
So my question is, is it possible to have the benefits of a batman.js-app in an angular-application with a little configuration? Do you have any good links for a good rails-integration?
I guess this is relevant to the batman.js side. We have a couple Rails + batman.js apps in production already and we'll be launching another one soon. Here are the advantages and disadvantages we've found:
Advantages:
Ruby & Rails-inspired syntax & architecture (eg, model associations, naming conventions, routing)
Easily integrates with Rails (batman-rails gem, JSON REST communication, CoffeeScript, Slim templates)
Easy to implement real-time updates (we're using Pusher)
Disadvantages:
Low "googleability". Not as widely used as Ember, Angular, Backbone etc.
Underdocumented. All our developers dive into the source from time to time.
For my part, I'm enjoying batman.js a lot and I'm trying to make up for the shortcomings by adding to documentation and being active on IRC, github and the google group. Good luck in your decision!

Uses of Ruby on Rails

I am a somewhat experienced rubyist, but I am now starting to do rails development. I know rails is used to make "web applications" but what exactly does that mean? Do you build entire sites with rails or do you build a feature for a website in rails, and integrate it in to an existing website? (I know this question is kind of vague, but any answers to how ROR is used in the creation of websites would be much appreciated).
Generally speaking, you will build a full site with Rails, though you can integrate it into an existing website, as well. It is designed to provide a top-to-bottom solution for entire websites.
For standalone features or small scripts you just want to expose to the web, you might look at something like Sinatra or Camping.
What is Ruby on Rails?
I'm also build some rails applications in that I had entirely use rails to develop my web application. It's quite easy rather than using two or three languages. But when I developed I had to use Javascripts, CSS and Ajax to create more attractively
For me it's a great framework to build website and minimal knowledge is required although experience and knowledge of ruby gives you an upper hand.
I also feel that rails is good not only to build websites but to manage it.
Also the support community of rails is awesome and is complemented by guys like Ryan bates with their screen casts.
Also when we build a website using ror, we have the flexibility to concentrate on different features at different times or by different person.
This gets a great modular structure.
So you can design the basic functionality today and tell your colleague to get the views attractive using JS and friends without affecting your functionality.
Also the external support for rails (like gems and plug ins) is awesome and makes life heaven!
I would suggest that it's a great framework and the best feature is that it is a very easy to begin and learn but it always surprises you with new things.
Thus it allows you to learn a lot!
Hope it helps.
The website/web application distinction is made because there are many Rails apps in existence with no front end or 'website' to speak of. Using Rails, one could serve JSON amongst a cluster of compute nodes for data aggregation, or text to unix sockets for display on terminals. The uses for Rails are only limited by ones imagination, and you could even use Rails for single host interprocess communication if you desired. This might prove effective for a highly skilled rails programmer rather than learn a new tool to achieve similar results.

what is ruby on rails?

I am a front end developer (HTML,CSS,JS & jQuery) I know a bit of PHP. I am trying to grasp what Ruby/Ruby on Rails is.
On http://rubyonrails.org/ it says "Ruby on Rails is an open-source web framework that's optimized for programmer happiness"
In actual, non-dreamy terms, what is special about Ruby/Ruby on Rails, and how can a person like myself benefit from learning to use it?
My simple question is: whose sister is it? PHP, mySQL, ASP, coldFusion, jsp, apache?
What does it do?
The Language
Ruby is a recent programming language that shares most of its heritage with Perl and Smalltalk. You can see what Ruby looks like (and try it yourself) at tryruby.org.
Just like you can use PHP to write web pages, you can do the same thing with Ruby.
The Framework
Ruby on Rails is a set of software devices that help you to more easily write a website in Ruby. The primary things they try to facilitate ("make easy") are:
Storing related data (e.g. blog posts and the comments on them) to a database.
Accepting web requests and respond to them programmatically (e.g. check a user's password)
Composing HTML using your data, with layouts and templates to make it easier.
"Rails," as it's called for short, is built with extreme prejudice towards certain application models, particularly MVC. What this means is that unlike PHP, where any .php source file is fair game for any bit of code, most code in a Rails application is written in a particular, conventional place. When people compare Rails to PHP, they often point this out.
There's a lot I didn't cover, but these are Rails' most basic features. To see what it looks like, I'd suggest watching the infamous, "Creating a Weblog..." screencast.
If you're looking for something to compare it to for someone coming from PHP, think of it like CakePHP for ruby. It's a bunch of pre-developed low-level classes and systems that can then be extended out to use in many different systems. It also bundles in a lot of other helpful tools and a plugin architecture. It uses ruby and an HTML templating language based on ruby (erb) rather than PHP.
It's a framework for Ruby whose sweet spot is CRUD based web apps. They make a number of assumptions (aka "conventions") that make it easy and fast to create tables with 1:1, 1:m, and m:n relationships, objects, controllers, and web pages for exposing and manipulating that data on the web.
Ruby is just another object-oriented language, like Java, C++, or C#. It was invented by a gentleman from Japan.
This might not be the best analogy, but like you would use HTML/CSS/JS to display a cohesive entity with specific stuff in their specific places. Rails provides a framework to allow you to work in an organized environment with specific stuff in their specific places. As well as having goodies to provide for more rapid development.
Generally:
Html would contain the content you want to display, while CSS would help control the specific look of that content, while JS would allow you to manipulate or interacte with said content.
Rails, uses MVC (Model, View, Controller) to provide a similar organization. Where Data objects are generally categorized as Models, while Views control the displaying of the Data. And Controllers would would facilitate the flow of that data from View to Model and vice versa.
Rails is not the only game in town. So do look around, as there are other great frameworks out there, and find the one best suited to your style and language needs.
Philosophy and design
Ruby on Rails is intended to emphasize Convention over Configuration (CoC), and the rapid development principle of Don't Repeat Yourself (DRY).
"Convention over Configuration" means a developer only needs to specify unconventional aspects of the application. For example, if there is a class Sale in the model, the corresponding table in the database is called sales by default. It is only if one deviates from this convention, such as calling the table "products sold", that the developer needs to write code regarding these names. Generally, this leads to less code and less repetition.
"Don't repeat yourself" means that information is located in a single, unambiguous place. For example, using the ActiveRecord module of Rails, the developer does not need to specify database column names in class definitions. Instead, Ruby on Rails can retrieve this information from the database based on the class name.
Taken from wikipedia
Ruby An elegant object-oriented programming language with objects like Smalltalk and syntax something like Python and Perl. It's kind of what PHP would have been had it been done right. That is, Ruby takes features from advanced "real" languages and combines them with the interpreted efficient syntax and library-rich practicality of Perl, Python, and PHP.
RailsAn advanced web framework. Many recent web frameworks are considered to be copies of or at least inspired by Rails. (Google "rails clone".) "The first good web framework".
Both Ruby and Rails are generally highly regarded.
Ruby is an object oriented programming language. Rails is a framework built in Ruby for developing web applications. I have been using rails for about 6 months now, and its been great so far. If you want some of the same features of rails, you should also consider django (python), cakephp or symfony (php).
Pros:
MVC Architecture - Code is organized,
broken up into small, manageable
files
Database abstraction - you dont have
to write SQL queries, database types
are easily interchanged
Convention over Configuration - rails automatically knows file, database, variable names without explicitly being specified
Fast Development - Rails requires less lines of code because of all the functions it automatically takes care of and is more expressive, resulting in faster development
Cons:
Slower - this is often debated, but a comparable rails application is slower than its PHP counterpart. Rails 3 is supposedly going to include speed increases.
Less Literature - rails lacks the plethora of tutorials, books and api that is available for PHP.
Less Hosting Support - Not all shared hosts support rails, but you can set it up on any vps or dedicated. Deployment is also a bit tougher in Rails as opposed to PHP
Ruby is a programming language. Ruby on Rails is a piece of software written in Ruby, it helps you develop web applications.
You can look at Ruby on Rails as if it is a set of tools. It helps you do common tasks when you're making a dynamic web application. E.g. session handling, cookies, database interaction, etc.
Rails goes beyond being just a library, though. To really understand this, you would have to experiment with it, which is actually very easy.
Rails Guides is a fantastic place to start.

Are there any potential disadvantages in using a Ruby framework other than Rails?

I would like to use a lighter framework than Rails (Sinatra/Ramaze/Camping) but am concerned that by doing so I will not be able to use a lot of shared libraries that have been tailored to Rails in the form of plugins. Is this a major concern or are most of these plugins usable across different Ruby frameworks?
Are there any other potential disadvantages in using a Ruby framework other than Rails?
You can still use gems in all of the frameworks you mentioned, so a ton of stuff is reusable. Want to swap in a new ORM, no problems. Want a fancy shmacy syntax highlighting, no problems. Rails has been making a huge push to move away from the old plugin model to use gems exclusively.
If one of the other frameworks fits your needs better use it. Keep in mind that when it comes to documentation and samples rails has more.
If I was learning Ruby and wanted to try out a web framework I would probably go with Rails not because its better, but because its got much better tooling and documentation.
Most Ruby modules used by Rails (even ActiveRecord) can be used without Rails. But then you lose the extra benefit of integration provided by Rails. You may have to work extra hard to glue Ruby modules to the framework of your choice. Please also note that most of the documentation about Ruby modules used by Rails tells you only how to use that module with Rails.
Network effects play a bit of a role.
One issue that comes up when you use other frameworks like sinatra, camping, etc is that rails gives you a proven structure for your files in the your application. Smaller frameworks are quite open and free.
This can be a downside when you are working with multiple developers as you need to have conversations about creating conventions rather then simply following them.
If you've been using Ruby for less than a year, stick to Rails, unless you have a very clear need that is better handled by one of the other frameworks.
The lighter frameworks, most notably Sinatra, tend to be popular with people who know exactly what they will need and can't afford to have any additional overhead from unused code. Essentially, you pick your toolchain, instead of generally being stuck with what Rails gives you. (Yes, in Rails, you can replace ActiveRecord, et al. with other libraries, but it's not exactly easy.) So the lighter frameworks give you significantly more freedom, but you also have quite a bit more work to do in many cases.
I think no rails plugin is going to work out of the box with any of the alternative frameworks, except for ActiveRecord plugins (such as acts_as_nested_set etc.) which are still going to need some plumbing work (setting $LOAD_PATH and requiring right files). I'd recommend DataMapper for ORM, not only it's way faster than ActiveRecord, but it's also very modularly built and plugins are actual gems that you can easily install. In difference, ActiveRecord plugins are mostly monkey-patches that tend to break with every new version.
Sinatra doesn't come with any "goodies", no Rakefiles, no skeletons, no script/generate, but actually that's what it's been written for. You can gradually "plumb in" all the extra stuff. There are also skeletons for sinatra apps that come with some basic layout and defaults, you may find these useful.

How does ruby on rails work?

I'm a PHP developer who knows a little bit of Ruby. I want to learn Ruby on Rails, but most of the resources I've come across treat RoR functionality as "magic" -- i.e., it has a certain internal consistency, but don't bother asking how it works in terms of Ruby, MySQL, etc.
Anyway, I want a deep understanding of how RoR works, the design decisions that went into building it, etc. In particular I'm interested in ActiveRecord, but really I'm looking for the whole package.
Any books / sites / advice welcome.
The books "Agile Web Development with Rails" and "The Rails Way" are both pretty good. "Pro Active Record" goes really in depth for Active Record, but doesn't touch on too much else. The podcast Railscasts sometimes just uses the magic, but sometimes it explains what is really going on. Various blogs such as Art of Mission can get into what your looking for.
Additionally, using the ruby-debug gem gives you a much better understanding of what is going on - you can step into what is running behind the scenes.
There are two areas here that can be examined separately:
General concepts (which include design patterns, general attitude principles like DRY and CoC or even agility etc.) since they are the principles behind many design decisions in Rails. Any of these concepts can be studied independently of Rails (since they are general programming concepts). It's good to have at least some understanding of these before digging any deeper. There are many sources about such general principles all over the net (aforementioned Martin Fowler's site is one of the better sources for grasping such concepts).
The way Rails implements these concepts into its corpus. There are, again, two things to have in mind here. Since Rails heavily exploits features of its mother language, it's crucial to understand Ruby's peculiarities (such as mixins and plenty of Ruby's dynamic features). The last thing to understand is how Rails uses these features, ie. how it's broken down into classes and modules, how many of its features are dynamically created on the run and so on. For this purpose, I highly recommend book Ruby for Rails from David A. Black (which is exactly about Ruby, its peculiarities and the way Rails uses them). While this book might be a bit out of date, I think it's still invaluable in its content.
If you really want to know how it works, you can just look at the source code. The online API docs let you see the source code of every method right in the documentation page.
ActiveRecord in Rails is based on Martin Fowler's Active Record pattern. It's basically an ORM (object-relational mapper). It's not even really that sophisticated as far as ORMs go. Java Persistence with Hibernate has a good overview of common ORM concepts.
This one might fit here well too -> http://railsforphp.com/
This is sort of a tangential answer to your question, but I, too, came from PHP development over to Ruby/Rails dev, and it was a big transition for me. As PHP devs, we're used to getting into the nitty-gritty, to to speak, and not being prevented from looking under the hood.
By design, Rails is a black box. You're supposed to learn Rails, almost as a language itself. It's a new way of thinking of web dev, especially from a PHP dev's perspective, though this idea is not uncommon to programming in general.
Not bothering to look under the hood may be the best way to start doing things "the Rails way."
Jamis Buck has a nice series of "Under the Hood" articles on his blog that cover some aspects of Rails' internals.
The "magic" in Rails involves method_missing and the concept of metaprogramming. Dave Thomas of The Pragmatic Programmers created a set of screencasts on metaprogramming Ruby.
PHP has a somewhat-equivalent function called __call, and PHP 5.3 introduced __callstatic. You might take a look at those, as well as search Google for "php metaprogramming", for some reading material on implementing Rails-like behavior in PHP.
I found this site a good starting reference:
http://www.tutorialspoint.com/ruby-on-rails-2.1/index.htm
It assumes MySQL in its examples.
I am sure you can find heaps and heaps by just googling for "ruby on rails tutorials", though.
ruby metaprogramming magic in rails: look at books by Rappin "Professional Rails" and Ediger "Advanced Rails", in addition to Black "Ruby for Rails" mentioned above
Naming conventions are important:
http://rails.wincent.com/wiki/Rails_conventions
http://www.softiesonrails.com/2007/10/18/ruby-101-naming-conventions
If you really want to understand Rails, read the source for the bit of Rails that you want.
/Library/Ruby/Gems/1.8/gems/ on OSX or freeze rails and have access to it in your RAILS_ROOT/vendor/ directory

Resources