Add a basic UI layer to existing ruby app - ruby-on-rails

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.

Related

Is it possible to see the underlying Ruby code in Rails?

I'm currently doing my Final Year Project so I need to include code as part of the submission. Rails generate directories of different files, rather than a block of code that can be copy pasted. If there isn't is there a workaround to submit Rails code in a presentable manner.
Yes, it is technically possible. Here is an example of where it was done:
https://gist.github.com/clupprich/0e8b816883ca4dac6b7632a9e8351c48
This was done manually though, so if you have an existing application, the only way to do this is manually.
Also, I would not suggest this, as the directory structure is setup the way it is for a reason.

How to generate a dummy app inside an already existing rails engine

I came to work for a company recently that has been working on a specific rails project for a while. Dropped in the middle of the development process, I'm beginning to go back and write tests for the existing code as well as the code currently being produced.
Testing the rails 4 app was easy enough, but once I got to testing the engine, I hit a mental block. After doing my due diligence, I found that most people take an approach like this (http://viget.com/extend/rails-engine-testing-with-rspec-capybara-and-factorygirl), but the common thread I saw in all of these responses was that people were building these apps from scratch, and thus had a dummy app generated for them automatically. I don't have the benefit of being able to generate everything from scratch, and have to work with what I was given, so what
I'd like to know is if there's a way to retroactively generate just the dummy application. Is there something simple I can type into the console and have it generated for me? Or is there a longer, slightly less pretty route? Or would a different strategy be better altogether?
Given that the dummy app is not supposed to be tied to the parent gem except for some vague names, that may do it:
cd some_path_where_your_engine_IS_NOT
rails plugin new YOUR_ENGINE_NAME --mountable --dummy-path=spec/dummy --skip-test-unit
mv YOUR_ENGINE_NAME/spec/dummy /real/path/to/YOUR_ENGINE_NAME/spec
rm -rf YOUR_ENGINE_NAME # cleanup useless cruft
Also, you may be interested in this answer to generate the app with the exact same rails version.
If the engine is within a parent app delete the engine from your Gemfile
Rerun the rails plugin new .. command you used to create the rails engine, but with the --skip parameter. This will create all the files, including dummy that do not exist in your engine, but leave existing files alone.

CKeditor not showing Formatted Code

I have installed CKeditor for my Rails app and while doing the Formatting, the Formatted code does not display in the screen, instead, HTML is rendered, like this
<h2><strong>In this project</strong> you’ll create a simple blog system and learn the basics of Ruby on Rails including: Models, Views, and Controllers (MVC) Data Structures & Relationships Routing Migrations Views with forms, partials, and helpers RESTful design Using Rails plugins/gems The project will be developed in five iterations. I0: Up and Running Part of the reason Ruby on Rails became popular quickly is that it takes a lot of the hard work off your hands, and that’s especially true in starting up a project. Rails practices the idea of "sensible defaults" and will, with one command, create a working application ready for your customization. Setting the Stage First we need to make sure everything is set up and installed. See the Environment Setup page for instructions on setting up and verifying your Ruby, Rails, and add-ons. This tutorial was created with Rails 4.0.0, and may need slight adaptations for other versions of Rails. Let us know if you find something strange! From the command line, switch to the folder that will store your projects. For instance, I use /Users/jcasimir/projects/. Within that folder, run the rails command:</h2>
Use the html_safe method
So, something like:
puts my_variable.html_safe
Lots more info here: http://yehudakatz.com/2010/02/01/safebuffers-and-rails-3-0/

How to use acts-as-commentable-with-threading in Rails

I am developing my first rails site (yup, i am a rails idiot).
I'm writing a blog, and i got to the comments part.
I installed acts-as-commentable-with-threading ( GitHub ), i made and ran the migration like the install instructions said.
I have added acts_as_commentable to my Posts model and i have a Comments controller
When i add
#comment = Comment.build_from(params[:id],1, params[:body] )
I get the error.
undefined method `build_from' for #
Clearly i am doing something terribly wrong, and i don't really get the example. What should i be feeding to build_from? Can somebody explain this plugin step by step? :)
Or is there an easier way to get simple threaded comments?
Did you by chance define your own comment model? If so that is going to completely override the model from the plugin that defines build_from in the first place. I ended up getting around this by creating a module with the extra stuff I wanted then creating an initializer to include it, which works perfectly.
As an aside, the first parameter to build_from needs to be the actual commentable object the comment is to be connected to, not just an id.
I'm currently using this plugin in production and can assure you it works :)
Besides the reason of not restarting server (btw you shouldn't use nginx + passenger for development, simple mongrel or thin will do the job better in this case) I can think of two more:
You didn't install plugin (or something wrong happened during installing). However this is unlikely as you could run migration ok right?
You have comment model in app/models and rails doesn't load it from plugin. In this case you might want to try requiring file with plain old require.

Ruby command line MVC framework?

I'm looking to write an app for the shell, *nix mostly. And I'm currently in love with Ruby, especially the 'rails way'.
So if there was a framework that applied rails like concepts to the commandline in ruby then that would be really fantastic.
I'v allready looked into SimpleCommand and Hirb, nothing quite what I was looking for.
To elaborate:
What I'm really looking for is a way to use a rails like (directory and application) structure to create a MVC command line application. So basically something like rails that doesn't respond to http, but instead reads and writes to the console.
Its not a shell, if i wanted that then irb works fine. It would be more like your options are A,B,C and they would work a bit like http links.
You should take a look at boson and hirb [2d] menus. First one for creation of commands, second one - for A,B,C options and custom/dynamic views for outputting data.
Maybe you should try script/console in your rails app. Is that what you wanted?

Resources