ReactJS for Beginners | A Step by Step Approach


Bookmark and Share

There are many problems while building large applications with data that changes over time. To solve this ,I suggest checking out ReactJS. React lets you express how your app should look at any given point, and can automatically manage all UI updates when your underlying data changes.

React is one of the most popular JavaScript front end libraries which is developed by Facebook. It’s used for handling view layer for web and mobile apps. The main feature of ReactJS is that it allows us to create reusable UI components.  The syntax used in React is JSX which allows you to mix HTML with JavaScript. This is not a requirement – you can still write in plain JavaScript. But this is suggested because this makes writing your components a breeze.

Installation

To install React with Yarn, run:

To install React with npm, run:

The bundlers like webpack or Browserify is recommended. So you can write modular code and bundle it together into small packages to optimize load time.

Use React with Read More

PStore, a little known feature in the standard library


Bookmark and Share

PStore(persistent store) implements a file based persistence mechanism based on a Hash. It writes Ruby objects to an external file so it can access easily if needed. If an I/O error occurs while PStore is writing to its file, then the file will become corrupted.You can prevent this by setting pstore.ultra_safe = true. Also, it supports thread-safe and uses Marshal internally.

To use this library, you must require it and instantiate a new object.

Which would create a file that stores the content to be written.

To store or retrieve data from the data store, you must open a transaction. Here transaction is a protective wrapper around SQL statements to ensure changes to the database only occur when all actions succeed together. We can access the content of database only through this transaction.

At the end of the transaction, all changes are committed.

Public Instance methods

Instance methods are methods that are called on an instance of a class. We can use the below methods while using PStore instances.

  • p[name]=obj

Stores obj in the database under the key name. When the …

Read More

How to write your own Rack middleware


Bookmark and Share

How to write your own Rack middleware

Rack is a Ruby package which provides an interface for a web server to communicate with the application. It is very easy to add middleware components between the web server and the app to customize the way your request/response behaves. The middleware component sits between the client and the server, processing inbound requests and outbound responses. Rack Middleware is an implementation of the pipeline design pattern for web servers using Rack.

For example with Rack, we can have separate stages of the pipeline:

  • Authentication: Checks whether the login details are correct or not when the request arrives.
  • Authorization:  It performs role-based security. i.e. checks whether the user is authorized to perform the particular task.
  • Caching: Return a cached result if the request is already processed.
  • Decoration: Enhance the request to make downstream processing better.
  • Performance & Usage Monitoring: Status get from the request and response.
  • Execution: actually handle the request and provide a response.

Next, …

Read More

Using gmail to send email in Ruby on Rails


Bookmark and Share

Emails can be sent from you Rails application through many services like mandril, sendgrid, amazon SES. In this article, we would be explaining how we can send email using a Gmail credentials. Even though we can’t use it in production scenario due to the 500 email per day limitation, it would help us in prototyping an application quickly and also for cases where you want to send actual email in your development environment. Action Mailer is the ruby library in rails that will help us to do this.

Action Mailer allows you to send emails from your application using mailer classes and views. Mailers work very similarly to controllers. They inherit from ActionMailer::Base and live in app/mailers, and they have associated views that appear in app/views.To setup action mailer, must do the following:

1. Configuring the mailer in your environment file
2. Generating the mailer
3. Defining mailer action
4. Generating the mailer view
5. Delivering the email

Action Mailer Configuration

To configure action mailer add the following to your appropriate config/environments/$RAILS_ENV.rb file:

Eg:

Generating the mailer

As you …

Read More

Different ways to run Shell Commands in Ruby


Bookmark and Share

During development of an application, there will come cases when we need to access system command from our program itself. In some cases we just need to know if the status was complete, in some cases, we also need to know the output the application returns.But before we start, let’s find out what Shell Commands are. Simply put, the shell is a program that takes your commands from the keyboard and gives them to the operating system to perform. There are several additional shell programs available on a typical Linux system. Described below are 6 different way to execute a shell script from ruby and their properties.

  • Exec
  • System
  • Backticks ()
  • IO#popen
  • open3#popen3
  • open4#popen4
  • Exec

    Kernel#exec (or simply exec) replaces the current process by running the given command, which can take one of the following forms:

    exec(commandline)
    command line string which is passed to the standard shell

    exec(cmdname, arg1, …)
    command name and one or more arguments (no shell)

    exec([cmdname, argv0], arg1, …)
    command name, argv and zero or more arguments (no shell)

    In the first form, the string is taken as a command line that is subject to shell expansion before being executed.

    In the second form …

    Read More

    New binding.irb introduced in ruby 2.4


    Bookmark and Share

    Ruby 2.4 will have the feature to introduce a REPL session, using IRB, in between your code execution for better debugging. IRB, which stands for Interactive Ruby, is the standard REPL which is bundled along with ruby. Pry is a popular alternative for IRB, which has many developer-friendly features like tab compilation and syntax highlighting. One of the most heavily used features of pry is the ability to introduce a REPL session in between your code execution for better debugging. Instead of using p or puts to print the result and various variables, this helps us try out various codes and fixes in between the code to find the right solution.

    binding.pry being used.

     

    To use binding.irb in your code, you need to require the IRB library to your code and call binding.irb where you want to introduce the REPL.

    and you will see a REPL like below.

    screen-shot-2016-11-22-at-10-47-27-am

     

    Read More

    Different types of Index in PostgreSQL


    Bookmark and Share

    This is part two of our PostgreSQL optimization series. You can read the first article where we discuss when to index here.

    PostgreSQL uses a different set of algorithm while indexing tables, each type of algorithm is good for a certain set of data. Here we will be discussing the various algorithms available and when we should be using them. (Note these are the algorithms found in PostgreSQL 9.5)

    Algorithms

    B-Tree (Balance Tree), is the default algorithm used when we build indexes in Rails. It keeps a sorted copy of our column, which would be our index. So if we want to find the row of the word starting with a then as soon as the words starting with a are over. It will stop searching and return null, as the index has kept everything sorted. It is good in most cases, hence it is the default algorithm used.

    Hash is one of the most popular indexing algorithms. But only the equate operator works on it, thus the query planner will only use an index with a hash algorithm if we do an equal operation searching for it. Another point to …

    Read More

    How to write maintainable routes in rails


    Bookmark and Share

    config/routes.rb is the gateway to your ruby on rails application. All request send by your users are directed to the appropriate code by the routes.

    Example:

    When someone visits your-website.com/profiles then the request is taken to the Index action of the UsersController. Under that action you will get the index.html.erb. So using routes we have configured the UsersController to respond to the users request it is its responsibility to do it now.

    We can declare routes in various ways:

    Since there are multiple ways to declare routes (as all forms are right), its best to stick with a single method for the code to be more readable. routes.rb is going to be one of the most heavily edited file in your project as when ever you add a new page or create a new form, you need to add a route to access the page or an end-point to accept the request. So it is most likely that your routes.rb file will start to grow ugly:

    So here we will share some tips to write proper, maintainable routes:

    First important point to note is that, its best to write routes as resources
    eg:

     

    declaring resources …

    Read More

    Mina: Faster deployment and remote server automation


    Bookmark and Share

    Even though we use CodeShip/Circle CI/Jenkins for continuous integration, we still need to write scripts to automate our deployment. We also need to write small commands to clear cache, restart queues, etc. It’s always a good practice to not have to enter the server directly but to have it done through scripts. There are many tools available for this purpose (in multiple languages) like Capistrano, Vlad, etc.

    Mina is a similar tool, but faster. The reason why it runs faster is because it generates a bash script, uploads it to the server and run there, rather than creating a new ssh session and run every command one by one. Mina is one of our default tools at Red Panthers.

    To use mina in your project, add mina to your Gemfile.

    and to get started do

     

    http://nadarei.co/mina/setting_up_a_project.html
    https://www.digitalocean.com/community/tutorials/how-to-deploy-with-mina-getting-started

     

    Having a one step deployment is an important requirement for any project so have one ready using mina or capistrano.

     

    Note:

    Assets pre-compile

     

    Run another rake task

    Run shell commands

    The syntax queue is referring to the fact that, the commands are all made into a single …

    Read More

    application_record.rb available since rails 5


    Bookmark and Share

    Those who have been starting with Rails 5, must have noticed the new application_record.rb file present in your model folder. And all new models seems to be inheriting the application_record.rb instead of the ActiveRecord::Base. This is done so that we don’t pollute the ActiveRecord::Base namespace with our own extensions.  Before when we require something, say an extension to the ActiveRecord itself we used to have it included using the following code.

    Now the problem with this approach is that when we use rails engines this NewFeature gets added in there as well, and it could end up doing things that we didn’t expect.

    With the new application_record.rb, which would be inherited by all the models, we need to include the new module at the ApplicationRecord and it would be available as the new feature of ActiveRecord. Every new engine generated using rails plugin new would also be having their own application_reocord.rb

    One more point to note is that we can place application wide hooks in this file. So if you were to do

    it would be triggered when a new record is created in any of the models of the rails application.

    Read More