Is it possible to see the underlying Ruby code in Rails? - ruby-on-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.

Related

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.

Convert a rails engine to an app

I would like to take the community_engine source code and change it so that it can be run as a its own application. The reason I would like to do this is because community_engine contains basically all of the code that my app will use, however much of it needs to be changed or overridden.
I figure that it doesn't make much sense for the app directories sole purpose being either to override a ton of stuff in the engine or to project the engine source that I have modified locally.
I also really want to do this so I will be able to easily see all of the code being used in the app in one place which will make things easier to understand and change.
Heres the community_engine repo: https://github.com/bborn/communityengine
I've asked similar questions as this one in the past but people always seem to think that I just want a to copy the source code to my machine to use locally so hopefully I was better at explaining myself this time:
using devise WITHOUT the gem, can I simply copy the files?
How to convert a large gem to standalone rails app
You could copy the engine in to app_root/engines/community_engine and point the Gemfile to the local path.
gem 'community_engine`, path: 'engines/community_engine'
The engine code can then be editted directly.

Can multiple Rails Applications share models/common business domain data between them?

I have several use cases that manipulate and add to the same data at different points in the process.
Each of these use cases share many of the same models, and actions in the process but would require totally different views and structure.
I was thinking of trying out the tips in this article from 8thLight but this was written in 2007.
http://blog.8thlight.com/jim-suchy/2007/02/20/sharing-a-database-among-multiple-rails-applications.html
According to them, the trick is to
(1) make a new folder with the shared models right above the application.
applications_in_same_business_domain
|-shared_models
|-application1
|-application2
|-...
(2) require this new folder or module in your application via enviorments.rb file
(which I believe the equivalent would be config/application.rb because I don't see enviornments.rb in Rails 3.)
They say some code like this will work (in environments.rb)
$: << File.dirname(__FILE__) + '/your_lib_path'
I tried this and it isn't working (in application.rb)...
config.autoload_paths += %W(../../../mardom_shared_models)
Is this the standard way to do this?
An API sounds like another way to do this...but...I don't know anything about API's here. Self-learning 6-Month Noob here)
Helping me get the above to work if it is possible I guess would be the specific question. Can I do this?
But any comments or articles on other matter would be appreciated.
I would need to modify the Rails generators here starting from this link: http://guides.rubyonrails.org/generators.html
I would rather put all models related stuff into a gem, and install gem locally to vengor/gems directory to simplify navigating in it.

Building an extension framework for a Rails app

I'm starting research on what I'd need in order to build a user-level plugin system (like Wordpress plugins) for a Rails app, so I'd appreciate some general pointers/advice. By user-level plugin I mean a package a user can extract into a folder and have it show up on an admin interface, allowing them to add some extra configuration and then activate it.
What is the best way to go about doing this? Is there any other opensource project that does this already? What does Rails itself already offer for programmer-level plugins that could be leveraged? Any Rails plugins that could help me with this?
A plugin would have to be able to:
run its own migrations (with this? it's undocumented)
have access to my models (plugins already do)
have entry points for adding content to views (can be done with content_for and yield)
replace entire views or partials (how?)
provide its own admin and user-facing views (how?)
create its own routes (or maybe just announce its presence and let me create the routes for it, to avoid plugins stepping on each other's toes)
Anything else I'm missing?
Also, is there a way to limit which tables/actions the plugin has access to concerning migrations and models, and also limit their access to routes (maybe letting them include, but not remove routes)?
P.S.: I'll try to keep this updated, compiling stuff I figure out and relevant answers so as to have a sort of guide for others.
You might want to check out mephisto, it's a blogging software built with ruby on rails and has add-on plugin support. Not sure if it works how you are thinking of, but might give you some good insights. The source can be found on GitHub.
You should look at deface gem. It allows to customize rails views, adding content via hooks and replacing whole views/partials. Spree is using this gem so you can look also at spree. Beside views they also have other solutions for customizing application so you can find more answers to your questions.

Should I use migrations to create folders?

I created a feature that requires two folders to work correctly. These folders didn't existed in the rails project so I had to create them. Now I need to push this functionality so that the other developers can use it as well.
What do you think is the best practice:
Create the folders locally and leave a note somewhere explaining that these folders needs to be created
Create a migration that will create these folders
Create the folders if they don't exist on execution
So what are the best practices regarding this?
Two choices
1. Put them under version control.
When the developers next check out the folders will get created. How you add them to VC depends on the nature of the tool you're using but many tools require that the directories contain a file (possibly hidden).
That works great if the folders are in the source tree. You also have the potential on a Unix-like os to soft-link from within the source tree to a well known location outside of the source tree. Depending on the VC system (we use Mercurial), you can make the soft link a versioned item.
2. Create them as your process starts
If you're going to do this, you might as well go the extra mile and make their location a configurable option. On start up, read the config file, make the folders and continue.
I would think that as part of the dev builds there must be rake tasks that get invoked to do certain activities; it would be appropriate to either include such activity in a rake tasks where it fits most appropriately.
It should check for folders and if it doesn't exists; just create them. Migrations for creating folder is really not the right way I guess.
I'd include this in a bootstrap rake task.
This is a good question.
For me I would put the folder creation in the migration if and only if the folder was required as part of that migration. So for instance you were adding or updating the table dealing with attachments (such as with attachment_fu etc), then I would consider putting the directory creation/deletion in there.
However creating directories is an environment specific thing (different on Windows vs *nix etc), especially where permissions are involved. Therefore it would make sense to go where these issues are handled in an environment-abstracted way. Perhaps in the deployment script (if using Capistrano) or as other people have suggested in a rake task.
I'll add them to the repository. Maybe if it holds temporary data I'll ignore the contents.

Resources