Bundle a rails 4 application into a gem? - ruby-on-rails

I am currently developing a rails 4 based media streaming app and wanted to know if i could have the rails app as a kind of source code and bundle it into a gem for distribution? is there some sort of easy solution for this? or do i have to roll my own?

From your description, I think you can do this via an Engine. From the Rails docs:
Engines can be considered miniature applications that provide functionality to their host applications.
...
Engines are also closely related to plugins where the two share a common lib directory structure and are both generated using the rails plugin new generator. The difference being that an engine is considered a "full plugin" by Rails as indicated by the --full option that's passed to the generator command, but this guide will refer to them simply as "engines" throughout. An engine can be a plugin, and a plugin can be an engine.
Read more at http://guides.rubyonrails.org/engines.html

Related

Can you use a Rails Engine with a JS Framework like Vue

I want to do a project with https://github.com/projectblacklight/blacklight
Can I use it as a Rails API with a Vue frontend?
It is a Rails Engine that returns JSON but it is traditionally used in a Rails app as a Plugin / Gem (really it's an engine which is similar).
https://github.com/projectblacklight/blacklight/wiki
Every Blacklight search provides JSON, RSS, and Atom Responses of search results
I am researching this and so far I think the answer is yes.
Jason Coyne from Stanford built an App using Ember with Blacklight
Here are some more discussions around the question.
+ https://github.com/projectblacklight/blacklight/wiki/JSON-API
+ https://github.com/projectblacklight/blacklight/pull/588
+ https://groups.google.com/forum/#!topic/blacklight-development/TIYCjemfp3A
There is not a lot of documentation about this that I can find.
blacklight-vue is an engine made explicitly for this purpose. blacklight-vue-demo is a sample build with this engine. The generic answer for building any Ruby on Rails application with Vue is to use the Webpacker gem.

Testing Rails Project/Application Template

There's every chance I've approached this entirely in the wrong way which might be why I'm struggling to find an answer, as is typically the way with Rails.
For clarity, I'm referring to the -m option of "rails new", eg.
rails new [new-project] -m [this is where my template goes]
I've created a project/application template to speed up the creation of a specific project type, with the view that further projects/products will have their own template also.
The template adds in the relevant gems and does a bit of housework to tidy up the output of the gem installation (moving JS/CSS files to /vendors, for example) as well as copying over a generator that gives the option to expand the project further.
I'm struggling to understand if there is a way to test that the project template is working as expected and copying the correct files.
Have I approached this in the correct manner, and is there a way to test project/application templates?
Edit - Further research is suggesting that an Engine may be a better solution. Is that accurate?
A Rails Engine is a way to package some application or application subset so that it is easy to share with other applications. Basically, if you have some models and controllers that can be shared by multiple applications, you can use an Engine. This sounds a lot like what you described as a project template. Engines can be packaged as gems, gems can automatically behave as Engines, include rake tasks, hook into Rails initialization, and more, so why not take advantage of Other People's Work if you can?

Separating Angularjs and Rails apps as standalone components

I wanted to try out Angularjs. However, I have been trouble deciding on where I should located my angular app.
I am using Rails framework for the backend. I have seen tutorials where the entire angular app lives under the assets/javascript folder.
I was wondering if instead of living within the assets/javascript folder, I could make it live outside my rails directory entirely. That way, I can potentially separate my backend and front end entirely. (Is this recommended?).
I believe the asset pipeline also precompiles a lot of the assets. If I were to separate out the angularjs asset, would I need to precompile the assets somehow?
Thanks
I've been working through a similar set of questions. There are some good tools that allow you to integrate AngularJS directly into your rails asset pipeline, and they to me look good if you want just a little bit of Angular.
However, if you want a full Angular front-end, aka a single page web app, I think you'll eventually be limited by compatibility and some of the tooling. I feel like the Rails gems won't quite keep up with Angular, and so you'll be into version conflicts. I've also seen more and more tooling for Angular as a standalone, and I very much like the ng-boilerplate project template. I also like much of the testing tooling such as karma, and I haven't really sorted out a way to integrate karma with rails.
For that reason, I eventually decided that I'd keep the two separate. Initially, I did that through creating a rails application and a separate angular application (separate directories). I used ng-boilerplate as the framework for the angular end. I wrote a tutorial on that. This eventually got a bit frustrating, and I wrote some more thoughts on it, the main annoyance was that I had two git repositories and it was annoying to keep them in synch. It's also sort of annoying working with an IDE across two directories. I've ended up shifting to rails and angular being in the same folder, and they seem to play nice, as each uses different directories within that project.
In this current structure, I'm using the grunt setup that came with ng-boilerplate to minify all the code, package it and also run karma unit testing. I haven't yet nailed the end-to-end testing, but it's on my list. I've found this to be a relatively productive work environment. My chosen structure for my pages, controllers and karma test cases has some repeated code (I'm choosing not to factor it out to maintain readability). I'm planning to extend the rails scaffold generator to create the javascript framework for me - so when I create a persons rails scaffold, it will also create a persons angularjs scaffold for me. I'll update here if and when I do that work.
EDIT: I've completed the scaffolding work as well, which allows rails to automatically generate the angularJS elements when you generate the rails models/controllers etc. The blog post is here: http://technpol.wordpress.com/2013/09/24/rails-generator-to-generate-angular-views/
You could use a grunt based workflow:
How to manage AngularJS workflow with lots of script files
http://newtriks.com/2013/06/11/automating-angularjs-with-yeoman-grunt-and-bower/
If you start with a decoupled frontend, use mocks at first so you can stay within angular and not lose focus switching between backend and frontend logic. An advantage of building a single page application is that you can develop it independently of the backend api. See (http://docs.angularjs.org/api/ngMockE2E.$httpBackend) for information about mocking http responses.
We have been using AngularJS with our Rails application, in a way where we have been using Rails ERB templates, but switch over to using ng directive as and when required.
For this above setup we have used bower/bower-rails gem, which lets us use bower to manage the angular packages and their dependencies. We commit this into our repo, in the javascripts directory, and is taken care of by the Rails asset pipeline.
This setup has worked well for us considering we have above 50-50 % split of our views between the ERB templates and Angularjs.
More about this setup in the links below:
http://angular-rails.com/bootstrap.html
http://pete-hamilton.co.uk/2013/07/13/angularjs-and-rails-with-bower/
http://start.jcolemorrison.com/setting-up-an-angularjs-and-rails-4-1-project/
There are many advantages of separating out your api service (rails in this case) and your frontend components. As we do for ios/android apps, angular client can live on its own as a separate entity. It will be a static website that can be deployed on s3 or any static website host. It just needs to communicate with your api service. You could setup CORS to make it possible.
Some advantages of this workflow
You could use rails-api, which is a subset of rails application. If you are just going to use rails to build apis, it doesnt make sense to have all functionality that a complete rails app provides. Its lightweight, faster and inclined more towards building API first arch than a MVC arch.
You could use yeoman angular-generator to generate an angular app and make the most of grunt & bower to manage build (concat,uglify,cdnify etc) and dependencies (angular modules).
Deployments will become flexible. You won't need to depend on one to push the other.
If you ever plan to change your backend stack (eg rails to play/revel), you would not need to worry about your client components.
By splitting the development of the frontend and the Rails backend you could distribute the work over two development teams and keep the application as a whole very extensible.
There is also one downside to this approach.
By having the applications in two separate repositories, you can’t easily have a full integration test. So you will have to test the apps separately. You could mock your apis to test angular app.
We have been using this approach and would recommend others the same.
Less dependency & more productivity.

Rails 3 - differences between Engines and Plugins

Unfortunately official guide for creating Rails plugins is outdated: http://guides.rubyonrails.org/plugins.html and I didn't find any good tutorial for creating engines only this code: https://github.com/mankind/Rails-3-engine-example and enginex https://github.com/josevalim/enginex were helpful.
So... what are differences between plugins and engines in Rails 3? (I'm thinking of plugins and engines with controllers and views)?
And how can I create plugin with views and controllers? Or maybe in Rails 3 it's better to use engines? Can I use main app model (for example user model, logged user information) inside my engine?
Engines are just required as gems in your app. Since Rails 3.1 you even don't have to require their assets such as .css or .js
In Rails 2.x, Engines didn't exist, so plugins enabled people to have these functionalities but all code was copied inside the app.

Ruby On Rails CMS Framework

I want to create a framework for rails application. It will be a rails application but packed into gem (like a Radiant CMS).
It must work like this:
gem install cmsframework
and then:
cmsframework the_app
After that we have a skeleton framework for a rails app, without any controllers, etc. All controllers are loaded from cmsframework gem.
If I want to rewrite some files (for example public/styles.css), I must simply create it in my app (the_app).
If I want new functions in my app I can create a plugin. But the main functionalities must be loaded from cmsframework gem.
What is the best way to implement this?
Maybe start here: http://guides.rails.info/plugins.html. Pay close attention to the parts about adding custom generators and packaging as a gem. This may help as well: http://railscasts.com/episodes/218-making-generators-in-rails-3.
You can use this framework it is very good CamaleonCMS

Resources