Hi, this blog is no longer maintained, my new blog is here

Ruby On Rails and a Conning Israeli entrepreneur

Showing posts with label elad. Show all posts
Showing posts with label elad. Show all posts

Site Mapping

One of the basic elements for each medium sized web-sites and above, is the site map.
when using rails, mapping in not so easy because the object oriented routing which makes it hard to distinct between regular actions and some "maintenance" actions.

I decided to prepare a small library (for now, extensions will be welcomed) which contains 3 basic mapping functions.

The first 2 are pretty custom made for my current project and will be irrelevant for the most of you, but the third is brought to you straight from the oven.

def site_map(path = RAILS_ROOT + "/app/controllers/")
collector = Hash.new
tree = Dir.entries(path).delete_if {|entry| entry.slice(0,1) == "."}
tree = tree.delete_if {|entry| entry.include?('application_controller.rb')}
unless tree.empty?
for element in tree
element = element.gsub('_controller', '')
if element.include?('.rb')
element = element.gsub('.rb', '')
collector['File-' + element] = element.camelize
else
collector['Dir-' + element] = site_map(path + '/' + element)
end
end
end
return collector
end




as you probably noticed, the site_map method returns a Hash.
regular controllers are returned with a key in this format. 'File-', so ClientsController for example will be issued a 'File-Clients' key.
Sub folders will be hashed as well and the same, except that the directory element's key will be in the format of 'Dir-'

Enjoy.

The Boolean connection

Few people understand the true mentality of dealing with a Boolean variable.
The normal programmer will panic, and immediately attempt to compare it to a Boolean constant.

when using Rails, This is wrong.

When migrating a boolean typed column using Rails, the actual column type created is "tinyint(1)" (a single digit number presentation which means that the actual value being saved is 0's or 1's.
Where is the problem? in Ruby for a change.
In Ruby, everything except nil and false is considered to a true value. In C, Python and many other languages, 0 and possibly other values, such as empty lists, are considered as false.

which brings us to the inevitable conclusion, that simplay comparing boolean values to true or false will not do.

Rails solves this tiny issue for us, for each boolean attribute, Rails implements a "query method" as i prefer to call them. These query methods are simply called by issuing the boolean attribute name with a "?" suffix.

for example, the @mail_message instance object has a is_important boolean attribute, so instead of doing:


@mail_message.is_important == true

or whatever, we can simply ask rails by:

@mail_message.is_important?


beautiful... isn't it?


The Web Ask eizesus.com

Subscribe

    follow me on Twitter

    Twiters Around

    About Me

    My photo
    I am a web developer for more than 9 years, managed, cried, coded, designed and made money in this industry. now trying to do it again.

    Labels