Archive

Archive for November, 2008

Rails Note #8: Testing with session variables

November 18th, 2008 rupert Comments off

Taken from http://guides.rails.info/testing_rails_applications.html

The get method kicks off the web request and populates the results into the response. It accepts 4 arguments:

*The action of the controller you are requesting. This can be in the form of a string or a symbol.
*An optional hash of request parameters to pass into the action (eg. query string parameters or post variables).
*An optional hash of session variables to pass along with the request.
*An optional hash of flash values.

Example: Calling the :show action, passing an id of 12 as the params and setting a user_id of 5 in the session:

get(:show, {'id' => "12"}, {'user_id' => 5})
Categories: rails, ruby Tags: ,

Rails Note #7: NIL in Fixtures

November 18th, 2008 rupert Comments off

If you want user_id to be ‘nil’, then omit it from your fixtures. I tried setting user_id: nil before, and it turned out to be ’0′ in the database.

# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
<% u = UserLogin.create( :password => 'foo') %>
<% u.password=('password') %>
 
user_no_profile:
 email: noprofile@yahoo.com
 admin: false
 user_id: nil
 salt: <%= u.salt %>
 salted_password: <%= u.salted_password %>

Picture 3.png

# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
<% u = UserLogin.create( :password => 'foo') %>
<% u.password=('password') %>
 
user_no_profile:
 email: noprofile@yahoo.com
 admin: false
 salt: <%= u.salt %>
 salted_password: <%= u.salted_password %>

Picture 2.png

Before pulling your hair out on what went wrong with your functional tests, check the test database if you have the correct values in your records. Remember ’0′ is different from ‘nil’.

Categories: rails, ruby Tags: ,

Rails Note #6: Pagination

November 16th, 2008 rupert Comments off

1. will_paginate docs
Main http://github.com/mislav/will_paginate/wikis
Reference http://mislav.uniqpath.com/static/will_paginate/doc/
Clone URL: git://github.com/mislav/will_paginate.git

2. Installation as a gem
http://github.com/mislav/will_paginate/wikis/installation
gem sources -a http://gems.github.com

Once installed, do script/server

3. Controller

class PoiAppController < ApplicationController 
  def poi_by_category
	mylimit = 100
    sql = "SELECT pa.* FROM poi_apps pa WHERE pa.id IN " +
          "(" +
			  "SELECT pc.poi_app_id " +
			  "FROM poi_categories pc " +
			  "WHERE pc.categ_node_id LIKE '" + params[:node_id] + "%%'" +
			  "LIMIT " + mylimit.to_s +
          ")"
    #@poi_apps = PoiApp.find_by_sql(sql)
    @poi_apps = PoiApp.paginate_by_sql [sql], :page => params[:page], :per_page => 10
 
  end
end

4. View

<h2>POIs</h2>
 
<div clas="page_info">
  <%= page_entries_info @poi_apps %>
</div>
 
<table>
  <tr>
    <td>POI_APP_ID</td>
    <td>CN_NAME</td>
  	<td>EN_NAME</td>
  </tr>
	<% for poi in @poi_apps %>
	<tr>
	  <td><%= poi.id %></td>
	  <td><%= poi.cn_name %></td>
		<td><%= poi.en_name %></td>
	</tr>
	<% end %>
</table>
 
<%= will_paginate @poi_apps %>

5. Checkout the styles http://mislav.uniqpath.com/static/will_paginate/

Categories: rails, ruby Tags: ,

My Textmate now has VI!

November 13th, 2008 rupert Comments off

Yep.. you read it right! Its not the full vim but I’m navigating much quicker now.. Now I have a drawer on my left, vim navigation on the files! sweet!

http://www.fowpas.net/vimate

Notes:
* No undo ‘u’. Use [COMMAND]+Z

Categories: rails Tags: , ,

Rails Note #5: Dynamic Layouts

November 13th, 2008 rupert Comments off

Wow.. everyday.. Ruby on Rails seems to amuse me.. I just figured how to seperate layouts just by having different files in the layouts/ which corresponds to a controller. If a layout file “home (home.html.erb)” exists and there is home_controller, then the home_controller would use that layout instead of application.html.erb.

Now.. I haven’t validated this concept by book and would do so soon..

Picture 1.png
Fig 1

Another way is specifying a filter…

1. In application.rb, specify a method that displays the ‘admin’ layout if the user is admin.

    def load_layout
      if admin?
        self.class.layout('admin')
      end
    end

2. home_controller

class HomeController < ApplicationController
  before_filter :load_layout
end

Picture 2.png
Fig 2. admin.html.erb now exists in layouts/

Reference:
http://railscasts.com/episodes/125-dynamic-layouts

Categories: rails Tags: