What is Binstub?

While I was installing RSpec I run a command here

$bundle binstub rspec-core

I was wondering, what exactly the binstub is?
And why we have to use it?

Binstub

Binstub is the Binaries require by the executable files. Basically, Binstub prepare the environment around executables before dispatching the call. Ruby might need an extra stuff to their job and here the binstub is use to make sure that the stage is properly set.

let’s go through an example from the Guide

#!/usr/bin/env ruby
require ‘rubygems’

# Prepares the $LOAD_PATH by adding to it lib directories of the gem and
# its dependencies:
gem ‘rspec-core’

# Loads the original executable
load Gem.bin_path(‘rspec-core’, ‘rspec’)

The purpose of every RubyGems binstub is to use RubyGems to prepare the $LOAD_PATH before calling the original executable.

Action Cable(Rails 5)

Action cables

Action cable is introduced in rails5. It uses for Real-time communication.
Now here we discuss how to configure action cables in your websites.

1. Setup Action cable connection
For this, Open app/channels/application_cables/connection.rb

Here we will define 2 methods

class Connection < ActionCable::Connection::Base
#Totally rely on siged in user
identified_by :current_user
def connect
self.current_user= find_current_user
end

def disconnect

end
def find_current_user
if current_user = User.find_by(id: cookies.signed[‘user.id’])
current_user
else
reject_unauthorized_connection
end
end

end
end

Here generally we find the current user from cookies of browser and get the user id

2. Authenticate the user on Web server

Open config/initializers/warden.rb

Warden::Manager.after_set_user do |user, auth, opts|
scope = opts[:scope]
auth.cookies.signed[“#{scope}.id”] = user.id
end

#Delete the cookies when user is signed out
Warden::Manager.before_logout do |user, auth, opts|
scope = opts[:scope]
auth.cookies.signed[“#{scope}.id”] = nil
end

this will perform 2 actions

First, this will set cookies for signed in user and then make it available for the user and secondly this will erase the cookies when user signed out

3. Acces Action cable server through rails server

For this, Open config/application.rb

and add a line under

Class Application <Rails::Application

i.e.

config.action_cable.mount_path=’/cable’

By default action cable works on mount under ‘/cables’

after that, we have to mount this /cable in routes.

4. Open Routes.rb

Add this line in the end

mount ActionCable.server => ‘/cable’

5. Create a messaging

After all this, We have to create a messaging channel.

Here, we are creating channel for comments.

Type

$rails generate channel comments

will create 3 files.

Screenshot from 2017-01-27 12-38-34.png

6. Open comments_channel.rb

Here uncomment the line written in subscribed method

def subscribed

stream_from “comments”

end

I created channel comments so I have replaced stream from comments.

7. Broadcast the comment

Open  comments_controller.rb

here add 2 lines under we save the comment

  if @comment.save

ActionCable.server.broadcast “comments”,
render(partial:’comments/comment’,object: @comment)

flash[:success] = “Comment has been created”

this code will generally  broadcast the comment and rendAer a partial named as comment

8. Create a partial for this

Open views/comments/_comment.html.erb

<%= comment.body %>
<%= time_ago_in_words(comment.created_at) %>
ago by <%= comment.user.email %>

and Add a line where you have created the form for controller

remote: true

like this

my comment form is present in show.html.erb

 <%= form_for [@article,@comment], remote: true, :html => {class: “form-horizontal”, role: “form”} do |f| %>

this will specify we are using Ajax

9.  Update comments.coffee file

open app/assests/javascripts/channels/comments.coffee

add a line

recieved: (data) ->

$(“#messages”).prepend(data)

this will call when there is an upcoming message on the web socket for this channel

in this make sure you have indented the line properly here indentation matters.

now check your app in the browser and if you have any problem leave a comment below.

July 15: Mess with Laptop

Today I thought to deploy my database of gurbani in ruby and rails as it scaffold very neatly and pleasantly as my database is in mysql and the default settings of rails are in sqlite 3 so I started changing that settings. I am facing a lots difficulty as its new for me and in every site there is a unique tutorial someone said you need to install mysql driver as they give the link I tried it but it’s not worth full As I mess up with my laptop by removing and adding new packages related to mysql :p I am feeling like a warrior :p
Then  I took a break and start watching Big Bang Theory Season 5 I really enjoy it.
Then I came back to work
And start searching I got a link they told me the right and easy way to do so.
I follow all the steps but shit my phpmyadmin crashes oh my god how’s that possible I haven’t touched it.
I again give it a break and start reading Agile web development with ruby on rails 4 without wasting time .
Its late night now I think I have to sleep. So lets continue tomorrow good night.

July 14: Magic is here!

Today i woke up early in the morning at 0730. Earlier past days i was suffering from fever but today i feel fresh and at 0800 i started working on rails from agile web development with ruby and rails 4
I read about ORM(Object relational mapping) layers in rails
ORM layer maps tables to classes, rows to object, columns to attributes of the class. Class method are used to prerform table level operation and instance method perform operation on individual row.

Then I read about Ruby names. I am sharing few with you.
Method names- lower case letter with an underscore
Instance variable-@
Class name- use capital letter ex- PurchaseOrder

Redrict_to :action=>”edit”, :id => params[:id]
:action-> thing named action

Then I read about how to create an array in Ruby. Let me share a shortcut with you :p
Shortcut to create an array of words
A=[‘ants’, ‘bee’ , ‘cat’]
#this is the same
A=%w {ant bee cast}

Then By all this my part one of the book is over now its time for the Part 2
In part 2, In this part I am creating a shopping website which has two user roles a buyer and a seller and in this i studied a magical command. As magician never reveled their trick :p same way you just need to use this command
And you will see the magic
Now i am creating a UI for a product details which we store in database and their attributes are
Title
Product description
Image URL
Price
Now the magic is going to happen
Type

$rails generate scaffold product title:string description:text image_url:string price:decimal

Product is the name of table
Now this will create a whole UI to add new item
Modify it
Delete it
Everything is done with this single command
U have to do nothing its like you got everything in a plate now u just have to eat it. Its just a Idiom :p
Its just a trailer for more details must read Agile ruby on rails 4
Seriously Ruby on Rails are very much interesting keep in touch with me to know more magic tricks.
Good nite 🙂