<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Railsstep</title>
 <link href="http://richardsondx.github.com/atom.xml" rel="self"/>
 <link href="http://richardsondx.github.com"/>
 <updated>2013-05-29T14:03:52-07:00</updated>
 <id>http://richardsondx.github.com</id>
 <author>
   <name>Richadson Dackam</name>
   <email>richardsondx@gmail.com</email>
 </author>

 
 <entry>
   <title>How I refactored OrderController</title>
   <link href="http://richardsondx.github.com/2012/08/16/how-i-refactored-ordercontroller"/>
   <updated>2012-08-16T00:00:00-07:00</updated>
   <id>http://richardsondx.github.com/2012/08/16/how-i-refactored-ordercontroller</id>
   <content type="html">&lt;p&gt;This post show how I refactored the OrderController I initially built for Railsview&lt;/p&gt;

&lt;h1 id='before_rawcontroller'&gt;BEFORE:: RAW-CONTROLLER&lt;/h1&gt;

&lt;pre&gt;&lt;code&gt;class OrdersController &amp;lt; ApplicationController

before_filter :assigns_gateway

skip_before_filter  :verify_authenticity_token, :only =&amp;gt; :notify_action

include OrderHelper

	include ActiveMerchant::Billing::Integrations   

	def checkout
		  assigns_gateway
		# CHANGE THESE VARIABLE TO SET UP THE PAYMENT
		@theme = Theme.find(params[:theme])
		price = @theme.price
		# SET UP THE AUTHOR PROFIT PERCENTAGE HERE
		# ===========================
		authorProfitPercentage = 0.75
		# ===========================
		# DO NOT CHANGE THIS
		@current_user = User.find(params[:current_user])
		puts &amp;quot;CURRENT_USER::::: #{@current_user.name}&amp;quot;
		comission = 1 - authorProfitPercentage
		author_profit = price * authorProfitPercentage
		railsview_profit = price * comission
		# ===========================
		# # The Author.Paypal Account Goes Here
		# author_paypal_account = &amp;quot;#{current_user.paypal}&amp;quot;
		author_paypal_account = &amp;quot;rico_1340337222_biz@gmail.com&amp;quot;
		# Marketplace.Paypal Account Goes Here
		railsview_paypal_account = &amp;quot;richar_1340037459_biz@gmail.com&amp;quot;
	
		puts &amp;quot;COMISSION: #{comission}&amp;quot;
		puts &amp;quot;authorProfitPercentage #{authorProfitPercentage}&amp;quot;
		puts &amp;quot;AuthorProfit: #{author_profit}&amp;quot;
		puts &amp;quot;railsviewProfit: #{railsview_profit}&amp;quot;
	
		recipients = [{:email =&amp;gt; author_paypal_account,
		               # # Active this if its a split payement and both primary are false
		               # :amount =&amp;gt; author_profit,
		               :amount =&amp;gt; price,
		               :primary =&amp;gt; true},
		              {:email =&amp;gt; railsview_paypal_account,
		               :amount =&amp;gt; railsview_profit,
		               :primary =&amp;gt; false}
		               ]
		response = @gateway.setup_purchase(
		  :return_url =&amp;gt; &amp;quot;http://localhost:3000&amp;quot;,
		  :cancel_url =&amp;gt; &amp;quot;http://localhost:3000&amp;quot;,
		  :ipn_notification_url =&amp;gt; orders_notify_action_url(
		    :theme =&amp;gt; @theme, 
		    :railsview_profit =&amp;gt; railsview_profit, 
		    :author_profit =&amp;gt; author_profit,
		    :current_user =&amp;gt; @current_user
		    ),
		  :receiver_list =&amp;gt; recipients
		)
	
		# Billing information
		@gateway.set_payment_options(
		:display_options =&amp;gt; {
		  :business_name    =&amp;gt; &amp;quot;Railsview.com || #{@theme.title}&amp;quot;,
		  :header_image_url =&amp;gt; &amp;quot;http://cdn.yourbusiness.com/logo-for-paypal.png&amp;quot;
		},
		:pay_key =&amp;gt; response[&amp;quot;payKey&amp;quot;],
		:receiver_options =&amp;gt; [
		  {
		    :description =&amp;gt; &amp;quot;Your purchase of #{@theme.title} made by #{@theme.user.name}&amp;quot;,
		    :invoice_data =&amp;gt; {
		      :item =&amp;gt; [
		        { :name =&amp;gt; &amp;quot;#{@theme.title} - #{@theme.id}&amp;quot;, :item_count =&amp;gt; 1, :item_price =&amp;gt;price, 			:price =&amp;gt; price},
		      ],
		      :total_shipping =&amp;gt; 0,
		      :total_tax =&amp;gt; 10
		    },
		    :receiver =&amp;gt; { :email =&amp;gt; &amp;quot;receiver1@example.com&amp;quot; }
		  },
		  {
		    :description =&amp;gt; &amp;quot;XYZ Processing fee&amp;quot;,
		    :invoice_data =&amp;gt; {
		      :item =&amp;gt; [{ :name =&amp;gt; &amp;quot;Fee&amp;quot;, :item_count =&amp;gt; 1, :item_price =&amp;gt; 10, :price =&amp;gt; 10 }]
		    },
		    :receiver =&amp;gt; { :email =&amp;gt; &amp;quot;receiver2@example.com&amp;quot; }
		  }
		]
		)	
	
		# redirect the customer to paypal
		redirect_to (@gateway.redirect_url_for(response[&amp;quot;payKey&amp;quot;]))
	end
	
	def notify_action
	  @theme = Theme.find(params[:theme])
	  notify = ActiveMerchant::Billing::Integrations::PaypalAdaptivePayment::Notification.ne				request.raw_post)
	  p &amp;quot;Notification object is #{notify}&amp;quot;
	  if notify.acknowledge
	    p &amp;quot;Transaction ID is #{notify.transaction_id}&amp;quot;
	    p &amp;quot;Notification object is #{notify}&amp;quot;
	    p &amp;quot;Notification item_id is #{notify.item_id}&amp;quot;
	    p &amp;quot;Notification amount is #{notify.amount}&amp;quot;
	    p &amp;quot;Notification status is #{notify.status}&amp;quot;
	    p &amp;quot;Notification invoice is #{notify.invoice}&amp;quot;
	    # @notification = Notification.find_by_txn_id(notify.txn_id) || Notification.create(
	    @notification = OrderNotification.create(
	    :status =&amp;gt; notify.status,
	    :txn_id =&amp;gt; notify.transaction_id,
	    :amount =&amp;gt; notify.amount,
	    :item_id =&amp;gt; &amp;quot;@theme.user.name/Theme_#{@theme.id}&amp;quot;,
	    :invoice =&amp;gt; notify.invoice
	    #  NEED TO ADD ITEM INFORMATION SUCH AS: THEME_ID
	    )
	
	    p &amp;quot;**NOTICE: Istant Payment Notifications details were succesfuly added t@notifiation&amp;quot;
	
	    # WONCE notifce.status == SUCCESS
	    if @notification.status == &amp;quot;COMPLETED&amp;quot;
	      # SEND A RANDOM GENERATED LINK TO THE USER EMAIL ADDRESS
	      # The link should have a counter to only work 3 times.
	
	      puts &amp;quot;THEME PURCHASED TABLE:&amp;quot;
	      puts &amp;quot;@theme.purchased = #{@theme.purchased}&amp;quot;
	      # The incrementaiton should then be visible on the landing page
	
	      # Add details to the purchase table
	      @current_user = User.find(params[:current_user])
	      @purchased = Purchase.create!(
	        :author =&amp;gt; @theme.user.name,
	        :buyer =&amp;gt; @current_user.name,
	        :theme =&amp;gt; &amp;quot;#{@theme.title}# id:#{@theme.id}&amp;quot;,
	        :price =&amp;gt; @theme.price,
	        :railsview_profit =&amp;gt; params[:railsview_profit],
	        :author_profit =&amp;gt; params[:author_profit]
	      )
	      puts &amp;quot;AUTHOR PROFIT:&amp;quot;
	      puts &amp;quot;The details were added to the Purchase table:&amp;quot;
	      puts @purchased
	
	      # ADD +1 to the specific THEME_ID PURCHASED COLUMN
	      Theme.increment_counter(:purchased, @theme.id)
	
	    else
	      puts &amp;quot;The transaction wasn&amp;#39;t completed&amp;quot;
	    end
	  end
	
		  render :nothing =&amp;gt; true
	end
	
	private
	  def assigns_gateway
	    @gateway = ActiveMerchant::Billing::PaypalAdaptivePayment.new(
	      :login =&amp;gt; &amp;quot;APP_PAYPAL_EMAIL&amp;quot;,
	      :password =&amp;gt; &amp;quot;APP_PASSWORD&amp;quot;,
	      :signature =&amp;gt; &amp;quot;APP_SIGNATURE&amp;quot;,
	      :appid =&amp;gt; &amp;quot;APP_ID&amp;quot;,
	      # :email =&amp;gt; &amp;quot;#{current_user.paypal}&amp;quot;
	      :email =&amp;gt; &amp;#39;AUTHOR_EMAIL_ADDRESS&amp;#39;
	    )
	  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;h1 id='after_refactored'&gt;AFTER:: REFACTORED&lt;/h1&gt;

&lt;p&gt;Once I refactored my controllers even a baby could read it :)&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class OrdersController &amp;lt; ApplicationController
  include OrderHelper
  include ActiveMerchant::Billing::Integrations   

  before_filter :get_buyer_info, :get_theme, :initializer
  skip_before_filter  :verify_authenticity_token, :only =&amp;gt; :notify_action

  def initializer
      app_paypal_email(&amp;quot;APP_PAYPAL_EMAIL_HERE&amp;quot;) # example_biz_api1.gmail.com&amp;quot;
      setup_seller_profit(75)
      setup_receivers(&amp;quot;PRIMARY_RECEIVER_HERE&amp;quot;, &amp;quot;SECONDARY_RECEIVER_HERE&amp;quot;)
      get_buyer_info
      get_theme
      assigns_gateway
  end

  def checkout
    initializer
  
    recipients = [{:email =&amp;gt; @seller,
                   # # Active this if its a split payement and both primary are false
                   # :amount =&amp;gt; @author_profit,
                   :amount =&amp;gt; @theme.price,
                   :primary =&amp;gt; true},
                  {:email =&amp;gt; @railsview,
                   :amount =&amp;gt; @railsview_profit,
                   :primary =&amp;gt; false}
                   ]
    response = @gateway.setup_purchase(
                :return_url =&amp;gt; &amp;quot;http://localhost:3000&amp;quot;,
                :cancel_url =&amp;gt; &amp;quot;http://localhost:3000&amp;quot;,
                :ipn_notification_url =&amp;gt; orders_notify_action_url(
                  :theme =&amp;gt; @theme, 
                  :current_user =&amp;gt; @current_user
                  ),
                :receiver_list =&amp;gt; recipients
                )
  
    setup_billing_information

    redirect_to (@gateway.redirect_url_for(response[&amp;quot;payKey&amp;quot;]))
  end

  def notify_action
    notify = ActiveMerchant::Billing::Integrations::PaypalAdaptivePayment::Notification.new(		request.raw_post)
    p &amp;quot;Notification object is #{notify}&amp;quot;
    if notify.acknowledge
      get_notification
    end

    def get_notification
      p &amp;quot;Transaction ID is #{notify.transaction_id}&amp;quot;
      p &amp;quot;Notification object is #{notify}&amp;quot;
      p &amp;quot;Notification item_id is #{notify.item_id}&amp;quot;
      p &amp;quot;Notification amount is #{notify.amount}&amp;quot;
      p &amp;quot;Notification status is #{notify.status}&amp;quot;
      p &amp;quot;Notification invoice is #{notify.invoice}&amp;quot;

      @notification = OrderNotification.create(
      :status =&amp;gt; notify.status,
      :txn_id =&amp;gt; notify.transaction_id,
      :amount =&amp;gt; notify.amount,
      :item_id =&amp;gt; &amp;quot;@theme.user.name/Theme_#{@theme.id}&amp;quot;,
      :invoice =&amp;gt; notify.invoice
      )

      p &amp;quot;**NOTICE: Istant Payment Notifications details were succesfuly added to @notifiation&amp;quot;

      if @notification.status == &amp;quot;COMPLETED&amp;quot;
        is_completed
      else
        puts &amp;quot;The transaction wasn&amp;#39;t completed&amp;quot;
      end
    end

    def is_completed

        # Add details to the purchase table
          @current_user = User.find(params[:current_user])
          @purchased = Purchase.create!(
          :author =&amp;gt; @theme.user.name,
          :buyer =&amp;gt; @current_user.name,
          :theme =&amp;gt; &amp;quot;#{@theme.title}# id:#{@theme.id}&amp;quot;,
          :price =&amp;gt; @theme.price,
          :author_profit =&amp;gt; @author_profit,
          :railsview_profit =&amp;gt; @railsview_profit
        )

        print_purchase_details(@author_profit)
        increment_theme_purchase(@theme.id)
    end

    render :nothing =&amp;gt; true
  end

  def print_purchase_details(author_profit)
    puts &amp;quot;AUTHOR PROFIT: #{author_profit}&amp;quot;
    puts &amp;quot;The details were added to the Purchase table:&amp;quot;
    puts @purchased.to_yaml
  end

  def setup_billing_information
    @gateway.set_payment_options(
      :display_options =&amp;gt; {
        :business_name    =&amp;gt; &amp;quot;Railsview.com || #{@theme.title}&amp;quot;,
        :header_image_url =&amp;gt; &amp;quot;http://cdn.yourbusiness.com/logo-for-paypal.png&amp;quot;
      },
    :pay_key =&amp;gt; response[&amp;quot;payKey&amp;quot;],
    :receiver_options =&amp;gt; [
      {
        :description =&amp;gt; &amp;quot;Your purchase of #{@theme.title} made by #{@theme.user.name}&amp;quot;,
        :invoice_data =&amp;gt; {
          :item =&amp;gt; [
            { :name =&amp;gt; &amp;quot;#{@theme.title} - #{@theme.id}&amp;quot;, :item_count =&amp;gt; 1, :item_price =&amp;gt; @theme	.	price, :price =&amp;gt; @theme.price},
          ],
          :total_shipping =&amp;gt; 0,
          :total_tax =&amp;gt; 10
        },
        :receiver =&amp;gt; { :email =&amp;gt; @seller }
      },
      {
        :description =&amp;gt; &amp;quot;XYZ Processing fee&amp;quot;,
        :invoice_data =&amp;gt; {
          :item =&amp;gt; [{ :name =&amp;gt; &amp;quot;Fee&amp;quot;, :item_count =&amp;gt; 1, :item_price =&amp;gt; 10, :price =&amp;gt; 10 }]
        },
        :receiver =&amp;gt; { :email =&amp;gt; @railsview }
      }
    ]
    )
  end

  private

    def assigns_gateway
      @gateway = ActiveMerchant::Billing::PaypalAdaptivePayment.new(
        :login =&amp;gt; @app_paypal,
        :password =&amp;gt; &amp;quot;=PASSWORD&amp;quot;,
        :signature =&amp;gt; &amp;quot;APPLICATION_SIGNATURE&amp;quot;,
        :appid =&amp;gt; &amp;quot;APPLICATION_ID&amp;quot;,
        # :email =&amp;gt; &amp;quot;#{current_user.paypal}&amp;quot;
        :email =&amp;gt; @seller
      )
    end

    def get_buyer_info
      @current_user = User.find(params[:current_user])  
    end

    def get_theme
      @theme = Theme.find(params[:theme])
    end

    def setup_seller_profit(profit_in_percentage)
      seller_profit = profit_in_percentage/100
      comission = 1 - seller_profit
      @railsview_profit =  @theme.price * comission
      @author_profit = @theme.price * seller_profit
    end
  
    def print_profit_rate
      puts &amp;quot;COMISSION: #{comission}&amp;quot;
      puts &amp;quot;authorProfitPercentage #{seller_profit}&amp;quot;
      puts &amp;quot;AuthorProfit: #{@author_profit.to_f}&amp;quot;
      puts &amp;quot;railsviewProfit: #{@railsview_profit}&amp;quot;
    end

    def setup_receivers(primary_receiver, secondary_receiver)
      @seller = primary_receiver
      @railsview = secondary_receiver
    end

    def app_paypal_email(email)
      @app_paypal = email
    end

    def increment_theme_purchase(theme_id)
      Theme.increment_counter(:purchased, theme_id)
    end

end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In my next post I will share how I built the TEST for the OrderController in RSPEC.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Adding Google Map to your App</title>
   <link href="http://richardsondx.github.com/2012/08/10/adding-google-map-to-your-app"/>
   <updated>2012-08-10T00:00:00-07:00</updated>
   <id>http://richardsondx.github.com/2012/08/10/adding-google-map-to-your-app</id>
   <content type="html">&lt;p&gt;You&amp;#8217;ll need the gem &lt;code&gt; Geocoder &lt;/code&gt; in order to use google api location service. At least its the one I&amp;#8217;m using here and I like it; its pretty simple to use. Check the documentation of the gem here &lt;a href='https://github.com/alexreisner/geocoder'&gt;GEM GEOCODER&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There is also a railscast on it: &lt;a href=''&gt;Railscast #273&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The official page of the gem is &lt;a href='http://www.rubygeocoder.com/'&gt;www.rubygeocoder.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I write this blogs for coder that already have a good understanding of ruby and rails. So I do not explain every single steps in details.&lt;/p&gt;

&lt;p&gt;This is some of the sourcecode for an app I built called Wherely. User would ask question such as &amp;#8220;Where can I go swimming in toronto&amp;#8221;; and people would answer location that would then be displayed on a map.&lt;/p&gt;

&lt;p&gt;Gemfile&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;	gem &amp;#39;geocoder&amp;#39;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Schema&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;	Answer Model Migration
	  create_table &amp;quot;answers&amp;quot;, :force =&amp;gt; true do |t|
	    t.string   &amp;quot;location&amp;quot;
	    t.string   &amp;quot;longitude&amp;quot;
	    t.string   &amp;quot;latitude&amp;quot;
	    t.integer  &amp;quot;post_id&amp;quot;
	    t.datetime &amp;quot;created_at&amp;quot;
	    t.datetime &amp;quot;updated_at&amp;quot;
	  end
	
	
	Post Model Migration
	  create_table &amp;quot;posts&amp;quot;, :force =&amp;gt; true do |t|
	    t.string   &amp;quot;content&amp;quot;
	    t.datetime &amp;quot;created_at&amp;quot;
	    t.datetime &amp;quot;updated_at&amp;quot;
	  end
	
	
	User Model Migration
	  create_table &amp;quot;users&amp;quot;, :force =&amp;gt; true do |t|
	    t.string   &amp;quot;provider&amp;quot;
	    t.string   &amp;quot;uid&amp;quot;
	    t.string   &amp;quot;name&amp;quot;
	    t.datetime &amp;quot;created_at&amp;quot;
	    t.datetime &amp;quot;updated_at&amp;quot;
	  end
	end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;views &amp;gt; posts&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;	&amp;lt;% title &amp;quot;Wherely ##{@post.id}&amp;quot; %&amp;gt;
	&amp;lt;p&amp;gt;
	  &amp;lt;strong&amp;gt;What I want to do:&amp;lt;/strong&amp;gt;
	  &amp;lt;%= @post.content %&amp;gt;
	&amp;lt;/p&amp;gt;
	&amp;lt;hr&amp;gt;
	&amp;lt;p&amp;gt;
	  &amp;lt;%= link_to &amp;quot;Edit&amp;quot;, edit_post_path(@post) %&amp;gt; |
	  &amp;lt;%= link_to &amp;quot;Destroy&amp;quot;, @post, :confirm =&amp;gt; &amp;#39;Are you sure?&amp;#39;, :method 		=&amp;gt; :delete %&amp;gt; |
	  &amp;lt;%= link_to &amp;quot;Go Back&amp;quot;, posts_path %&amp;gt;
	&amp;lt;/p&amp;gt;
	&amp;lt;hr&amp;gt;
	&amp;lt;h2&amp;gt;Where you can do it:&amp;lt;/h2&amp;gt;
	&amp;lt;%= form_for([@post, @post.answers.build]) do |f| %&amp;gt;
	  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;
	   Share a location:&amp;lt;br /&amp;gt;
	    &amp;lt;%= f.text_field :location %&amp;gt;
	  &amp;lt;/div&amp;gt;
	  &amp;lt;div class=&amp;quot;actions&amp;quot;&amp;gt;
	    &amp;lt;%= f.submit %&amp;gt;
	  &amp;lt;/div&amp;gt;
	&amp;lt;% end %&amp;gt;
	&amp;lt;% @post.answers.each do |post| %&amp;gt;
	  &amp;lt;% unless post.id.nil? %&amp;gt;
	  &amp;lt;p&amp;gt;
	    &amp;lt;b&amp;gt;Map:&amp;lt;/b&amp;gt;
	   &amp;lt;%= image_tag &amp;quot;http://maps.googleapis.		com/maps/api/staticmap?size=350x350&amp;amp;sensor=false&amp;amp;zoom=16&amp;amp;		markers=#{post.latitude}%2C#{post.longitude}&amp;quot; %&amp;gt;
	  &amp;lt;/p&amp;gt;
	  &amp;lt;p&amp;gt;
	    &amp;lt;b&amp;gt;Answer:&amp;lt;/b&amp;gt;
	    &amp;lt;%= post.location%&amp;gt;
	  &amp;lt;/p&amp;gt;
	  &amp;lt;%= link_to &amp;quot; ↑ Destroy this answer #{post.id}&amp;quot;, [@post, post], 		:confirm =&amp;gt; &amp;#39;Are you sure?&amp;#39;, :method =&amp;gt; :delete %&amp;gt;
	  &amp;lt;% end %&amp;gt;
	&amp;lt;% end %&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;models &amp;gt; answer&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;	class Answer &amp;lt; ActiveRecord::Base
	  validates :location, :presence =&amp;gt; true
	
	  attr_accessible :location, :longitude, :latitude, :post_id
	  belongs_to :posts
	
	  geocoded_by :location
	  after_validation :geocode
	end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;models &amp;gt; post&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;	class Post &amp;lt; ActiveRecord::Base
	  validates :content, :presence =&amp;gt; true, :length =&amp;gt; { :minimum =&amp;gt; 5 }
	  attr_accessible :content
	  has_many :answers
	end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;controllers&amp;gt; answers_controller.rb&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;	#Answer is nested under Post model
	
	class AnswersController &amp;lt; ApplicationController
	  def index
	    @post = Post.find(params[:post_id])
	    @answers = @post.answers
	  end
	
	  def show
	    @post = Post.find(params[:post_id])
	    @answer = @post.answers.find(params[:id])
	  end
	
	  def new
	    @post = Post.find(params[:post_id])
	    @answer = @post.answers.new
	  end
	
	  def create
	    @post = Post.find(params[:post_id])
	    @answer = @post.answers.new(params[:answer])
	    if @answer.save
	      redirect_to [@post, @answer], :notice =&amp;gt; &amp;quot;Successfully created 		answer.&amp;quot;
	    else
	      render :action =&amp;gt; &amp;#39;new&amp;#39;
	    end
	  end
	
	  def edit
	    @post = Post.find(params[:post_id])
	    @answer = @post.answers.find(params[:id])
	  end
	
	  def update
	    @post = Post.find(params[:post_id])
	    @answer = @post.answers.find(params[:id])
	    if @answer.update_attributes(params[:answer])
	      redirect_to [@post, @answer], :notice  =&amp;gt; &amp;quot;Successfully 		updated answer.&amp;quot;
	    else
	      render :action =&amp;gt; &amp;#39;edit&amp;#39;
	    end
	  end
	
	  def destroy
	    @post = Post.find(params[:post_id])
	    @answer = @post.answers.find(params[:id])
	    @answer.destroy
	    redirect_to post_answers_url(@post), :notice =&amp;gt; &amp;quot;Successfully 		destroyed answer.&amp;quot;
	  end
	end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;#8217;s it! Let me know if you have any questions. It&amp;#8217;s pretty simple to use and I wanted to show how here.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Rake Task Omniauth Identity + carrierwave</title>
   <link href="http://richardsondx.github.com/rake/2012/08/08/rake-task-omniauth-identity--carrierwave"/>
   <updated>2012-08-08T00:00:00-07:00</updated>
   <id>http://richardsondx.github.com/rake/2012/08/08/rake-task-omniauth-identity--carrierwave</id>
   <content type="html">&lt;p&gt;I often use &lt;code&gt;rake db:populate&lt;/code&gt; in development mode to populate my database with random data. It’s a useful too to test your website and see how it would look like when you have a lot of users. It’s also extremely useful for testing paginations. On Railsview.com I build a script that auto generate random user using the Omniauth Identity gem and then generate random user’s theme that each have an image using the carrierwave gem.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;namespace :db do
	require ‘Faker’
	require ‘omniauth’
	desc “Fill database with sample data”
	task :populate =&amp;gt; :environment do
		Rake::Task[&amp;#39;db:reset&amp;#39;].invoke
	make_users
	end
end

def make_users

20.times do |n|
	name = Faker::Name.name
	email = “test-#{n+1}@test.com”
	password = “password”
	
	# CREATE USERS: Populate the database with random users
	Identity.create! do |user|
		user.name = name
		user.email = email
		user.password_digest = BCrypt::Password.create(password)
	end

	hash = OmniAuth::AuthHash.new(
		:info =&amp;gt; {
		:name =&amp;gt; name,
		:email =&amp;gt; email,
		}
	)

	user = User.create_from_omniauth(hash)
	
	# CREATE THEMES: Populate the database with random themes that each users own.
	theme = user.themes.new
	@uploader = ImageUploader.new(theme, :image)
	
	2.times do |a|
		@uploader.cache!(File.open(“app/assets/images/sample_themes/sample#{rand(1..10)}.png”))
		theme = user.themes.create!(
			:title =&amp;gt; “#{a}Capadona#{n+1}”,
			:image =&amp;gt; @uploader,
			:price =&amp;gt; rand(1..100),
			:description =&amp;gt; “lorem ipsum efeeffaf”
		)
	end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;end&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Nested Resources</title>
   <link href="http://richardsondx.github.com/2012/08/08/nested-resources"/>
   <updated>2012-08-08T00:00:00-07:00</updated>
   <id>http://richardsondx.github.com/2012/08/08/nested-resources</id>
   <content type="html">&lt;p&gt;Making association and link with path sound easy, but I’ve found doing it pretty complex. I had an hard time understanding nesting resources. Many tutorial assume that it is easy to grasp and assume that you’ve already a good understandings of how routes work on rails but I found this part pretty challenging. Here a few references that I found very helpful to understand those concepts.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href='http://railsforzombies.org/'&gt;Rails for Zombies&lt;/a&gt; , many people suggest this tutorial to get onto rails but I would suggest Michael Harlt book then The Pragmatic Agile Developer to get onto Rails and once you got a good grasp of those concept to watch Rails for Zombies to see if you fully understood the concepts.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;&lt;a href='http://guides.rubyonrails.org/'&gt;The Ruby on Rails Guide&lt;/a&gt; was very helpful guide to get onto the basic concept of nested resources after you are making association but honestly it didn’t really help me to fully grasp the subject.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;&lt;a href='http://weblog.jamisbuck.org/2007/2/5/nesting-resources'&gt;The {Buckblog :here}&lt;/a&gt; Good, especially for understanding path and url in rails.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;&lt;a href='http://tr3w.com/'&gt;Book “The Rails 3 Way”&lt;/a&gt; is absolutely a great reference for understanding many concept of Rails and the architecture of your app folder and how methods works. Definitely buy this book and put it right next to your keyboard as a reference book&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hope you find these list helpful, feel free to comment with more links.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Installing Rmagick ImageMagick on Mac OS 10.7 Lion can be a PITA</title>
   <link href="http://richardsondx.github.com/2012/08/08/installing-rmagick-imagemagick-on-mac-os-107-lion-can-be-a-pita"/>
   <updated>2012-08-08T00:00:00-07:00</updated>
   <id>http://richardsondx.github.com/2012/08/08/installing-rmagick-imagemagick-on-mac-os-107-lion-can-be-a-pita</id>
   <content type="html">&lt;p&gt;Rmagick is an interface between the Ruby programming language and ImageMagick/GraphicsMagick.&lt;/p&gt;

&lt;p&gt;In orde rot install Rmagick YOU’LL NEED ImageMagick pre-instaleld on your machine. Most tutorial will tell you that you just need to type “brew install ImageMagick” then “gem install rmagick” but this option tend to result in an error or bugs. I tried a few method and the one that ended up being my favorite one was cloning the Imagemagick package on my machine and installing it locally via ssh. Thanks to a shell script I was able to Install ImageMagick on Snow Leopard! Here are the directions:&lt;/p&gt;

&lt;p&gt;First you need to download the script. If you have git installed…&lt;/p&gt;

&lt;p&gt;&lt;code&gt; cd ~/src git clone git://github.com/masterkain/ImageMagick-sl.git cd ImageMagick-sl sh install_im.sh &lt;/code&gt;&lt;/p&gt;

&lt;p&gt;At one point, it runs a command using sudo, so it will ask for your password. After the script has finished, ImageMagick will be installed. Now, to install the RMagick gem…&lt;/p&gt;

&lt;p&gt;&lt;code&gt; sudo gem install rmagick  &lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That’s it!&lt;/p&gt;

&lt;p&gt;All the credit goes to Andrew on StackOverflow. Thanks to his post I was able to find a working solution.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>How to build your own gems</title>
   <link href="http://richardsondx.github.com/2012/08/08/how-to-build-your-own-gems"/>
   <updated>2012-08-08T00:00:00-07:00</updated>
   <id>http://richardsondx.github.com/2012/08/08/how-to-build-your-own-gems</id>
   <content type="html">&lt;p&gt;Gems are a pretty cool way to share ruby code. I have a lot of fun building my own gems. Here some details on what I did and a useful link to learn how to do the same..&lt;/p&gt;

&lt;p&gt;From the &lt;a href='http://guides.rubygems.org/make-your-own-gem/'&gt;RubyGem Guide&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;Here the most important details you’ll need to make your gem:&lt;/p&gt;

&lt;p&gt;First, start by creating an account on RubyGems.org - http://rubygems.org/sign_up&lt;/p&gt;

&lt;p&gt;Let’s assume that your gem name is “my_totokudo”&lt;/p&gt;

&lt;p&gt;Create your gem folder as follow:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ my_totokudo

$ \lib\my_totokudo.rb

$ my_totokudo.gemspec&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt; my_totokudo.rb &lt;/code&gt; is in the subfolder “lib” of your folder “my_totokudo”&lt;/p&gt;

&lt;p&gt;Your &lt;code&gt; my_totokudo.rb &lt;/code&gt; contains the code of your gem and the gemspec usually contain the specification of your gems, here something you can copy and use. Make sure to edit it as per your need:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Gem::Specification.new do |s|
  s.name        = &amp;#39;my_string_extend&amp;#39;
  s.version     = &amp;#39;0.0.1&amp;#39;
  s.date        = &amp;#39;2012-02-13&amp;#39;
  s.summary     = &amp;quot;Short summary&amp;quot;
  s.description = &amp;quot;Description of your gem.&amp;quot;
  s.authors     = [&amp;quot;Your Name&amp;quot;]
  s.email       = [&amp;quot;Your@Email.here&amp;quot;]
  s.homepage    = &amp;quot;http://twitter.com/richardsondx&amp;quot;
  s.files       = [&amp;quot;lib/my_totokudo.rb&amp;quot;]
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Before you install and push your gem you need to make sure that the name of your gem isn’t in use already:&lt;/p&gt;

&lt;p&gt;&lt;code&gt; $ gem list -r my_totokudo &lt;/code&gt; Then you want to make sure that you have the last version of Rubygems installed: &lt;code&gt; $ gem update --system &lt;/code&gt; Once you have all the specification of your gem and the .gemspec file ready you can build your gem. In the folder my_totokudo where the .gemspec is located Type: &lt;code&gt; $ gem build my_totokudo.gemspec &lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then install it locally to test it if needed:&lt;/p&gt;

&lt;p&gt;&lt;code&gt; $ gem install ./my_string_extend-0.0.1.gem &lt;/code&gt;&lt;/p&gt;

&lt;p&gt;to test it you&amp;#8217;ll have to prompt the testing command. Depending of your code and gem, what you may need to test it may varies. To launch the prompt type:&lt;/p&gt;

&lt;p&gt;&lt;code&gt; $ irb --simple-prompt &lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then require your gem:&lt;/p&gt;

&lt;p&gt;&lt;code&gt; &amp;gt;&amp;gt; require &amp;#39;my_totokamo&amp;#39; &lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Do whatever test you need to do here. To exit the prompt type &amp;#8220;quit&amp;#8221;. Publishing your gem is very simple. You just have to type:&lt;/p&gt;

&lt;p&gt;&lt;code&gt; $ gem push my_totokamo-0.0.1.gem &lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And follow the instruction on the screen.&lt;/p&gt;

&lt;p&gt;If you build exciting Gems please share it with me by sending a mention and a link to your gem on twitter @Richardsondx&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>How I set up Paypal Adaptive Payment for Railsview.com</title>
   <link href="http://richardsondx.github.com/ecommerce/2012/08/08/how-i-set-up-paypal-adaptive-payment-for-railsviewcom"/>
   <updated>2012-08-08T00:00:00-07:00</updated>
   <id>http://richardsondx.github.com/ecommerce/2012/08/08/how-i-set-up-paypal-adaptive-payment-for-railsviewcom</id>
   <content type="html">&lt;p&gt;Before you start, you need to create a developer account on &lt;a href='https://www.x.com/'&gt;X.commerce an Ebay Company that own Paypal&lt;/a&gt; and have set up a test user in your paypal sandbox account. One for the merchant, and one for the seller. These account will be use to test this payment here. here railsview_paypal_account@gmail.com will be the sample email I will use to represent the merchant and author_paypal_account@gmail.com is the sample email I will use to represent the seller. This payment model is a chained payment which mean that there will be more than one recipient for the amount of the sale.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;	class OrdersController &amp;lt; ApplicationController
	  include OrderHelper
	  include ActiveMerchant::Billing::Integrations   
	
	  before_filter :get_buyer_info, :get_theme, :initializer
	  skip_before_filter  :verify_authenticity_token, :only =&amp;gt; :notify_action
	
	  def initializer
	      app_paypal_email(&amp;quot;APP_PAYPAL_EMAIL_HERE&amp;quot;) # example_biz_api1.gmail.com&amp;quot;
	      setup_seller_profit(75)
	      setup_receivers(&amp;quot;PRIMARY_RECEIVER_HERE&amp;quot;, &amp;quot;SECONDARY_RECEIVER_HERE&amp;quot;)
	      get_buyer_info
	      get_theme
	      assigns_gateway
	  end
	
	  def checkout
	    initializer
	  
	    recipients = [{:email =&amp;gt; @seller,
	                   # # Active this if its a split payement and both primary are false
	                   # :amount =&amp;gt; @author_profit,
	                   :amount =&amp;gt; @theme.price,
	                   :primary =&amp;gt; true},
	                  {:email =&amp;gt; @railsview,
	                   :amount =&amp;gt; @railsview_profit,
	                   :primary =&amp;gt; false}
	                   ]
	    response = @gateway.setup_purchase(
	                :return_url =&amp;gt; &amp;quot;http://localhost:3000&amp;quot;,
	                :cancel_url =&amp;gt; &amp;quot;http://localhost:3000&amp;quot;,
	                :ipn_notification_url =&amp;gt; orders_notify_action_url(
	                  :theme =&amp;gt; @theme, 
	                  :current_user =&amp;gt; @current_user
	                  ),
	                :receiver_list =&amp;gt; recipients
	                )
	  
	    setup_billing_information
	
	    redirect_to (@gateway.redirect_url_for(response[&amp;quot;payKey&amp;quot;]))
	  end
	
	  def notify_action
	    notify = ActiveMerchant::Billing::Integrations::PaypalAdaptivePayment::Notification.new(		request.raw_post)
	    p &amp;quot;Notification object is #{notify}&amp;quot;
	    if notify.acknowledge
	      get_notification
	    end
	
	    def get_notification
	      p &amp;quot;Transaction ID is #{notify.transaction_id}&amp;quot;
	      p &amp;quot;Notification object is #{notify}&amp;quot;
	      p &amp;quot;Notification item_id is #{notify.item_id}&amp;quot;
	      p &amp;quot;Notification amount is #{notify.amount}&amp;quot;
	      p &amp;quot;Notification status is #{notify.status}&amp;quot;
	      p &amp;quot;Notification invoice is #{notify.invoice}&amp;quot;
	
	      @notification = OrderNotification.create(
	      :status =&amp;gt; notify.status,
	      :txn_id =&amp;gt; notify.transaction_id,
	      :amount =&amp;gt; notify.amount,
	      :item_id =&amp;gt; &amp;quot;@theme.user.name/Theme_#{@theme.id}&amp;quot;,
	      :invoice =&amp;gt; notify.invoice
	      )
	
	      p &amp;quot;**NOTICE: Istant Payment Notifications details were succesfuly added to @notifiation&amp;quot;
	
	      if @notification.status == &amp;quot;COMPLETED&amp;quot;
	        is_completed
	      else
	        puts &amp;quot;The transaction wasn&amp;#39;t completed&amp;quot;
	      end
	    end
	
	    def is_completed
	
	        # Add details to the purchase table
	          @current_user = User.find(params[:current_user])
	          @purchased = Purchase.create!(
	          :author =&amp;gt; @theme.user.name,
	          :buyer =&amp;gt; @current_user.name,
	          :theme =&amp;gt; &amp;quot;#{@theme.title}# id:#{@theme.id}&amp;quot;,
	          :price =&amp;gt; @theme.price,
	          :author_profit =&amp;gt; @author_profit,
	          :railsview_profit =&amp;gt; @railsview_profit
	        )
	
	        print_purchase_details(@author_profit)
	        increment_theme_purchase(@theme.id)
	    end
	
	    render :nothing =&amp;gt; true
	  end
	
	  def print_purchase_details(author_profit)
	    puts &amp;quot;AUTHOR PROFIT: #{author_profit}&amp;quot;
	    puts &amp;quot;The details were added to the Purchase table:&amp;quot;
	    puts @purchased.to_yaml
	  end
	
	  def setup_billing_information
	    @gateway.set_payment_options(
	      :display_options =&amp;gt; {
	        :business_name    =&amp;gt; &amp;quot;Railsview.com || #{@theme.title}&amp;quot;,
	        :header_image_url =&amp;gt; &amp;quot;http://cdn.yourbusiness.com/logo-for-paypal.png&amp;quot;
	      },
	    :pay_key =&amp;gt; response[&amp;quot;payKey&amp;quot;],
	    :receiver_options =&amp;gt; [
	      {
	        :description =&amp;gt; &amp;quot;Your purchase of #{@theme.title} made by #{@theme.user.name}&amp;quot;,
	        :invoice_data =&amp;gt; {
	          :item =&amp;gt; [
	            { :name =&amp;gt; &amp;quot;#{@theme.title} - #{@theme.id}&amp;quot;, :item_count =&amp;gt; 1, :item_price =&amp;gt; @theme	.	price, :price =&amp;gt; @theme.price},
	          ],
	          :total_shipping =&amp;gt; 0,
	          :total_tax =&amp;gt; 10
	        },
	        :receiver =&amp;gt; { :email =&amp;gt; @seller }
	      },
	      {
	        :description =&amp;gt; &amp;quot;XYZ Processing fee&amp;quot;,
	        :invoice_data =&amp;gt; {
	          :item =&amp;gt; [{ :name =&amp;gt; &amp;quot;Fee&amp;quot;, :item_count =&amp;gt; 1, :item_price =&amp;gt; 10, :price =&amp;gt; 10 }]
	        },
	        :receiver =&amp;gt; { :email =&amp;gt; @railsview }
	      }
	    ]
	    )
	  end
	
	  private
	
	    def assigns_gateway
	      @gateway = ActiveMerchant::Billing::PaypalAdaptivePayment.new(
	        :login =&amp;gt; @app_paypal,
	        :password =&amp;gt; &amp;quot;=PASSWORD&amp;quot;,
	        :signature =&amp;gt; &amp;quot;APPLICATION_SIGNATURE&amp;quot;,
	        :appid =&amp;gt; &amp;quot;APPLICATION_ID&amp;quot;,
	        # :email =&amp;gt; &amp;quot;#{current_user.paypal}&amp;quot;
	        :email =&amp;gt; @seller
	      )
	    end
	
	    def get_buyer_info
	      @current_user = User.find(params[:current_user])  
	    end
	
	    def get_theme
	      @theme = Theme.find(params[:theme])
	    end
	
	    def setup_seller_profit(profit_in_percentage)
	      seller_profit = profit_in_percentage/100
	      comission = 1 - seller_profit
	      @railsview_profit =  @theme.price * comission
	      @author_profit = @theme.price * seller_profit
	    end
	  
	    def print_profit_rate
	      puts &amp;quot;COMISSION: #{comission}&amp;quot;
	      puts &amp;quot;authorProfitPercentage #{seller_profit}&amp;quot;
	      puts &amp;quot;AuthorProfit: #{@author_profit.to_f}&amp;quot;
	      puts &amp;quot;railsviewProfit: #{@railsview_profit}&amp;quot;
	    end
	
	    def setup_receivers(primary_receiver, secondary_receiver)
	      @seller = primary_receiver
	      @railsview = secondary_receiver
	    end
	
	    def app_paypal_email(email)
	      @app_paypal = email
	    end
	
	    def increment_theme_purchase(theme_id)
	      Theme.increment_counter(:purchased, theme_id)
	    end
	
	end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ideally I would recommend to put all the sensitive information in a .yml file and add it to your .gitignore file this way when you push your project to github all the logins information remains private.&lt;/p&gt;</content>
 </entry>
 
 
</feed>