Jan 29

All the Agile folks would say if you run/create your tests you might not even get this far. However, that’s not always reality for me.

1. ‘tail -f /development.log’

If you are on OSX, the above is easy.
On Windows, I’d suggest looking at Cygwin and getting tail working. It’s worth it.

a) open terminal.
b) Clear the screen before you reattempt what is broken. (Use Apple + K on OSX)
c) Stop tail right after getting the error (CTRL + C)
d) Look for the first line that is not normal Rails and debug output. See next area.

2. Skip all the fluff and go for Ruby errors. You will learn to see “stacktraces” and errors in Ruby/Rails after a little practice.

They will look something akin to this:

1)An error
2)The file that the error occurred in…
3)the line number/and problem code…
4)A sample of the code at and around the error.

5) A bunch of code that was run prior to this happening.

3. Did you recently change the file in question? If so, revert what you did?

4. Use logger.debug to print out something right before the error to see if your code is making it that far.

Tagged with:
Jan 04

First as you read Rails and learn more you will discover the DRY (Don’t Repeat Yourself) principle. This means do something once and then don’t dig another ditch.

So here’s my opinion, both partials and tags are for DRY.

One usage difference in my opion: Is if the output is only a few lines, then a tag is simpler and cleaner. However, if the output is many, many lines of HTML like a menu then I start to lean toward partial. However,

There is one thing that a tag can do nicely that partials can’t as cleanly. Arguments can be passed and code run away from the view. Example:

<%=  commentor_pretty_name(blog.login_id) %>

here is the actual tag in application_helper.rb:

##############################
# takes a login_id alone and converts to pretty name for comment/tag
##############################
def commentor_pretty_name(login_id)

def_return = "Nil Name"
begin
logger.debug("app helper commenter_pretty_name")
#the "||" operand below means that we will use the def_return of "Nil Name" if Login.find returns nil
Login.find(login_id).unique_name || def_return
#end
rescue
def_return
end

end

#### END CODE

To do the same in a partial you would have to have this:

<%= render :partial => 'commenter', :locals => { :login_id => user.id} %>

user.id would be passed as argument AND then on the partial you would need some of the ruby code in the actual partial to lookup the user.

<%   #note no '=' tag this is purely to run ruby code and set variables

@def_return = "Nil Name"
begin
logger.debug("app helper commenter_pretty_name")
@def_return  = Login.find(login_id).unique_name
#end
rescue
@def_return
end
%>
    <%= @def_return  %>

Best of both worlds? Use a tag to run the ruby code and then a partial to render? When the tag gets riddled with html/view then this presents a good blend of keeping view and model seperate by using a tag to setup/do ruby work and simple partial to present.

Dec 18

There are several ways out there to install RMagick: via ports, package installer, etc. My advice: before you even start working on these.

1. Install ALL updates from Apple for your OSX.

2. Update XCode to the latest downloadable version.

Doing the above will save you a whole weekend and avoid weird errors like this:
.libs/libMagick.dylib must have a ‘:’ between its file names
H

Nov 30

Causes?

1. ‘require ‘action_controller/integration’ is in your environment.rb instead of just production. This is added to allow script/runner to do background jobs,etc as suggested on a site elsewhere I can’t find. Put this in the specific environment -> config/environments/production.rb’

Nov 19

I’m writing these rules for my own projects and because I have yet to see anything concrete.

1. method names should be ALL lower case with underscores. Use standard verbs for the purpose of the method if possible, ie. get, set, create, add,etc with the item acted upon also as part of method. Examples:

‘get_profile_names’, ’set_profile_pictures’

2. DON’t BE redudant. no method names with ‘method or function’.

3. Keep variables names meaningful. NO: x,y,i unless it’s simply part of a loop increment. Make them: pic_count, event_name.

4. About commenting. Actually this is the part I find most confounding about Ruby and Rails. There is no standard way other than RDoc. I AM NOT suggesting Hungarian notation. Yuck! BUT. I have started using the below on my projects for basic Rails Controller and Model documentation.

##############################
# OUTER METHOD COMMENTS
##############################

#******************************
# INNER METHOD COMMENTS
#******************************

One of the hardest things for me when I dove into the Ruby pool was the abundance of ‘def’ tags and NO other control characters. My mind becomes glazed and overwhelmed with bigger classes. While Java and C has {}; and other items to help break up the appearance.

This will be a work in progress.

Nov 17

Found a good post:

http://www.railsjitsu.com/ruby-on-rails/partial-flash-messages-in-rails/trackback/

Oct 29

This is to share my opinion. A very good alternative SMTP server that is cross platform and simple, James from the apache project: james.apache.org/. You’ll need Java installed (but who doesn’t these days?)

It’s a little bit confusing when you extract it. James is a module that sits on top of Avalon/Pheonix. To get to the nitty gritty and actually configure your server:

[where you extracted]/apps/james/SAR-INF/

the config.xml is where you want to make a few changes.

On my mac I created a script to start it up. You need to sudo b/c it uses lower ports.

#my script

sudo /Applications/james-2.3.1/bin/run.sh

To manage the server, it creates console app on port 4555. just “telnet localhost 4555″ and login with the root user you configured in the config.xml. Type ‘HELP’ and follow commands. Add a user and now you can use Thunderbird, Apple Mail or Outlook to connect locally and test email.

Enjoy.

H

Sep 30

applications.rb implements :before_filter that will

model for actions: ‘action_permissions’

model for user permissions: ‘user_permissions’

the session level user permission variable: ‘user_permission_sum’ using the previously discussed base2 sum of all allowed permissions .
check that the user can execute the controller/action using ‘permission_token’

Sep 20

REVISED:

ACL, privilege , permission system based on sum of exponents. {REVISED: sum of square exponents from 2 and including 1} Huh?

A little theory:

Look at this NOW: Masking

Idea/Premise:

I present the automatic bar tender. It makes sure people of age get served beverages and also people who have partial liver failure only get beer.

RULES:
Everyone has a number on their driver’s license. It’s a single number that can’t be forged (please assume this). it’s easy to read and easy to carry. The automatic bartender can read this simply by scanning your license. Let’s call it your “Permission Number”:

Permission Number Examples

List of possible drinks, these numbers are all square exponents of 2 and including
————————
1 Drink Water
2 Drink Soda
4 Drink Beer
8 Drink Gin

The sum of all permission numbers in binary might be represented as:

00001111

Permission numbers for our test subjects. These are created by SUMming the permission numbers.
———————
Sara:
1 Drink Water
+ 4 Drink Beer
+ 8 Drink Gin
————
Permission Number = 13 (00001101)

Fun Bobby:
+ 4 Drink Beer
+ 8 Drink Gin
————
Permission Number = 12 (00001100)

Underage Kid:
+ 1 Drink Water
+ 2 Drink Soda
————–
Permission Number = 3 (00000011)

Ok now that the example people have their numbers. Let’s play fun. The math is really simple. Because each drink permission is subject to masking, I can tell by your single permission number what drinks you can drink.

Ordering Drinks Example(paste into irb for fun!):

#!/usr/bin/ruby
class Rbar_Tender  #rbar_tender.rb

########################################
# The bartender. He's actually very stupid.
########################################
def self.order_drink(drink_permission,person_permission)

response = "Talk to the manager, I can't serve you now."
if (person_permission.to_i & drink_permission.to_i) > 0
response =  "here's your drink, thanks for your business."
end
return response
end
end #end of class

#basic permissions
drink_water = 1 #0001b
drink_soda = 2 #0010b
drink_beer = 4 #0100b
drink_gin = 8 #1000b

#people's permissions
sara_permission = 13 #1101 #assigned during walking into bar with ID.

#open for business
puts "sara wants a beer..."

#drink_beer.to_i & sara_permission.to_i
puts Rbar_Tender.order_drink(drink_beer,sara_permission)

puts "\n...a minutes later....\n\n"

puts "sara wants a soda..."
#drink_soda.to_i & sara_permission.to_i
puts Rbar_Tender.order_drink(drink_soda,sara_permission)




What is your point Hank? Well, in applications and web applications in particular, you need lot’s of permissions to do stuff. I mean literally dozens or hundreds of unique permissions:
*Login to your account.
*Delete pictures.
*Email a friend.

When someone wants to do something, you check that persons own record if they have access. Below are some examples of permissions you might have to look up each time for each person (NOTE looking this up takes time and resources), I’ve included a 2 exponent example beside it:

HAS_LOGIN_ACCESS (64)
HAS_DELETE_PICTURE_ACCESS (256)
HAS_EMAIL_FRIEND_ACCESS (1024)
HAS_ABILITY_TO_INVADE_IRAQ (73,786,976,295,000,000,000)

——
But wouldn’t it be cooler and use a lot less typing to have a single number, say for me 19. And magically b/c of math, we know what you are permitted to do. No database look ups for each time you need to do something. Your number IS what you can do. And each time you do an ACTION, that action has it’s permission number.

UPDATE: 7/21/2007

The above idea does have one limitation. DUH! The permission numbers grow exponentially as we add permissions. Simply having 66 permissions creates the huge number for HAS_ABILITY_TO_INVADE_IRAQ above. So I will explore something like TCP with a series of 8 or 16 bit binaries and do XOR on those instead. So the above permissions would be like so:

HAS_LOGIN_ACCESS (1000010)
HAS_DELETE_PICTURE_ACCESS (100000000)
HAS_EMAIL_FRIEND_ACCESS (10000000000)…

H

Sep 20

Not to toot my own horn, but you might want to read my post about my Setting pattern b/c I make use of it extensively in all my Rails code as you can see below. I do this to avoid hardcoding anything.

# cheap background jobs in rails

1. fork and exec

#START RAILS CODE
# in my controller

touch_file = Setting.get_rails_home + “DONE” #used to debug
script_runner = “export RAILS_ENV=’#{Setting.get_rails_env}’; #{Setting.get_runner_script_path}”
#this controller runs an Actionmailer that batches through a VERY large list of emails.
cmd = script_runner + ” \”app = ActionController::Integration::Session.new; app.get ‘mailing_list_emails/blast_batch_bcc_list’\” ”
logger.debug(”blast_background to run: [#{cmd}]“)
# logger.debug(”touching files to run: #{touch_file}”)

fork do
#exec “#{cmd}”
exec “echo running #{cmd} >> #{touch_file}” #you need the quotes, because any characters like > or | need to be let through

end
#Process.wait # process wait will make the Rails process wait and the process WILL timeout in CGI.

redirect_to :action => “index”
#END RAILS CODE

2. Because I am doing a fork and exec zombie process WILL be created. So use cron to fix that.
59 * * * * /home/scripts/zombie.sh –cron

There are examples for the zombie.sh on the web. I used this one, thanks.
(http://www.linuxscrew.com/2007/08/17/automatic-zombie-processes-killing-shell-script/)