Showing posts with label ruby on rails. Show all posts
Showing posts with label ruby on rails. Show all posts

Wednesday, 28 November 2012

Guest user functionality in rails app

Hi,

In many requirements you may be asked to provide guest user functionality in application. Here are the simple steps to make that functionality simple.


rails g migration add_guest_to_users guest:boolean
rake db:migrate


index.html.erb
<p><%= button_to "Guest User!", users_path, method: :post %></p>



layouts/application.html.erb
Logged in as <strong><%= current_user.name %></strong>.
<% if current_user.guest? %>
  <%= link_to "Become a member", signup_path %>
<% else %>
  <%= link_to "Log Out", logout_path %>
<% end %>


user_controller.rb
def create
  @user = params[:user] ? User.new(params[:user]) : User.new_guest
  if @user.save
    current_user.move_to(@user) if current_user && current_user.guest?
    session[:user_id] = @user.id
    redirect_to root_url
  else
    render "new"
  end
end


models/user.rb
class User < ActiveRecord::Base
  has_many :tasks, dependent: :destroy

  attr_accessible :username, :email, :password, :password_confirmation

  validates_presence_of :username, :email, :password_digest, unless: :guest?
  validates_uniqueness_of :username, allow_blank: true
  validates_confirmation_of :password

  # override has_secure_password to customize validation until Rails 4.
  require 'bcrypt'
  attr_reader :password
  include ActiveModel::SecurePassword::InstanceMethodsOnActivation
 
  def self.new_guest
    new { |u| u.guest = true }
  end
 
  def name
    guest ? "Guest" : username
  end
 
  def move_to(user)
    tasks.update_all(user_id: user.id)
  end
end


lib/tasks/guests.rake
namespace :guests do
  desc "Remove guest accounts more than a week old."
  task :cleanup => :environment do
    User.where(guest: true).where("created_at < ?", 1.week.ago).destroy_all
  end
end


By applying above simple steps you can add guest user functionality in existing rails application easily .

Thank You,
Uma Mahesh.

Thursday, 13 September 2012

Generating barcode in ruby on rails

Hi,

In ruby we can generate bar code easily with nice wrapper using barby.


BARBY - The Ruby barcode generator. To know more about barby check the http://toretore.eu/barby/


Here are the simple ruby program to generate barcode in ruby file.

require 'barby'
require 'barby/barcode/code_128'
require 'barby/outputter/png_outputter'

barcode = Barby::Code128B.new('The noise of mankind has become too much')
File.open('code128b.png', 'w'){|f|
  f.write barcode.to_png(:height => 20, :margin => 5)
}



Below is the stepwise instructions to implement barcode in rails application.


Add the below gems to your gem file.


gem 'barby'
gem "has_barcode"


Add the below code to your model to which you would like to generate barcode

class Product < ActiveRecord::Base
  include HasBarcode

  has_barcode :barcode,
    :outputter => :svg,
    :type => :code_39,
    :value => Proc.new { |p| "#{p.id}" }

end



Display the barcode in your view as below

<%= @product.barcode_data.html_safe  %>


By using above simple steps you can generate barcode in rails application.

Thank You,
Uma Mahesh.

Thursday, 16 August 2012

knockout.js in ruby on rails

knockout.js helps you to simplify dynamic JavaScript UIs using the Model-View-ViewModel (MVVM) pattern.

Key features :

1) Easily associate DOM elements with model data using a concise, readable syntax.

2) Pure JavaScript and works with any web framework.

3) When your data model's state changes, your UI updates automatically.

4) Small and Lightweight.

5) Implicitly set up chains of relationships between model data, to transform and combine it.

6) Supports all mainstream browsers.

7) Quickly generate sophisticated, nested UIs as a function of your model data.

Knockout.js follows Model-View-View Model (MVVM) is a design pattern for building user interfaces.

The official website for knockout.js is http://knockoutjs.com/

Coming to using knockout.js in ruby on rails application. After googling I came to see its been using ruby on rails application with the help of gem 'knockout-rails' https://github.com/dnagir/knockout-rails

Here is one of the useful URL which explains the benefits of knockoutjs over backbone.js
URL:http://jeffdeville.com/javascript-development-and-frameworks


some useful links related to knockout.js with rails Implementation

http://obruening.github.com/projects/knockoutjs/help.html

https://github.com/umamahesh/Knockout-On-Rails

Thank You,
Uma Mahesh.

Thursday, 28 June 2012

minimum things that a rails developer should have


This what I think a middle rails developer should have :


1. 2-3 RoR projects experience long enough to find design flaw in the implementation
2. Fluent with one test engine (test-unit, rspec, minitest)
3. Knowing minimal gems (authentication, pagination)
4. Writing idiomatic ruby code
5. Knowing at least 1 thing he doesn't like in RoR

The above points are said by martin one of my linkedin friend.

Thank You,
Uma Mahesh.

David Heinemeier Hansson rails creator

David Heinemeier Hansson rails creator

From Wikipedia, the free encyclopedia

David Heinemeier Hansson (known to the Ruby and ALMS communities as DHH) is a Danish programmer and the creator of the popular Ruby on Rails web development framework and the Instiki wiki. He is also a partner at the web-based software development firm 37signals.

Hansson co-wrote Agile Web Development with Rails with Dave Thomas in 2005 as part of The Facets of Ruby Series. He also co-wrote and Getting Real and Rework with Jason Fried.

Programming
In 1999 Hansson founded and built a Danish online gaming news website and community called Daily Rush, which he ran until 2001..

After attracting the attention of Jason Fried by offering him help with PHP coding, Hansson was hired by Fried to build a web-based project management tool, which ultimately became 37signals' Basecamp Software as a Service product.

To aid the development process, Hansson used the then-obscure Ruby programming language to develop a custom web framework. The web framework he created was later released separately from the project management tool as the open source project Ruby On Rails. In 2005 he was recognized by Google and O'Reilly with the Hacker of the Year award for his creation of Ruby on Rails.


facebook page : http://www.facebook.com/pages/David-Heinemeier-Hansson

official website : http://david.heinemeierhansson.com/

twitter page : https://twitter.com/#!/dhh


Thank You,
Uma Mahesh.