Friday 26 June 2015

Faye Web Socket in Rails

Faye is  general-purpose web-socket it's also provide 2-way communication for client side ( user ) or server side. It is very use-full for sending notification on a given condition without reloading the whole page in browser and also help full for showing progress bar on run time server response simple way to implement this gem into your rails application.
Faye is working on multiple web server like

I am using thin web-server for my application to show notification.

1. First you add this line into you Gemfile 
--------------------------------------------------------

gem 'faye'
gem 'thin' 

----------------------------------------
2. Now ! run bundle install   command for install gem and it's dependency.

3. create a faye.ru file and add given line (a rackup file for run Faye server ).

require 'rubygems'
require 'thin'
require 'faye'

faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
run faye_server


4. Now add line to your application.erb file

<%= javascript_include_tag 'application', "http://localhost:9292/faye.js", 'data-turbolinks-track' => true %>


Now ! adding a method with name broadcast or any name which is subtable for you in websoket.rb (first create websoket.rb file inside initializers ) .

#======confi/initializers/websoket.rb


module Websocket
  def broadcast(channel, msg)
    message = {:channel => channel, :data => msg}
    uri = URI.parse("http://localhost:9292/faye")
    Net::HTTP.post_form(uri, :message => message.to_json)
  end
end



Note: If you want to use this method as a helper method then you add this inside application_helper.rb for view.(I include this method into model for sending notification as per my business logic ) for more detail watch RailsCast 260.

Inside this method channel is use for sending messages on a particular URL and MSG is used for send messages on that particular channel.

example :

#===============include Websocket
 
 
 
Inside Notification.rb model
def send_invitation_notification
   broadcast("/users/#{user.id}", {username: "#{user.full_name }", msg: "Hello you are invited for project--| #{project.name} | please check your mail"})
end


I include Web-socket for access broadcast method in model. Now on browser when we call this method using console then it will get notification but first you have to create a view for this notification and a subscriber inside the view using Faye.Cilent('http://localhost:9292/faye').


Example:


<div id="websocket" style="background-color: #999999;">
</div>
<script>
$(function () {
var faye = new Faye.Client('http://localhost:9292/faye');
        faye.subscribe('/users/<%= current_user.id %>', function (data) {
            $('#websocket').text(data.username + ": " + data.msg);
        });
    });
</script>

5. Now ! Run your faye.ru file using terminal


rackup faye.ru -s thin -E prodcution




6. Open User show page something like http://localhost:3000/users/1

7. Now ! send notification using terminal



u = Notification.first
u.broadcast("/projects/16", {username: "your name ",msg: "Hello you did it "})



For more detail about faye gem see it's official website - http://faye.jcoglan.com/ruby.html


Friday 5 June 2015

Condition on Associations in rails

Unsaved objects and associations

You can manipulate objects and associations before they are saved to the database, but there is some special behavior you should be aware of, mostly involving the saving of associated objects.
You can set the :autosave option on a has_one, belongs_to, has_many, or has_and_belongs_to_many association. Setting it to true will always save the members, whereas setting it to false will never save the members. More details about :autosave option is available at AutosaveAssociation.

One-to-one associations

  • Assigning an object to a has_one association automatically saves that object and the object being replaced (if there is one), in order to update their foreign keys - except if the parent object is unsaved (new_record? == true).
  • If either of these saves fail (due to one of the objects being invalid), an ActiveRecord::RecordNotSaved exception is raised and the assignment is cancelled.
  • If you wish to assign an object to a has_one association without saving it, use the build_association method (documented below). The object being replaced will still be saved to update its foreign key.
  • Assigning an object to a belongs_to association does not save the object, since the foreign key field belongs on the parent. It does not save the parent either.

Collections

  • Adding an object to a collection (has_many or has_and_belongs_to_many) automatically saves that object, except if the parent object (the owner of the collection) is not yet stored in the database.
  • If saving any of the objects being added to a collection (via push or similar) fails, then push returns false.
  • If saving fails while replacing the collection (via association=), an ActiveRecord::RecordNotSaved exception is raised and the assignment is cancelled.
  • You can add an object to a collection without automatically saving it by using the collection.build method (documented below).
  • All unsaved (new_record? == true) members of the collection are automatically saved when the parent is saved.

 

Customizing the query

Associations are built from Relations, and you can use the Relation syntax to customize them. For example, to add a condition:


class Blog < ActiveRecord::Base
  has_many :published_posts, -> { where published: true }, class_name: 'Post'
end
 
 
Inside the -> { ... } block you can use all of the usual Relation methods.
 


Salesforce CRM vs. Zoho: A Comparative Analysis

Introduction: Selecting the right customer relationship management (CRM) software is crucial for businesses seeking to streamline their sal...