I'm making a forum application with various levels of authorization, one of which is a Monitor. I am doing this by extending my User class, and I plan on fine tuning this with "-ship" classes (e.g. administratorship, authorship, moderatorship, etc.). Apparently the Monitor class is part of ruby mixin. How do I keep my resource name without the collisions?
+2
A:
Declare your Monitor class in other module.
module MyModule
class Monitor
end
end
Draco Ater
2010-04-22 20:56:00
You don't need the `MyModule::` if you are namespacing the class.
Tony Fontenot
2010-04-22 21:25:34
And put this file under _app/models/my_module/monitor.rb_
Ryan Bigg
2010-04-23 04:13:30
Should I do this with all of the User children? Also, shouldn't the module class be inheriting from User (e.g. class Monitor < User)?
Octopus Inc
2010-04-23 13:33:29
@ryan So lets say I was going to call this module Hierarchy, you are telling me to put my model in app/models/hierarchy/monitor.rb and that will work? Why would I still need this file if it is defined in the module?
Octopus Inc
2010-04-23 13:46:55
+2
A:
Some possibilities:
- avoid the
require 'monitor.rb'
call which is pulling in the standard Monitor instance - do some runtime magic to rename the existing Monitor class.
- monkey with your load path so that
require 'monitor.rb'
pulls in an empty implementation of Monitor.
But in all cases you could end up with the situation where a 3rd party library is using Monitor expecting it to be the standard Monitor class. So, I'd advise against any of the above.
I'd say your only two reasonable options are:
A) you could put your class in a namespace:
Module MyApp
class Monitor
#...
end
end
if your app uses some kind of auto-require magic (e.g it's a rails app) then you would put your implementation in /my_app/monitor.rb. When you wanted to refer to that class you would do something like my_monitor = MyApp::Monitor.new()
, or whatever.
B) you could use a different class name :)
Pete Hodgson
2010-04-22 20:56:29
As mentioned above (MyModule::), how would I go about referencing these classes after I've implemented them? I've read some about modules and mixins before but I've never used a custom one I've made in an app.
Octopus Inc
2010-04-23 13:45:34
I edited my answer to include more details on the namespacing approach. Hope that makes it clearer for you.
Pete Hodgson
2010-04-23 16:16:21
Nice, I like suggestion B too... I will try this and accept if it works out OK.
Octopus Inc
2010-04-27 00:32:40
It seems as though Tony isn't right. The new module does not take precedence in the cascade. Option B is looking better and better...
Octopus Inc
2010-04-27 03:43:12