Eric's thoughts

ruby, rails, and more

How to break down a complex project

leave a comment »

steps to break down a (complex web based) project:

1. wireframes for every page

  • draw out all necessary elements – menus, links, buttons and messages
  • for each of the element on the wireframe, ask questions – where does this link to, who can see or do this.
  • go through one most important use case, see if the wireframes accomplishe this

2. use post-it note or tracker to group functions

  • page by page
  • break different functions on each page into stories. ask “can xx exist/make sense without xxx”. if function A can exist/function/make sense without function B, then they are two stories
  • detail the story based on the wireframe- include who, what(the function), and how – e.g. as the owner of the project(who), i can invite other people to join the project(what), by use the text field in left side and click add(how), or as a user(who), i can see a list of the projcts with name, title, and last updated date(what)

3. estimate each story or post-it note

Written by ericzou

August 5, 2009 at 1:57 am

Posted in Productivity, project

Tagged with ,

Top Ten RubyMine Shortcuts I Use Every Day:

leave a comment »

Here are some RubyMine shortcuts I use everyday

1. cmd + E
cmd + E brings up the ‘Recent Files’ dialog, but I find it most useful when switching two files back and forth.

2. cmd + [ or cmd + ]
similar to ‘back’ and ‘forward’ buttons, every useful

3. cmd + shift + N
find files

4. cmd+ shift + F
global search

5. cmd + 1 .. 9
bring up different panels

6. ctrl + W
once for single word selection, press multiple times to increase the selection range

7. fn + Up/Down
scroll up/down one page, the other useful shortcut is ctrl + Up/Down which scroll up/down one method

8. cmd + B
look up a method, very useful

9. cmd + option + L
format code – you have to love this feature, it is awesome.

10 ctrl + N
Use it in project panel to create new files

Here are some shortcuts I should really start to use

1. opt + shift + C
bring up all recent changes

2. cmd + shift + A
Go to Action. if you can’t remember shortcuts, this is the way to look it up

Bonus:

There is an excellent productivity guide in RubyMine helper. It even records how many times you use each feature. so check it out.

Written by ericzou

July 17, 2009 at 2:37 pm

Posted in Productivity

Tagged with , , , ,

count v.s. lenght v.s. size

leave a comment »

there are couple difference between count and length, generally speaking, count does’t load the array so it is faster. However, there are situation count won’t give you an accurate number
here is an example

suppose you have three tables ‘users’, ‘roles’, and the join table ‘roles_users’, and you want to all active users that has role of ‘user’ or ‘admin’
Here is how to get it in rails:

named_scope :active, :joins=> :roles,
     :conditions=>['state=? and roles.name in (?) ', 'active', ['user', 'admin'] ], :group=>'id'

here group is necessary otherwise you will get duplicate users records if users has more than one role.

Here is where length different than count:
say joe is the only user and he has both user and admin roles

User.active.count # returns 2
User.active.length # returns 1

It is easy to figure out why through SQL:
the named_scope translate to

select u.id, u.first_name, u.email
from users u
inner join roles_users ru on ru.user_id = u.id
inner join roles r on r.id = ru.role_id
where u.state = 'active' and r.name in ('user', 'admin')
group by u.id;

the count translates to

select count(*), u.id, u.first_name, u.email
from users u
inner join roles_users ru on ru.user_id = u.id
inner join roles r on r.id = ru.role_id
where u.id = 2 and r.name in ('user', 'admin')
group by u.id;
without group by
+----+------------+--------------------+
| id | first_name | email              |
+----+------------+--------------------+
|  1 | Joe        |    joe@example.com |
|  1 | Joe        |    joe@example.com |
+----+------------+--------------------+
5 rows in set (0.00 sec)

with group by
+----+------------+--------------------+
| id | first_name | email              |
+----+------------+--------------------+
|  1 | Joe        |    joe@example.com |
+----+------------+--------------------+
1 row in set (0.00 sec)

using count and here is why it returns 2
+----------+----+------------+--------------------+
| count(*) | id | first_name | email              |
+----------+----+------------+--------------------+
|        2 |  1 | Joe        |    joe@example.com |
+----------+----+------------+--------------------+
1 row in set (0.00 sec)

the bottom line: don’t use count when using :group

p.s. for more difference between length, size and count, read this blog post: http://rhnh.net/2007/09/26/counting-activerecord-associations-count-size-or-length

Written by ericzou

June 20, 2009 at 5:24 pm

Posted in rails programming

Tagged with ,

pass parameters in rake test

leave a comment »

parameters are passed in rake test through ENV hash, here is an example:

$>rake foo:bar example='this is an example'

namespace :foo do
  desc 'example'
  task :bar do
    puts "parameter = #{ENV['example']}"
  end
end

Written by ericzou

June 20, 2009 at 5:11 pm

Posted in ruby programming

Tagged with ,

execute shell command in ruby rake task

with 2 comments

There are a few differnt options to execute shell cmd in ruby rake task:
1. exec
2. system
3. backtick “
4. %x{}

%x{} returns back to rake task whereas  exec and system does not (exec replace the current process with a new one, system return true or false )
so if you  do something like

desc 'example'
  task :example do
    exec {cmd_one}
    exec {cmd_two}
  end
end

cmd two will never get executed. Switch to %x{} instead.

btw, you can run multiple rake task like this

desc 'example'
  task :example do
    Rake::Task['name_space:task_one']
    Rake::Task['name_space:task_two']
  end
end

Written by ericzou

June 20, 2009 at 5:07 pm

Posted in ruby programming

Tagged with , , ,

Follow

Get every new post delivered to your Inbox.