Sunday, April 13, 2008

Rails attr_accessor is an instance variable

It took me some time to truly understand rails attr_accessor macro method though I've already used it a lot before.

Attr_accessor, along with attr_reader and attr_writer, they are in fact ruby things not rails. Attr_accessor defines instance variables and builds the get/set methods for each instance variable.

To know the difference between ActiveRecord's attribute (for table field) and the attr_accessor, the best way is to give an example, let's see:

class User > ActiveRecord::Base
attr_accessor :password #password is not a user's table field
validates_presence_of :login
end

##########################

[/home/jack/test_app]$ script/console
Loading development environment (Rails 2.0.2)
>> user = User.new(:login => "jimmy", :password => "railsrocks")
=> #
### password is not a field

>> user.instance_variables
=> ["@attributes_cache", "@attributes", "@password", "@new_record"]
>> user.instance_variable_get :@login
=> nil
>> user.instance_variable_get :@password
=> "railsrocks"
>> user.has_attribute? "login"
=> true
>> user.has_attribute? "password"
=> false
### login is an attribute, while password is a instance variable

>> user.login
=> "jimmy"
>> user.password
=> "railsrocks"
>> user.send 'login'
=> "jimmy"
>> user.send 'password'
=> "railsrocks"
### both getter/setter are working
### in some situation, for example, the field name itself is a variable
### you can use: user.send method_name

>> user[:login]
=> "jimmy"
>> user.attributes
=> {"login"=>"jimmy"}
>> user[:password]
=> nil
### oops, I can not get password by this way

>> user.attributes={:login => "grissom", :password => "rubyrocks"}
=> {:password=>"rubyrocks", :login=>"grissom"}
>> user.password
=> "rubyrocks"
### well, to assign value is working

Thursday, April 10, 2008

Design Free Administration Back Office For Rails Application

There are some common concerns to build a back office for a rails project. Not like the front pages, which are usually designed by professional web designers, back office pages often need to be scratched by programmers. It often takes me a lot of time to do the layout, HTML structures, header and footer, div content boxes, list table styles and form field styles. Though I am familiar with CSS, I still troubled a lot with the overall look and feel because of my lack of the web design gene. So I am writing here to conclude a way to be design fee....

1. Install my plugin uni_admin for admin layout, table styles:

http://code.google.com/p/ruby-on-rails-plugin-uni-admin/

2. Install uni-form and my uni-form-patch for form styles

http://code.google.com/p/rails-uni-form-patch/

3. Install plugin restful authentication for basic authentication

http://xiaoboonrails.blogspot.com/2008/02/install-and-config-rails-plugin.html

4. If you have installed comatose, it is easy to integrate comatose_admin with uni_admin layout
rake comatose:admin:customize
update related content of layouts/uni_admin_layout.html.erb to layouts/comatose_admin.rhtml

Rails Plugin Uni_Form_Patch for Plugin Uni-Form

UniFormPatch
============
A small patch plugin for Marcus Irven's uni-form plugin.

Code home: http://code.google.com/p/rails-uni-form-patch/

Fix the following features:
  • Make the form show properly
  • Show field focus and error background images
  • Show error and success form messages properly

Installation
=========
If you havn't installed uni-form, install it at first:
script/plugin install http://uni-form.rubyforge.org/svn/trunk/plugins/uni-form/

Follow the installation notes in Marcus's uni-form README to finish installation

Install uni_form_patch:
script/plugin install http://rails-uni-form-patch.googlecode.com/svn/trunk/uni_form_patch

The installation will automatically copy/update/delete files:
Copy public/images/uni-form/*.png
Update public/stylesheets/uni-form.css
Delete public/stylesheets/uni-form-generic.css

Example
=======
For the usage of uni-form, read Marcus's uni-form README.

Show uni-form style messages:
<% uni_form_for :user do |form| %>

<%= uniform_success_message flash[:notice] if flash[:notice] %>
<%= uniform_error_messages [@user] %>
<%= uniform_error_messages [@user, @address] %>

<% form.fieldset :type => "block", :legend => "cool stuff" do %>
<%= form.text_field :first_name, :required => true, :label => "Your first name" %>
<%= form.text_field :last_name %>
<% end %>
<%= form.submit "save" %>
<% end %>

Ruby On Rails Plugin Uni Admin For Admin Layout

Today I code a new simple plugin Uni_Admin. Uni Admin is a simple administration layout support for ruby on rails web application. Now it includes:
  • an admin base controller and dashboard action
  • a simple admin layout
  • a simple admin CSS file
  • an integrated admin menu
  • a record list table style
Demo:


More details please see the code page:

http://code.google.com/p/ruby-on-rails-plugin-uni-admin/

Wednesday, April 9, 2008

KikOut.com Beta Version 0.2 Released

My ruby on rails project KikOut.com released beta version 0.2.

It is a search engine to help you facilitate your web searching, for example, using youtube:keyword to search youtube directly. You will dig more fun after using it. More information about Kikout please check out the Kikout why&howto page.

Welcome make your own search definitions or share your experiences/submit your comments.

Wednesday, March 19, 2008

Rails ActiveMerchant Patch For BeanStream

I submitted the patch to active merchant plugin for Canada payment gateway BeanStream. I guess that it will take some time to get reviewed, tested and accepted ( or not accepted because the remote test can not be passed without beanstream's test account which it doesn't provide it to me), so I uploaded the patch to a google code project:

http://code.google.com/p/activemerchant-patch-for-beanstream/

The installation, description and a simple example have been already written in the project home page. Issue your bug or give me your feedback if you encounter any problem when using it.

A Rails Plugin table_styles to let you easily use up to 42 different css styles for HTML table.

Today I wrote a rails plugin based on CSS Table Gallery .

I upload to google code: http://code.google.com/p/rails-plugin-table-styles/

Installation
script/plugin install http://rails-plugin-table-styles.googlecode.com/svn/trunk/table_styles

Usage

* Use the demo to find one of your favorable style.
# Start your server: >script/server
# Visit http://localhost:3000/table_styles_demo/
# Remember to add map.connect :controller/:action in your routes.rb

* Choose one of table styles in your layout header, use "tagbox" as an example.
<%= table_stylesheet_link_tag "tagbox" %>

* Just use table tag it in your view

* Use form helper if you want to let admin user to choose table style in user preferences page.
table_style_select or table_style_options_for_select