Plain old Ruby object location in Rails - ruby-on-rails

Rails n00b question. I am creating a plain old Ruby object in Rails. But I am not sure if there is a standard location in the app that I should put this object.
Thanks

Several choices, but the lib directory is the most common.
If you need to initialize the code, the most common place is config/initializers.
So you might have:
lib/mystuff.rb
and:
config/initializers/mystuff.rb
lib/mystuff.rb
contains your ruby code.
config/initializers/mystuff.rb
contains code that initializes your stuff, whatever Ruby files that are found in config/initializers are run when Rails boots up.

Related

Configuring Sublime Text 3 for Ruby on Rails development

I am a beginner in this whole thing. I have previously used Sublime for HTML and CSS practice. Now I want to work on a website using Ruby, but do not know how to set up Sublime for rails, since every tutorial for Ruby has those files in the left side of the Sublime window, like App directory and similar generated somehow. I would not like to work blindfolded. I have tried to find a way to generate those directories, but did not find any step by step instructions. Which are welcomed in this case. I would appreciate those, or if there is a link to some detailed tutorial on how to do that. Thanks in advance for any help!
There is a blog that I hope it would help you in your case.
Here it is: Setting up Sublime Text 3 for Rails Development
The directories you're talking about (app, config, etc.) aren't generated by SublimeText; they're generated by Rails when you create a new application with rails new at the command line.
Once you've created the application, you can open the top-level directory in SublimeText, and you'll see all the directories in your sidebar. For example, if you keep your apps in a directory called my_app, and you want to create a Rails app called new_app, you can do this from your terminal:
cd my_apps
rails new new_app
subl new_app
And you should see something like this:
For more on how to get started with Rails, I'd recommend Michael Hartl's Ruby on Rails tutorial.

Is it possible to create a ruby gem that when added to a rails project simply appends code to its initializers?

I've got some helper code (monkey patches) I tend to carry around from rails project to rails project via simply copying files or pasting code into the initializers folder.
I think a clean way to deploy different categories of my initalizer code would be to simply add gems to my project Gemfile that would do the equivalent of this for me. I'm not very skilled at creating gems or the deeper side of ruby on rails engineering so I just wanted to know if this were possible before I got started with it.
Yes, this is definitely possible. It should be probably enough to make a simple gem, put your monkey-patches there and require them in the gem entry point (e.g., if the gem is called foobar, then bundler will require lib/foobar.rb by default).
There is a guide on creating gems available in Bundler docs. In case you need to interact with Rails application configuration, you can take a look at Rails Engines.

Add a basic UI layer to existing ruby app

First, I am fairly new to Ruby/RoR and so you'll have to forgive me for any wrong terminology, but hopefully I'll get my point across.
I built an ruby app that I am needing to add an extremely simple UI layer using rails. Read up on a previous post of mine that explains the project thoroughly to give you good an idea of what it does. Specifically take a look at the tree outline that I pasted in so you see the existing file structure for the project.
What I need to know, is how to convert this existing project into a rails app? My experience in building something with rails has always started out with rails new app_name, but never anything like this. Any tips would be appreciated.
I saw your parser script, and it is not a daemon (a program that keeps running indefinitely in the background), right?
If I'm right, then you have several options:
The easiest option
Just build a rails application using rails new app_name, and inside some controller action, make a system call to run your script
class SomeController
def some_action
succeeded = system(:ruby, '/path/to/main.rb', '/path/to/some.txt')
# Do some rendering stuff here based on the result of the system call
end
end
This approach is somehow nasty for me, and it's not performant because each system call reads your ruby script and compiles or interprets it then runs it.
The harder option
Refactor your script so that it's features can be wrapped into a gem.
Then you install that gem, require it in your rails app, and use it.
I saw your original ruby script is almost there, it shouldn't be that hard to make it become a gem.
Rails is just "something" on top of Ruby. Especially, you can use any plain ruby objects inside of Rails, anywhere, and this is nothing unusual (google "PORO").
In your case, I would make a simple Rails app in the way you have mentioned yourself with rails new. Then trivially refactor your existing code until you have a simple, standalone class that does what you need to be done but takes its input/output from simple ruby data structures (i.e., method arguments, return values, no global state, no file operations). Then you can use that class from inside your Rails controller (taking input from a HTML form, rendering output to HTML), and also from inside your script (reading input from a file or STDIN, rendering output to STDOUT).
Where you put that class is up to you. In the MVC paradigm, it is not "C" or "V", and one could argue about whether it's "M". So put it into app/models/ or lib/, whatever you like more.
These were great answers and I'm sure they would have worked perfectly. However, they were a little bit more complex than what I was looking for.
What I ultimately ended up doing was just cd into the directory above where the ruby app was located and then just simply ran rails new app_name. Rails will ask if you'd like to overwrite any files that exist already. From there I just integrated my script into the controller actions and created the views.

Ruby on Rails - using modules from a different directory

This is a very basic question, but I can't find exactly the answer I need.
I have the following code in trunk/app/models/parsers/my_file.rb in my dev environment:
def initialize
...
#logger = Utils::SingletonLogger.get_logger
#logger.debug("Instantiating my_file object")
end
It runs fine.
But when I deploy it to the test environment (all code should be identical, but I'm not sure where to start looking for differences if there are any), Rails complains that it can't find Parsers::MyFile::Utils. The Utils module I want to use is in app/lib/my_utils.rb. This makes me think that Rails is creating some sort of namespace for the code in the parsers sub-directory, and only looking there for the Utils module, but I haven't been able to figure out how to make it work for me. Is there some main, application level prefix I can use to specify to look outside of the current directory structure?
I've tried adding require 'my_utils' and require_relative '../../../lib/my_utils.rb'. The former can't find the file, the latter just throws the same error as when I don't have any require at all. I'm not sure if I should have to require this or not.
In any case, I clearly don't quite understand how to refer to code in modules in a different directory, I don't understand when/why rails needs an explicit path at some times/environments but not others, and I don't know how to make Rails look outside of the current file for code. Any help with any of these would be appreciated.
Oh, I'm using Ruby 1.9.3, and rails 3.2.1.
Edit: It just started working, without any changes to the application.rb or environment files. It doesn't even have a require in the current version. Is there any obvious reason for it not to work at first, then to work after another server restart? (I'm pretty sure I restarted it after the code went in - I don't think I just forgot to do that before.)
Anyway, thanks for your help - I really do appreciate it.
You can manually add directories you want to include in application.rb. Might want to make sure in your application.rb or test.rb config files you have this autoload_paths in there, yours might be specific to your development.rb file.
config.autoload_paths += %W(#{config.root}/lib/)

How to integrate an already-existing .rb file in Rails?

I am still learning Ruby on Rails, but have a general question about using a link on a Rails view to trigger a ruby program to run. In other words, rather than type "ruby filename.rb" at the command prompt, I want a link in my Rails view to execute the code in filename.rb, when it's clicked.
I know this is a bit of hack, but I'm trying to learn one step at a time...
If you're using Ruby 1.9.x you can use Process.spawn;
Process.spawn("ruby #{Rails.root}/my_ruby_file.rb")
Copy that file in the lib directory and use require "already-existing.rb" in your rails view helper and call the methods from that file in your view .

Resources