Essential & Useful Ruby Gems for Rails 4


Essential & Useful Ruby Gems for Rails 4

Ruby on Rails framework is a powerful tool for building web applications. It is easy to learn and it comes with plenty of built-in functions which help to speed up the development of your web application such as an object-relation mapper and routing. Ruby on Rails uses the MVC pattern to organize application programming. Ruby gems makes development much easier. They are packaging systems designed to facilitate the creation and distribution of Ruby programs and libraries.

Check out some of the best Ruby on Rails gems for Rails 4.

CarrierWave

Using this gem you can upload files in your applications. It store them on different back ends. It supports a variety of ORMs, frameworks and image processors such as ImageMagick and GraphicsMagick. CarrierWave keeps your code clean, reusable and it is easy to test.

To install :

$ gem install carrierwave

To add to your Gemfile:

gem “carrierwave”

To generate an uploder :

class AvatarUploader < CarrierWave::Uploader::Base
storage :file
end

The about command will give you a file in app/uploaders/image_uploader.rb

Sidekiq

There are many gems for background processing, Sidekiq is the one of the most popular one and it uses threads to perform jobs at the same time in the same process and it is compatible with Resque. Sidekiq supports sending emails asynchronously with Action Mailer out of the box. The reason behind its popularity is the simplicity of its API.

Gem Installation : gem install sidekiq

Bullet

Bullet gem is designed to help you increase your application’s performance by reducing the number of queries it makes. It will watch your queries while you develop your application and notify you when you should add eager loading, when you are using eager loading that is not necessary and when you should use counter cache.Best practice is to use Bullet in development mode or staging mode.

Draper

Draper gem provides decorators which bundles view logic in an object oriented fashion. It helps make your views much cleaner and lets you avoid writing helpers
To install Draper gem below line to your Gemfile:

gem “draper”, “~> 1.3”

PaperTrail

paper_trial gem is used to track your model changes for auditing, and it stores the pre change version of model. Also, it does not waste space storing a version of the object as it currently stands, PaperTrail stores the values in the Model Before column. Other auditing plugins store the after column. PaperTrail makes reverting to a previous version easy, PaperTrail can restore three types of associations: has_many_through, has_many, and has_one.

To install Paper Trial gem below line to your Gemfile:

gem “paper_trail”

Active Admin

Active admin is Ruby On Rails plugin. Using this framework you can create an admin interface to manage you data and this gem is highly customizable.it simple for developers to implement beautiful and elegant interfaces with very little effort. To install this gem add line to you gem file and run buldle install, after installing the gem, you need to run the generator. By default this gem is using Devise, and the generator creates an AdminUser model.

gem “activeadmin” # to install gem
rails g active_admin:install   # creates the AdminUser class

Strong parameters

Rails 4 requires you to whitelist input values for your app. This feature is known as Strong Parameters. It adds an extra layer of security that prevents attackers from posting harmful or garbage information to your application.

To install Strong Parameters gem below line to your Gemfile:

gem “strong_parameters”

AxlsxRails

axlsx_rails provides a renderer and a template handler. It adds the: xlsx format and parses .xlsx.axlsx templates. This lets you take all the Axlsx code out of your controller or model and place it inside the template, where view code belongs, axlsx provides three options for initializing a spreadsheet:

1. xlsx_author (String) – The author of the document
2. xlsx_created_at (Time) – Timestamp in the document properties (defaults to current time)
3. xlsx_use_shared_strings (Boolean) – This is passed to the workbook to specify that shared strings should be used when serializing the package.

If you think Ruby on Rails is a great option to build your application, get in touch with us to know how we can help you!

Splitting Monolithic Rails Application to Microservices


As a business gets bigger, they require more functionalities and thus you can’t help but add new models/controller to the existing Rails application and sometimes it becomes a monolith. If you are facing a monolith application which has become unmaintainable and difficult to deploy, you need to know some ways to manage it. If your team has to go through thousands of lines in order to understand the project and to make some changes, it is time to split them.

When to split application

  • When your tests run for more than 20 minutes.
  • Models grown to several hundred or thousands.
  • Independent functionality.
  • When developers cannot work independently to develop/deploy their own modules.

Different ways to split Monolithic application

  • Using microservices.
  • Using ‘faster’ technologies.
  • Add a bunch of servers.

What is microservice                                                                                                                                           Microservices is a strategical way for decomposing a large project into smaller, more manageable pieces. A microservice is a piece of application functionality re-factored out into its own code base, speaking to other microservices over a standard protocol. To accomplish this, first divide your business requirements into related groups like user management logic, advertising logic, and a web user interface.

Why microservice
The first force that led to the surge in microservices was a reaction against normal monolithic architecture. While a monolithic app is One Big Program with many responsibilities, microservice-based applications are composed of smaller programs, each with a single responsibility. This allows teams of devs to work independently on different services. The inherent decoupling also encourages smaller, simpler programs that are easier to understand, so new devs can start contributing more quickly. Finally, since no single program represents the whole of application, services can change direction without massive costs. If new technology becomes available that makes more sense for a particular service, it’s feasible to rewrite just that service. Similarly, since microservices communicate across a different languages, an application can be composed of several different platforms – Ruby, Java, PHP, Node, Go, etc without any issue.

Advantages

  • Modules can be reused.
  • Deployment goes smoothly independent of whole big application.
  • Easy to add features.
  • Reduces testing time to test one module instead of all.
  • Big applications tend to be harder to maintain that the small ones.
  • Having set of small applications allows us to focus on working on isolated parts.
  • We might have more chance of not breaking other parts of the application.
  • Reliability and fault tolerance.
  • Easy to migrate or upgrade.
  • Technology diversity.
  • If any single application function is not working entire application doesn’t goes down.
  • Makes it easier for a new developer to understand the functionality of a service.

Disadvantages
Because everything is now an independent service, you have to handle requests carefully travelling between your modules. There can be a scenario where any of services may not be responding.
Multiple databases and transaction management can be painful.
Productivity will be less till the microservice base is set.

Monolithic v/s Microservices
The below diagram shows the difference between traditional monolithic app and microservices in terms of human resource, time, maintenance and handling complexity of applications.

image - Splitting Monolithic Rails Application to Microservices

How to set up microservices
Strategical how to shrink the monolithic application is to split the layers like presentation, business logic and data access layers. A typical enterprise application consists of at least three different types of components:

  • Presentation layer – Components that handle HTTP requests and implement either a (REST) API or an
    HTML-based web UI. In an application that has a sophisticated user interface.
  • Business logic layer – Components which are the core of the application where business rules are implemented.
  • Data-access layer – Components having access to infrastructure components such as databases and message brokers.

RailsCarma has been implementing Ruby on Rails applications from its nascent stages for development, training, deploying and contributing back to the Rails Community. Through trusted technical expertise and consummate customer service combined to deliver a delightful experience for clients, RailsCarma provide end to end Ruby on Rails Development ,consulting, architecture, management and extension to companies around the globe. Contact us to know more.

Source : Splitting Monolithic Rails Application to Microservices

How to Translate JavaScript Strings in Rails


Rails I18n and elegant message passing to JavaScript

The process of “internationalization” usually means to abstract all strings and other locale specific bits (such as date or currency formats) out of your application. The process of “localization” means to provide translations and localized formats for these bits.

How I18n in Ruby on Rails Works

The limits of my language are the limits of my world. ‒Ludwig Wittgenstein. With over 6,909 distinct languages in the world and most of them differing in so many different ways (e.g. in pluralization rules), it is difficult to provide specific tools for internalization. However, for unrestricted barriers of languages, Rails I18n API focuses on:

  • providing support for English and similar languages out of the box
  • making it easy to customize and extend everything for other languages

As part of this solution, every static string in the Rails framework – e.g. Active Record validation messages, time and date formats – has been internationalized, so localization of a Rails application means “over-riding” these defaults.

Read more on RailsCarma Blog

How to Add Functionality to Ruby Classes with Decorators


How to Add Functionality to Ruby Classes with Decorators

Decorators allow us to add behavior to objects in runtime and don’t affect other objects of the class. Decorators can be applied when you need to dynamically add and remove responsibility to a class. The decorator pattern is a helpful alternative to creating sub-classes. They give additional functionality to a class while still keeping the public API consistent. Let’s look at an example to understand the importance of Ruby Decorators.

consider we have a Tattoo class with a price method that returns 300.

Class Tattoo
def price
300
end
end

Now we will add an extra color as a feature, and the price would be increased by 150

The simplest way is to create a TattooWithColour subclass that returns 450 in the price method.

class TattooWithColour < Tattoo
def price
450
end
end

Next, we need to represent a big tattoo that adds 200 to the price of our tattoos. We can represent this using a BigTattoo subclass of Tattoo.

class BigTattoo < Tattoo
def price
500
end
end

We could also have bigger sized tattoos and they may add further price to our BigTattoo. If we were to consider that these tattoos types could be used with colours, we would need to add BigTattooWithColour and BiggerTattooWithColour subclasses.

With this method, we end up with total of 6 classes. Even Double that the number if you want to represent these combinations with extra designs on tattoo.

Read full article at RailsCarma Blog.