Tutorial links on rails, you can filter by tag.

Feb 2017

Yarn

Fri 17 Start Using Yarn With Rails Today

Features yarn

Yarn is a JavaScript package manager, and a faster alternative to NPM.

It works almost exactly like NPM, but has one advantage - it creates a yarn.lock file which you can check into source control, and make sure you’re running the same version of the packages everywhere.

Oct 2016

If you have been building Rails applications for a while, you have likely noticed a folder called concerns.
This folder gets created inside the app/controllers and app/models directories whenever you generate a new Rails application.
I thought it was useless until recently when we had to make use of it at work.

Sep 2016

Elasticsearch

Tue 06 Full-Text Search in Rails Using Elasticsearch

RubyGems elasticsearch-model elasticsearch-rails searchkick

In this article I'm going to show you how to implement full-text search using Ruby on Rails and Elasticsearch.
Everyone is used nowadays to entering a search term and getting suggestions as well as results with the search term highlighted.

If you misspell what you are trying to search, having auto-correct is also a nice feature, as we can see on websites such as Google or Facebook.

require 'elasticsearch/model'

class Article < ActiveRecord::Base
  # injects the Search method that we were using in our previous controller among others
  include Elasticsearch::Model

  # integrates with ActiveRecord callbacks to index each instance of an article that we save to the database,
  # and it also updates the index if we modify or delete the article from the database
  include Elasticsearch::Model::Callbacks
end

Aug 2016

Sooner or later, every application needs an API. It will need to integrate with your microservices, peer client service, or your new front-end mobile and web apps.

With JSONAPI, and JSONAPI-Resources, building your API platform is easy.
You just add your own application magic. Along with one of these JSONAPI Client Libraries, your application can rule the world.

# app/resources/api/v1/post_resource.rb
class Api::V1::PostResource < Api::V1::ApplicationResource
  attributes :title, :body, :status
  has_many   :comments
  filters    :id, :title
  filter     :status, default: "published,pending"
end

# app/resources/api/v1/comment_resource.rb
class Api::V1::CommentResource < Api::V1::ApplicationResource
  attributes :created_at, :body, :author
  has_one    :post
  filters    :id, :author
end
Rails

Thu 18 Building a Customer Service Chat App with Rails 5 Action Cable and SMS

RubyGems dotenv-rails nexmo Rails5 5.0 Features ActionCable

Rails 5 shipped with some amazing new features like Turbolinks 5 and API mode, but the feature that caught our eye is the new build in WebSockets integration with the ActionCable API.

This new abstraction around WebSockets is baked straight into Rails and it’s perfect for real-time events and two way communication. Today we’re going to have a look at building a customer service experience on top of it and the Nexmo SMS API.

// app/assets/javascripts/channels/messages.js
App.cable.subscriptions.create('MessagesChannel', {
  received: function(data) {
    var list      = $('.numbers');
    var thread    = $('.thread');
    var number    = thread.data('number');
    var latest    = $('.message[data-number="'+data.number+'"]');

    // prepend to message thread
    if (thread.length &&
        data.number == number) thread.prepend(data.html);

    // prepend to list of ongoing threads
    if (list.length) {
      latest.remove();
      list.prepend(data.html);
    }

    $('.message:first').transition('flash');
  }
});

Jul 2016

Rails

Mon 11 How to Test Rails Models with Minitest - Semaphore

RubyGems minitest Rails5 5.0

Minitest is a testing suite for Ruby. It provides a complete suite of testing facilities supporting test-driven development (TDD), behavior-driven development (BDD), mocking, and benchmarking.
It's small, fast, and it aims to make tests clean and readable.

Sqs

Mon 11 Quickly Process API Requests with Shoryuken and SQS

RubyGems aws-sdk shoryuken AWS SQS

processing queued jobs is Shoryuken, which works in conjunction with Amazon’s SQS (Simple Queue Service). This is a basic store of messages that you can process later via Shoryuken workers. These workers then work outside of the main Rails processes.

With SQS and Shoryuken, you create a queue for your workers to use and these workers cycle through the jobs until the queue is empty.

class MyWorker
  include Shoryuken::Worker
  shoryuken_options queue: QUEUE_NAME, auto_delete: true, body_parser: JSON

  def perform(sqs_msg, body)
    id = body.fetch('id')
    flickr = Flickr.new({
      key:"your_key",
      secret:"your_secret",
      token_cache:"token_cache.yml"
    })
    flickr.photos.search(:user_id => id).values_at(0..(5 - 1))
  end
end

Jun 2016

Amazon Simple Email Service (Amazon SES) is a cost-effective email service built on the reliable and scalable infrastructure that Amazon.com developed to serve its own customer base. With Amazon SES, you can send transactional email, marketing messages, or any other type of high-quality content to your customers.

You can also use Amazon SES to receive messages and deliver them to an Amazon S3 bucket, call your custom code via an AWS Lambda function, or publish notifications to Amazon SNS. With Amazon SES, you have no required minimum commitments – you pay as you go and only pay for what you use.

# action_mailer.smtp_settings block should look similar to this
config.action_mailer.smtp_settings = {
  :address => "email-smtp.us-east-1.amazonaws.com",
  :port => 587,
  :user_name => ENV["SES_SMTP_USERNAME"], #Your SMTP user
  :password => ENV["SES_SMTP_PASSWORD"], #Your SMTP password
  :authentication => :login,
  :enable_starttls_auto => true
}
Rails

Sat 18 Creating Rails 5 API only application following JSON:API specification « Simplify d.o.o.

RubyGems active_model_serializers minitest rack-cors Rails5 5.0 Features API-only

This article describes how to build API only Rails application using new Rails 5 --api option. Further, I'll explain how to follow JSON:API specification in your code and how to test your API's.

Also, I'll cover token authentication using some of the new Rails 5 features. All code from this article is available on GitHub.

class UserSerializer < ActiveModel::Serializer
  attributes :id, :full_name, :description, :created_at
  has_many :posts
  link(:self) { user_url(object) }
end
Capybara

Mon 06 Step by Step Guide to Feature Driven Development

RubyGems capybara factory_girl_rails rspec-rails simple_bdd Rails4 4.2

Having seen an introduction to feature-driven development using RSpec and SimpleBDD (part 1 of this series), let's use it to construct a to-do list web app, step-by-step.
This isn't going to be a complicated app, but it will illustrate the process.

May 2016

Rails

Sat 28 Rails Deployment Tutorial

WebServer nginx RubyGems mina RailsServer unicorn Rails4 4.2 Hosting DigitalOcean

Mina is used to handle the Ruby on Rails deployment. It is very fast because it generates a deployment script locally and runs it on the server.
It automatically updates the bundles, migrates the database and generates the assets on the server side and it is clever about only running these tasks when needed.

In the lifetime of every application the time comes for it to be presented to everyone.
That’s why we have to put our application on a special server which is designed for this purpose.

In one word, we need to deploy our application.
In this post you will see how to deploy app with Capistrano 3.

Capybara

Wed 18 Rails: RSpec and Capybara by Example – Codepany Blog

RubyGems capybara rspec-rails

Article presents how to setup and use two popular gems used in tests in Rails: RSpec and Capybara. Post’s example based on TDD approach.

Rails

Fri 13 Sending Web Push Notifications from Rails - rossta.net

RubyGems serviceworker-rails webpush

Web Push notifications are powerful because they allow you to engage with your users even when they're not on your site.

I'm going to share how to I got a working demo of the new Web Push API from a Rails (or any Ruby) web application to push desktop-style notifications through supporting browsers - currently Chrome and Firefox at the time of this writing.

We'll cover the basics of implementing Push yourself though it's interesting to note that third party services are already stepping in to do some of the technical work for you, like Roost, PushCrew, or OneSignal.

Capybara

Mon 02 Understanding Feature Driven Development

Rails4 4.2 RubyGems rspec-rails capybara simple_bdd factory_girl_rails

Apr 2016

Jsonapi resources

Tue 19 Build a JSON API with Rails and JSONAPI Resources - TutorialsForDevs

RubyGems jsonapi-resources Rails4 4.2

Building an API that is JSONAPI spec compliant is a lot of work especially if you need many of the features that it covers.

In this tutorial, to we will use a Rails library called jsonapi-resources to help us quickly build a JSONAPI spec compliant API.

# To create a todo:

POST /todos

Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": {
    "type": "todos",
    "attributes": {
      "title": "Sample title",
      "description": "Sample description"
    }
  }
}

This guide will walk you through creating just about the simplest Angular + Rails application possible.

We’re going to create an Angular app, then create a Rails app, then get the two to talk to each other.

Mar 2016

Vue js

Wed 09 Vue.js and Rails | Dreaming of Code

Front-end framework Vue.js

I hope that this tutorial will show how easy it is to incorporate it into a Rails app and hope that the Rails community adopts the library as well.

This tutorial will demonstrate building a Vue.js and Rails application from scratch.

Feb 2016

Rails

Thu 04 Action Cable and WebSockets: An in-Depth Tutorial

Features ActionCable Rails5 5.0

Mar 2015

State machines can be broken down into states and events. Events control the flow of one state to another.

Each event has a from and a to transition. Only if a transition occurs successfully will an object’s state change. You can manipulate how an event or transition works using callbacks, blocks, and guards.

Jun 2014

Rails

Fri 20 Australian Ruby on Rails developers blog: Variants With Ruby on Rails

RubyGems browser Features Action Pack Variants

Variants allow your app to easily serve different templates in different cases, for example, different templates for phones, tablets and desktops.

In other words, variants allow you to arbitrarily respond with different templates within the same MIME type (e.g. HTML).

class ApplicationController < ActionController::Base
  before_action do
    if browser.tablet?
      request.variant = :tablet
    elsif browser.mobile?
      request.variant = :phone
    end
  end
end
Rails

Sun 01 Action Pack Variants in Rails 4.1 - RichOnRails.com

Features Action Pack Variants