Sunday, February 25, 2007


I have been introduced to rails for about 30 days ago. I really don't spend much time on developing my coding skills. I almost know nothing about the ruby syntax, I took the shortest way and dig into my tasks.
Many people think that the best way is "do it yourself"; don't spend much time on reading tutorials, couple of days are enough and then release your skills to go further. me too, I believe in this. But I think also, that you have a good view to know when to stop digging.
It's true that i did good progress in my programming tasks, but i still find that this is not enough. I believe it's time to make some readings to enlarge my dictionary.

Hunting the best opportunity to do so is the most important part. It's not a silly H.W you do to raise your salary. It's the willingness of tasting some craft.
We were working on a small forum, when it comes to define a forum/thread to be closed, blocked, forbidden..bla, bla...whatever anybody calls these restricted access issues.
Yeah, it's simple. A tiny flag in some few minutes will solve the whole story...."NOW Let's move to another real task".
I started to think for something different. Why we don't just add some craft to this part; although it doesn't deserve all that panic.

My proposed solution to go for using a rails plugin. I know I may look silly, but Just I felt that I need to make more interaction with plugin.
I made a quick lookup for the available plugins, it was difficult at the beginning as I was looking for something without knowing what it can be called. Finally, I found the acts_as_enumerated plugin, it should be the one. I succesfully installed it, at the end it didn't work with me ( I may be missing something) but i decided not to give aq second try for now. I thought it doesn't satisfy my design. Instead i started to think of something which should like this:
Did I make something wrong here? It seems no sense to make a relation of many-to-many. Why could any forum have more than one state!!!
Well, I made this on purpose. What if we want in the future to expand our feature to be supported on the user-level. I mean, we may want to support (for example) "block option" for a forum on the user-level. In this case the forum will have more than one state depending on the user. We will need to add to the staging-table a new column carrying the user_id. Let's keep it simple for now, this is my first plugin, I don't want to get stuck.

Steps to create a plugin:
Note: the following steps are provided as guidance to create plugins.

1. In your project directory run the following command.

$ruby script/generate plugin acts_as_statable
create vendor/plugins/acts_as_statable/lib
create vendor/plugins/acts_as_statable/tasks
create vendor/plugins/acts_as_statable/test
create vendor/plugins/acts_as_statable/README
create vendor/plugins/acts_as_statable/Rakefile
create vendor/plugins/acts_as_statable/init.rb
create vendor/plugins/acts_as_statable/install.rb
create vendor/plugins/acts_as_statable/uninstall.rb
create vendor/plugins/acts_as_statable/lib/acts_as_statable.rb
create vendor/plugins/acts_as_statable/tasks/acts_as_statable_tasks.rake
create vendor/plugins/acts_as_statable/test/acts_as_statable_test.rb

2. Edit "vendor/plugins/acts_as_statable/init.rb" to look like this

require 'acts_as_statable'
ActiveRecord::Base.send(:include, ActiveRecord::Acts::Statable)
require File.dirname(__FILE__) + '/lib/stating'
require File.dirname(__FILE__) + '/lib/state'

3. your real work will be in this file "vendor/plugins/acts_as_statable/lib/acts_as_statable.rb"
you will need to define your methods just right here

module ActiveRecord
module Acts
module statable

4. create the new classes stating and state

class Stating < ActiveRecord::Base
belongs_to :state
belongs_to :statable, polymorphic =>true

class State < ActiveRecord::Base
has_many : statings

No comments: