Monday, April 28, 2008

sed / grep /awk and other useful commands

Here i'll post useful commands using sed grep & awk for OSX:

sed "s/SEARCHTERM/RESULTTERM/" result.txt

Convert Uppercase to lowercase
cat test.txt | tr [:upper:] [:lower:]

More information:SED onenliners

Delete your desired files:
find / -name "*\.eml" -exec rm -f {} \;

Thursday, April 17, 2008

Guidelines for developing an application in Ruby on Rails

Here I collect some guidelines how to develop in Ruby on Rails. Please feel free to comment on these guidelines. I would love to discuss them since I am quite new in Ruby on Rails

- Use an IDE (e.g. NetBeans)
- Use version control (e.g. SubVersion)
- Use the model to implement business logic
- The controller should be lean
- Don’t put business logic in to the view
- DRY: Use partials and helper-classes, …
- Use CSS for rendering your page
- Use migrations sequentially
- Consolidate migrations

Thursday, April 10, 2008

Rails cheat sheet

As follows I show you some rails commands, which are pretty useful. Read also the following document, which summarize the most useful Rails-commands:
byOnRails-Cheatsheet-BlaineKendall.pdf

Find first Order in the table
o = Order.find(1)

Find by SQL
Use o.findbySQL

Assume an order has n protocols:

order.rb
class Order < ActiveRecord::Base
has_many :protocols

end

class Protocol < ActiveRecord::Base
belongs_to :order
end

script/console
pp = Protocol.find(1)
oo = Order.find(1)
oo.protocols << pp
oo.protocols

Iterate trough oder-array

arrayorder = Order.find(:all)

arrayorder.each do |order|
p order
end

Access the session
session[:user_id]

In the view:
< %=h Protocol.find(order.id).orderinit_date % >

Access to params:
< %= params['controller'] % >

Wednesday, April 09, 2008

Ruby structures

If/then/else

order.order_date != nil ? order.order_date.strftime(" %d.%m.%Y %I:%M"):""

Friday, April 04, 2008

Ror: Redirecting

Redirect the form to a non default route:

# POST /customer_profiles.xml
def create
....
respond_to do |format|
if @customer_profile.save
flash[:notice] = 'CustomerProfile was successfully created.'
format.html { redirect_to new_customer_path }
format.xml { render :xml => @customer_profile, :status => :created, :location => @customer_profile }
else
...
end
end
end

Thursday, April 03, 2008

1:n relations in ROR

1. Generate a customer_profile which belongs to a customer
./script/generate scaffold customer_profile name:string customer_id:integer

2. Make migration:
rake db:migrate


3. In the model customer_prfoile.rb
class CustomerProfile < ActiveRecord::Base
belongs_to :customer
end


4. In the console you can test the 1:n relation (script/console):

>> customer = Customer.create(:name => "Dominik")
=> #
>> cprofile = CustomerProfile.create(:name => "myprofile")
=> #
>> cprofile.customer = customer
=> #
>> p cprofile.customer.name
"Dominik"
=> nil
>> p cprofile.customer
#
=> nil
>>

Ruby routes howto

http://localhost:3000/customers/32/client_profiles/new


in routes.rb:
map.resources :customers do |c|
c.resources :client_profiles
end


in client_profiles_controler.rb:

in method new:

# GET /client_profiles/new
# GET /client_profiles/new.xml
def new
@client_profile = ClientProfile.new
@client_profile.customer_id = params[:customer_id]
respond_to do |format|
....
end
end


in method create:
# POST /client_profiles
# POST /client_profiles.xml
def create
@client_profile = ClientProfile.new(params[:client_profile])
@client_profile.customer_id = params[:customer_id]
respond_to do |format|
if @client_profile.save
.....
end

Wednesday, April 02, 2008

Scaffolding types

-binary
-boolean
-date
-datetime
-decimal
-float
-integer
-primary_key
-string
-text
-time
-timestamp


See also: http://www.packtpub.com/article/Working-with-Rails-ActiveRecord-Migrations-Models-Scaffolding-and-Database-Completion

Tuesday, April 01, 2008

Usefull ruby commands

Create project with mysql:
rails bla --database=mysql

Create database:
In the Projectdirectory: "rake db:create:all"

Update tables:
In the Projectdirectory: "rake db:migrate"

Start Server:
In the Projectdirectory: "script/server"

Start documentation:
gem server -p 3333

Start console:
In the Projectdirectory: "script/console"

Scaffolding:
./script/generate scaffold person_profile name:string company:string organisation:string unit:string function:string role:string

After using command "rake db:migrate" you'll find the generated code here:
http://localhost:3000/profiles

Show all routes:
rake routes

Partials:
Comming soon

Migrations in Ruby:
1. Create migration: script/generate migration alter_client_profile
2. Alter migration, see example below:
class AlterClientProfile <>

3. rake db:migrate

New consistent database from migrations:
- rake db:drop
- rake db:create
- rake db:migrate

MySQL using terminal with OSX

Precondition:
Install MySQL-Binary from http://dev.mysql.com/downloads/mysql/5.0.html#macosx-dmg

Start Server:
/usr/local/mysql/bin/mysqld_safe

Start DB-Client:
/usr/local/mysql/bin/mysql -u root
- Use Database: use langodem_development
- show tables;
- Delete Database: drop database langodem_development


Create DB langodem:
/usr/local/mysql/bin/mysqladmin -u root create langodem

Connect to DB langodem:
./mysql -u root langodem

Find all running mysql-server instances
ps -ef | grep mysqld

Kill all mysql-server instances
killall -TERM mysqld

Set Path in OSX / Linux

In bash_login

export PATH=/usr/local/mysql-max-5.0.27-osx10.4-powerpc/