establishing an sqlite3 db connection via ActiveRecord

ActiveRecord can be used as a stand-alone gem without going through a Rails app. When establishing a connection we must provide a name for the database. Here is an example where I use sqlite3 as a db engine. I want to provide a full path to the database also.

[code]
my_db_dir = ‘~/projects/myapp/db’
filename = File.join(my_db_dir,’development.sqlite3′)
ActiveRecord::Base.establish_connection(:adapter => ‘sqlite3’, :database => filename)
[/code]

On Ubuntu systems it is very likely that we will get an error


No such file or directory

I know the file is there. I know the path is correct. So what is wrong? The problem is the “~” character. ActiveRecord uses Ruby’s File.binread() method to read the file. The binread() method is a wrapper for a function in the c code (io.c) of the Ruby interpreter. When you dig deep into the source code of the Ruby project you will find the file.c file which contains a function for checking file paths for security related issues. This function is called rb_get_path_check().

Basically we should not pass a tilde character to File.binread(). It is something that will confuse you for many hours if you it!!

Here is a demonstration of the problem using interactive Ruby (irb).

[code]
$ irb
ruby-1.9.2-rc2 > File.binread("~/myfile.txt")
errno::ENOENT: No such file or directory – ~/myfile.txt
from (irb):1:in `binread’
from (irb):1
from /home/philip/.rvm/rubies/ruby-1.9.2-rc2/bin/irb:17:in `<main>’
ruby-1.9.2-rc2 > File.binread("/home/myuser/myfile.txt")
=> "This is the content of myfile… "
[/code]

You can see that the “~” character did not work. 🙁

Thor – add jQuery example

I create new Rails3 apps quite frequently. As you know, Rails is packaged with Prototype- the Javascript library. I prefer to use the jQuery library. So, I need to manually add the library file. I can copy it from another project but that requires me to type a long command such as
cp ../../source.../file ./public/javascripts

There are other problems. Rails projects have a ‘rails.js’ file which needs to be swapped out with a jQuery compatible file. Also, we need to include the Javascript files inside our html head tag. To save some time I decided to create a new Thor task to accomplish all this work. Once the task is created I can simply run the following command inside the root directory of any new Rails app:

thor fbp_tools:jQuerify

fbp_tools is the name of my custom collection of thor tasks. Here is what happens when the jQuerify task runs:

[code]
…$ thor fbp_tools:jQuerify
jQuery
create public/javascripts/jquery-1.4.3.min.js
create public/javascripts/rails.js
inject app/views/layouts/application.html.haml
NOTICE: please open and verify your modified layout
[/code]

We can see that 2 files were created in correct Rails directories. Also, the application.html.haml layout template file was opened and modified (the term for this is “inject”). Finally a notice to the user suggesting the verification of the modified layout file due to the fact that .haml template files require proper white-space alignment. (I’ll post something about the amazing haml system in a future article.)

What happens if we run it again?

[code]
…$ thor fbp_tools:jQuerify
jQuery
identical public/javascripts/jquery-1.4.3.min.js
identical public/javascripts/rails.js
inject app/views/layouts/application.html.haml
NOTICE: please open and verify your modified layout
[/code]

Since the files are identical, nothing happens. Also, the inject procedure doesn’t need to do anything because it detects the previous edit.

Thor – an intro

Thor is a simple and efficient tool for building self-documenting command line utilities. It removes the pain of parsing command line options, writing “USAGE:” banners, and can also be used as an alternative to the Rake build tool. The syntax is Rake-like, so it should be familiar to most Rake users.

I have built a corporate security model. When I say “security model” I am talking in a generic fashion. So what is it? Well it is ER-model for a relational database which allows me to record people objects, their web user objects, the organization objects that they belong to, the role objects for web app usage rights, and many other objects related to general web application use by humans.

When implementing such a model in an MVC architecture like RubyOnRails there will be many files located in various directories. This is not a big problem at all until you decide that you want to use this security model’s functionality in other RubyOnRails web applications. The obvious solution is to build some kind of plugin. Indeed, frameworks such as RubyOnRails do provide some sort of support for building plugins that can be reused.

In the Ruby development world we have the excellent Rake build and automation tool. However, this article will explore Thor which can be considered a kind of “next generation” Rake tool.

I’ll keep this post short and explore more details at a later time. For now let’s take a look at my Thor “configuration.”

[code]
…dev/_thor/testarea$ thor installed
Modules Namespaces
——- ———-
authena authena
fbptools fbp_tools

authena
——-
thor authena:install NAMEs files #
thor authena:removes installed files #

fbp_tools
———
thor fbp_tools:install NAME # installs files
thor fbp_tools:uninstall NAME # removes installed files

[/code]

We see that there are two Thor packages installed in my environment. I built them both. Authena is the name of the Thor task package which installs and uninstalls the security model files. fbp_tools is the package which installs some utility files which I use in many Rails apps.

Both packages install and remove files into various directories such as the “models”, “views”, and “controllers” directories.

Let me finish with a teaser showing you what happens when we install the Authena package. Next time, I’ll dig into the code a bit deeper.

[code]
…testarea$ thor authena:install
create app/models/accessibility.rb
create app/models/address.rb
create app/models/contact.rb
create app/models/organization_binding.rb
create app/models/organization.rb
create app/models/person.rb
create app/models/role.rb
create app/models/software_application.rb
create app/models/user.rb
create app/views/accessibilities/_access.html.haml
create app/views/accessibilities/change_user_role_state.js.rjs
create app/views/accessibilities/invalid_state_change.js.rjs
create app/views/layouts/admin.html.haml
create app/views/layouts/organizations.html.haml
create app/views/layouts/user_sessions.html.haml
create app/views/layouts/users.html.haml
create app/views/organizations/edit.html.haml
create app/views/organizations/_form.html.haml
create app/views/organizations/index.html.haml
create app/views/organizations/new.html.haml
create app/views/organizations/show.html.haml
create app/views/people/cancel_access_edit_area.js.rjs
create app/views/people/edit.html.haml
create app/views/people/new.html.haml
create app/views/people/_roles_by_user.html.haml
create app/views/people/show.html.haml
create app/views/people/destroy.html.haml
create app/views/people/index.html.haml
create app/views/people/_people_list.html.haml
create app/views/people/show_access_edit_area.js.rjs
create app/views/roles/_action_menu.html.haml
create app/views/roles/index.html.haml
create app/views/roles/show.html.haml
create app/views/users/_accessibilities_for_users.html.haml
create app/views/users/change_user_teacher.js.rjs
create app/views/users/_form.html.haml
create app/views/users/show_assign_to_teacher_area.js.rjs
create app/views/users/cancel_assign_student_to_teacher_area.js.rjs
create app/views/users/edit.html.haml
create app/views/users/new.html.haml
create app/views/users/show.html.haml
create app/controllers/accessibilities_controller.rb
create app/controllers/admin_controller.rb
create app/controllers/managers_controller.rb
create app/controllers/organizations_controller.rb
create app/controllers/people_controller.rb
create app/controllers/roles_controller.rb
create app/controllers/users_controller.rb
create app/helpers/accessibilities_helper.rb
create app/helpers/admin_helper.rb
create app/helpers/managers_helper.rb
create app/helpers/organizations_helper.rb
create app/helpers/people_helper.rb
create app/helpers/roles_helper.rb
create app/helpers/user_sessions_helper.rb
create app/helpers/users_helper.rb
create db/schema.rb
[/code]

As you can image it would be very time consuming to copy these files manually every time you want to add this module to your MVC application!!!

Removing this security model’s files is quite easy as well:

[code]
…/testarea$ thor authena:uninstall
remove app/models/accessibility.rb
remove app/models/address.rb
remove app/models/contact.rb
remove app/models/organization_binding.rb
remove app/models/organization.rb
remove app/models/person.rb
remove app/models/role.rb
remove app/models/software_application.rb
remove app/models/user.rb
remove app/views/accessibilities/_access.html.haml
remove app/views/accessibilities/change_user_role_state.js.rjs
remove app/views/accessibilities/invalid_state_change.js.rjs
remove app/views/layouts/admin.html.haml
remove app/views/layouts/organizations.html.haml
remove app/views/layouts/user_sessions.html.haml
remove app/views/layouts/users.html.haml
remove app/views/organizations/edit.html.haml
remove app/views/organizations/_form.html.haml
remove app/views/organizations/index.html.haml
remove app/views/organizations/new.html.haml
remove app/views/organizations/show.html.haml
remove app/views/people/cancel_access_edit_area.js.rjs
remove app/views/people/edit.html.haml
remove app/views/people/new.html.haml
remove app/views/people/_roles_by_user.html.haml
remove app/views/people/show.html.haml
remove app/views/people/destroy.html.haml
remove app/views/people/index.html.haml
remove app/views/people/_people_list.html.haml
remove app/views/people/show_access_edit_area.js.rjs
remove app/views/roles/_action_menu.html.haml
remove app/views/roles/index.html.haml
remove app/views/roles/show.html.haml
remove app/views/users/_accessibilities_for_users.html.haml
remove app/views/users/change_user_teacher.js.rjs
remove app/views/users/_form.html.haml
remove app/views/users/show_assign_to_teacher_area.js.rjs
remove app/views/users/cancel_assign_student_to_teacher_area.js.rjs
remove app/views/users/edit.html.haml
remove app/views/users/new.html.haml
remove app/views/users/show.html.haml
remove app/controllers/accessibilities_controller.rb
remove app/controllers/admin_controller.rb
remove app/controllers/managers_controller.rb
remove app/controllers/organizations_controller.rb
remove app/controllers/people_controller.rb
remove app/controllers/roles_controller.rb
remove app/controllers/users_controller.rb
remove app/helpers/accessibilities_helper.rb
remove app/helpers/admin_helper.rb
remove app/helpers/managers_helper.rb
remove app/helpers/organizations_helper.rb
remove app/helpers/people_helper.rb
remove app/helpers/roles_helper.rb
remove app/helpers/user_sessions_helper.rb
remove app/helpers/users_helper.rb
remove db/schema.rb
[/code]

Dr. Rick Warren- 2010 Rails Rumble Panel Judge

Dr. Rick Warren

“48 hour web application development competition”

Rails Rumble Expert Panelists include people that do great stuff from all parts of the web development and startup ecosystem: web developers, programmers, designers, user experience experts, startup and venture business folks, and media people. The panel also includes a number of influential Ruby community leaders.

Pastors can be tech stars too! You’ve probably heard heaps about his global 2 year best seller “The Purpose Driven Life,” but what you probably didn’t know is that his tech-savvy church, Saddleback, was the first church on the Internet, way back in 1992. That’s before IE or Netscape – and using Mosaic, Gopher, & FTP! THE NEW YORKER says, “Like Ray Kroc and Sam Walton, he pioneered new delivery systems,” and FORTUNE MAGAZINE says, “America’s pastor has a plan to mobilize millions to fight poverty, illiteracy, and AIDS in Africa, and he’s got the management genius to do it.” It’s a lot for a Ruby hacker to look up to and be proud of, and Rumble 2010 is stoked to have Dr. Rick Warren on our panel of experts.