Ruby Forum Ruby > sharing classes in rails and with other ruby scripts

Posted by Shawn Bright (nephish)
on 17.08.2008 07:30
(Received via mailing list)
Hey there all,

I have several Ruby classes that I need to share between several
applications.
Our company collects information from industrial machines, processes it, 
and
alerts our customers of system changes. This is either by text message,
website, etc..
So i have a ruby on rails website built that allows them to see whats 
up.
Then, we have other companies we build info sites on, kinda based on the
same code, but private labled, and modified in other ways too.

Here is what I would like, we have several websites that all have a
EquipmentSensor model, a ruby class of type ActiveRecord::Base, we also 
have
a folder of models that we 'require' to use in our data processing 
software,
and in our cron jobs, etc...

Is there a way i can share this class ( model ) with several different
websites running rails and also use it in my cron jobs, etc..

thanks for any tips.

sk
Posted by Joshua Ballanco (jballanc)
on 19.08.2008 03:17
Shawn Bright wrote:
> Is there a way i can share this class ( model ) with several different
> websites running rails and also use it in my cron jobs, etc..

Well, ActiveRecord is available as it's own gem, and a class which 
inherits from ActiveRecord does not need the full Rails framework to be 
useful (a bit of Googling should give you examples of ActiveRecord used 
outside of Rails...or you could just look at Camping: 
http://code.whytheluckystiff.net/camping). If you wanted, you could keep 
your model collection in one place (say, /usr/lib/mycompanysstuff), and 
then you would just need to modify your environment to require the model 
classes from there...

...however! If you are constantly accessing the models from Rails apps, 
it would probably be worth your while to look into setting up a 
dedicated Rails app to serve up the model data and then access the data 
from your other apps using ActiveResource. That solution has the 
advantage of being scalable as well (...and ActiveResource is just dang 
cool!).
Posted by Shawn Bright (nephish)
on 19.08.2008 05:54
(Received via mailing list)
Thanks for this, I am using these models in apps that are not rails apps
andy yes, It is pretty easy to do. I had some advice in the rails irc 
where
someone recommended the ActiveResource way to go. However, after having 
read
some on it, i don't think i can employ it here because it is pretty new 
and
my rails version is 1.2.4  If i upgrade to rails 2, i run the risk of
breaking my site (which is pretty large now) .

Thanks for your attention on this Joshua, and if you know of a good 
trick
that might work for me I would welcome any suggestions.  Actually, it is
only about 4 of the models that i want to share, but if i do 4, i may as
well do them all.

thanks again
sk
Posted by Joshua Ballanco (jballanc)
on 19.08.2008 07:34
Shawn Bright wrote:
> Thanks for your attention on this Joshua, and if you know of a good 
> trick that might work for me I would welcome any suggestions.

Well, I think it's important to remember that Rails is still Ruby, so 
all the good ol' Ruby tricks still work. In other words, you can always 
pull out common code into a lib and 'require' it. This would even work 
for ActiveRecord subclasses.
Posted by Shawn Bright (nephish)
on 19.08.2008 15:42
(Received via mailing list)
cool, thanks for this.
sk