#if forum /* May 14, 07:05 */
Hi all
I’d like to begin with clean development of engines that I will be able
to re-use and give to the Rails community.
Now my question: what should I always keep in mind when writing code for
an engine? I’m thinking here about stuff like that I shouldn’t hard-code
table names etc.
I’ll dive in on this last question:
There’s no need to hard-code table names, have a look at:
user_engine/lib/user_engine.rb, notably this part:
The names of the new Role and Permission tables
if ActiveRecord::Base.pluralize_table_names
config :role_table, “roles”
config :permission_table, “permissions”
else
config :role_table, “role”
config :permission_table, “permission”
end
IIUC you could override the actual table names by placing directives
into $RAILS_ROOT/config/environment.rb, like this:
module UserEngine
config :role_table, “my_roles”
config :permission_table, “my_perms”
end
Then, when you actually come to use the table names, say for example in
a JOIN thats tucked away in your code, you can pick out the table name:
UserEngine.config(:role_table)
and use it like:
:joins => "INNER JOIN #{UserEngine.config(:role_table)} ON " +
“#{UserEngine.config(:role_table)}.somefield = " +
" some_table.some_column”
And if you want the the class name, you could do:
UserEngine.config(:role_table).classify
which would pick out “Role”
using these techniques, i believe you can decouple the table names and
classes from your code.
Although, the class names may still conflict with other ones, if you
choose generic names.
HTH at least a little.
–
keys: http://codex.net/gpg.asc
[a] The Japanese eat very little fat and suffer fewer heart attacks
than
the British or Americans.
[b] On the other hand, the French eat a lot of fat and also suffer
fewer
heart attacks than the British or Americans.
[c] Conclusion: Eat what you like. It’s speaking English that kills
you.