Invoking ruby classes outside of rails app structure - ruby-on-rails

I have a question regarding how to call ruby classes that reside outside of rails app structure.
I have few Ruby classes that are used/invoked from cron job periodically.
I would like to use the same ruby classes to be invoked from a rails controller
after user creates a model object.
Is it possible to invoke code that resides outside of rails app directory without copying the ruby classes
over. I am mostly concerned that both the classes will be out of sync soon if I have to copy them to rails app folder.
Ruby app reside in, /usr/local/railsapp1 . The ruby classes reside in /usr/local/other_task/
Any suggestions would be of great help.
thanks

You can. Just use in your config/environment.rb
config.load_paths << "/usr/local/other_task/"
This will load whatever classes in that folder into your rails environment.
But the path is hard-coded in this case, so you have to be careful when deploying.

Related

How to properly include code in .rb files into Rails architecture?

I am currently coding an application in Ruby that does some simple external API calls to Soundcloud's API.
I have developed a bunch of code inside a single .rb file and want to put this into the rails architecture. This Ruby file has the following classes:
class SoundcloudUser
class SoundcloudQuery
class SoundcloudFollowers
Currently I understand that I can put these classes into seperate .rb files, and then just put them into the /models/ folder which then gives me the ability to call these classes from elsewhere in my rails application (using require/include).
My question is simply, is this the correct way to go about this? I am familiar with rails, but I am new to transferring a Ruby developed project into the rails format. I tried searching best practices for this in the Ruby style guide but I didn't really find anything.
On a side note - I wanted to also create another class that acts as a ?service? wherein in checks my local database if an entry already exists in the database, and if not, then it will query new data. My side-question here would be similar - where would this .rb file for this 'service' live?
I hope I explained my question clearly enough, if not, I am happy to add some clarifications. Thank you for your time!
If in Rails, you can put them in either lib/ or somewhere in the main app directory. For example, you can create app/services and put them inside there, and when you restart the Rails server you should be able to call SoundcloudUser (provided you name them app/services/soundcloud_user.rb.
I always look at the Gitlab source code for this. It's a gigantic Rails app but look at this file: https://github.com/gitlabhq/gitlabhq/blob/master/app/services/gravatar_service.rb. Because it's inside an app/services (any name actually), GravatarService can be called from anywhere in Rails. If you want to have some namespacing, you have to put it in app/services/soundcloud/user.rb or lib/soundcloud/user.rb and name the class Soundcloud::User.
For the class that acts as a service, it seems like it orchestrates the logic of "check if (song?) exists, else scrape. Some people put it in a model class, I'd probably put it in a service class a la the Gitlab source code. Hope I helped.

Convention for capitalising the first letter of a Ruby on Rails file?

Some standard Ruby on Rails files have the first letter capitalised, e.g. Gemfile, Gemfile.lock, and Guardfile. Is there a convention in regard to this, or can I do this for any file I consider it important?
Capitalized files are not Rails application source files, they are files required by some tools. for example, Gemfile and Gemfile.lock are for bundler, and Guardfile is for guard. None of these tools is shipped with Rails, and all of them can be used without Rails.
You have to stick to Rails conventions, because Rails conventions are not only conventions, they have some performance concerns.
For example, Rails forces you to put your model MyModel in the file app/models/my_model.rb. Rails will not load this file until this class is used. Rails assume that the class MyModel is defined in a file named my_model.rb, and is located in one of some special directories called autoload_paths, so Rails searches these directories for this specific file and loads it. If no such file, or there's no class MyModel defined in that file, then an error will be raised. Using this lazy loading mechanism, Rails can keep those never used (perhaps legacy) classes from being loaded into memory.

Why is there Rails.rb files all over the place?

Was digging around my Rails applications and noticed that there are rails.rb files all over the place. In my ruby gems directories like:
...gems\devise-2.0.4\lib\devise\rails.rb
...gems\cucumber-rails-1.3.0\lib\cucumber\rails.rb
...gems\railties-3.2.3\lib\rails.rb
I am assuming that there are executed whenever you issue some command like "rails xxx". So all these extra rails.rb files combine with the original rails.rb file to essentially make one big rails.rb file. Essentially, when we type in "rails xxx" it goes thru all them all?
Just looking for some confirmation PLUS a little more knowledge about this. Thanks.
The best way to understand what these rails.rb files are doing, is to read the source code.
ralties
devise
cucumber-rails
As you can see, in any library the file assumes a different scope. The common behaviour is that the file rails.rb normally contains the code required to initialize the library when loaded from a Rails project.
BTW, this has nothing to do with the script/rails command and there is no "big rails.rb" file.
The files are not generated but are simply source files of these libraries you are using.
In this case they are probably rails-related classes that either extend Rails in some way or modify it or make the library interact with Rails.
Rails is a very common framework in Ruby land so most if not all libraries will have some sort of integration with Rails.
By no means are all of those loaded when you run rails XXX but rather when your application loads these libraries their rails.rb files may be executed to provide some sort of integration with Rails.

Updating to Rails 3.2.2: How to properly move my plugin from the '/vendor' to '/lib' directory?

I am upgrading Ruby on Rails from 3.1 to 3.2.2 and I would like to know what I should make and at what I should be care in order to properly move my vendor plugin (note: it is not a gem and at this time I am not planning to make that a gem) from the directory /vendor to /lib as well as wrote in the official documentation:
Rails 3.2 deprecates vendor/plugins and Rails 4.0 will remove them completely. You can start replacing these plugins by extracting them as gems and adding them in your Gemfile. If you choose not to make them gems, you can move them into, say, lib/my_plugin/* and add an appropriate initializer in config/initializers/my_plugin.rb.
I refer mostly to the "an appropriate initializer in config/initializers/my_plugin.rb": What code should I put in that file?
More: Do you have some advice or alert on making the above process?
The initializer should contain the appropriate requires and other startup related tasks that are necessary for your plugin to work correctly. It's difficult to help you without real code examples from your app but this link should help you get started.
http://code.coneybeare.net/how-to-convert-simple-rails-23-style-plugins
The example in the link requires the plugin (now in the lib directory) and adds a module to ActiveRecord::Base.

Adding standalone Ruby Files to a Ruby on Rails project

I have a ruby on rails project with scaffolding , views, layouts, models and such. I also have some standalone ruby (.rb) files which i would like to include in my afforementioned project. Is it possible to simply call these methods defined in these rb files by placing them somewhere and calling them from the controller or so?
If not, how can i go about and add them?
Any assistance would be highly appreciated thanks
Place your code in the lib folder, then create a new initializer file in config/initializers and require the files u want.

Resources