Category: Ruby on Rails

2013 in review

The WordPress.com stats helper monkeys prepared a 2013 annual report for this blog.

Here’s an excerpt:

A New York City subway train holds 1,200 people. This blog was viewed about 5,400 times in 2013. If it were a NYC subway train, it would take about 5 trips to carry that many people.

Click here to see the complete report.

Advertisements

Rails-Dynamic-DropDown

I have found many codes and blog posts for dynamic drop down with mysql in rails. But I want to implement it with mongoid in my application. Many codes that I found are not perfectly working for this. Some gave me problem of non appearing data during edit. So I tried to develop my own dynamic drop-down with mongoid in rails. Finally I become success to implement full featured rails dynamic drop down with mongoid and coffeescript.

Find the source code in github:

https://github.com/gkunwar/dynamic-dropdown

Getting Started

First of all you make sure that mongodb is installed.

Clone

If you want to copy and customize the app with changes that are only useful for your own project, you can clone the GitHub repo. You probably should generate the app instead (see below). To clone:

$ git clone git://github.com/gkunwar/dynamic-dropdown.git

Then install the bundle and run the application.

Ruby Arithmetic Operation for Date

I wanted to get the difference between two dates. My date are in format “DD/MM/YYYY”. I tried to get number of days between two date of same format. Finally I got an easy method to get number of day between these two date.

For this first of all I changed these date to date format using following method.

d1 = “12/2/2012”
d2 = “15/2/2012”
d1 = d1.to_date
d2 = d2.to_date
no_of_days = (d2-d1).to_i

Finally, I get number of days between two date. If there are any better methods for this operation, suggestions are welcome.

Using helper in mode;s, views, and controllers

So far, it\'s RoR

Using this as a guide JLHPBv2: Using Helpers in Models in Rails, you can make a single Module that can be accessed from models, views, and controllers. The helpers automatically created in app/helpers/*_helpers.rb   might also be available anywhere as long as you include them.

I decided to make the methods in the helper class-scoped (using self below) so that the origin of the helper method is explicitly obvious in the call (eg. to_s below)

app/lib/utility_helper.rb
module UtilityHelper
  def self.camelcase_to_spaced(word)
    word.gsub(/([A-Z])/, " \\1").strip
  end
end

app/models/event.rb
class Event < ActiveRecord::Base
  include UtilityHelper
 
  def to_s
    UtilityHelper.camelcase_to_spaced("This an Event object")
  end
end

app/helpers/events_helper.rb
module EventsHelper
  include UtilityHelper
end

app/controllers/event_controller.rb
class EventController < ApplicationController
  include UtilityHelper
end


app/views/events/index.html.erb
<%=UtilityHelper.camelcase_to_spaced(event[:event_type])%&rt;

View original post

Configuring SMTP Setting dynamically

SMTP is mail delivery method in rails  which is used to send email through rails. Setting of SMTP is configured in different environment like development, production and test. For this we configure following setting in different file like config/environments/development.rb, config/environments/production.rb, config/environments/test.rb with respect to different environment.

 config.action_mailer.smtp_settings = {
address: “smtp.gmail.com”,
port: 587,
domain: “mail.google.com”,
user_name: “example@gmail.com”,
password: “********”,
authentication: “plain”,
enable_starttls_auto: true
}

But sometime we need to set up smtp attributes like address, domain, port etc. dynamically on run time then these above method gives some problem. Because file on the cofig folder loaded when we start the server. For this we configure smtp settings in application controller. The configuration is as below.

before_filter :set_mailer_settings

private
def set_mailer_settings
options = Mails.first()
ActionMailer::Base.smtp_settings = {
:address => options[“smtp_address”],
:port => options[“smtp_port”],
:domain => options[“smtp_domain”],
:authentication => options[“authentication_type”],
:user_name => options[“smtp_username”],
:password => options[“smtp_password”],
}
end

Where Mails is a model from which attributes of smtp are ftech. By this way we can configure smtp setting dynamically in rails on run time.

Install and use mongodb with rails

MongoDB is a scalable, high-performance, document-oriented schemaless database. In this post I describe how to install MongoDB in ubuntu 12.04 and how to use mongobd with rails.

Followings are the steps to install MongoDB

1. Add GPG Key
sudo apt-key adv –keyserver keyserver.ubuntu.com –recv 7F0CEB10

2. Edit your source.list
sudo gedit /etc/apt/sources.list

and add this into bottom line :
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

3. Update and install
sudo apt-get update
sudo apt-get install mongodb-10gen

After installing the MongoDB, you have to install and use mongoid gem to use MongoDB with rails. Followings are the steps to use mongoid gem with rails.

1. Add following code in your gem file
gem “mongoid”, “>= 3.0.1”

2. Install bundle
bundle install
Alternatively you can get the Mongoid gem direcly from rubygems.org:
$ gem install mongoid

3. Generate mongoid.yml
You can generate a config file by executing the generator and then editing myapp/config/mongoid.yml to your heart’s desire. Mongoid will then handle everything else from there.
$ rails g mongoid:config

Now your application support mongodb as database.