Ruby already uses the class name of my model

I am making a forum application with various levels of authorization, one of which is Monitor. I do this by expanding my User class, and I plan on fine-tuning this with the -ship classes (e.g. administrators, authorship, moderator, etc.). Apparently, the Monitor class is part of ruby ​​mixin. How to save a resource name without collisions?

+1
ruby namespaces class ruby-on-rails conflict
source share
3 answers

Some features:

  • avoid calling require 'monitor.rb' which pulls out a standard monitor instance
  • run some runtime masks to rename the existing Monitor class.
  • monkey from your boot path to require 'monitor.rb' pull in the empty Monitor implementation.

But in all cases, you may encounter a situation where a third-party library uses Monitor, expecting it to become the standard Monitor class. So, I would advise with none of the above.

I would say that you have only two reasonable options:

A) you can put your class in a namespace:

 Module MyApp class Monitor #... end end 

if your application uses some kind of manual with automatic requirements (for example, an application for rails), then you will add your implementation to / my _app / monitor.rb. When you want to access this class, you will do something like my_monitor = MyApp::Monitor.new() or something else.

B) you can use a different class name :)

+6
source share

Declare your Monitor class in another module.

 module MyModule class Monitor end end 
+2
source share

FYI, I just found a neat trick (errr, hack) to get around this, which might work.

I am working on a large legacy application, which unfortunately has the Fixture model, which is very important and is used everywhere. When you run tests, you cannot create an instance of Fixture because of the Fixture class used by ActiveRecord when running the tests. So I did the following:

 FixtureModel = Fixture.dup 

This freezes my class in place, so I can reference it later (but only in my tests!) Without being an extended ActiveRecord Fixture class (which does not fit in names)

+1
source share

All Articles