Separating Angularjs and Rails apps as standalone components - ruby-on-rails

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.

Related

Connecting Front End and Backend - Rails and Vue

I am a little confused on this.
If in Rails, with erb files we can build pages layout, 1) in what situations would we use webpacker to add vue.
What i understand so far is a Vue application would at times make a request to the server lets say to populate data. 2) Would that be the only use or are they other uses cases?
Also 3) is it best to generate a vue or angular app as a standalone (like vue create app so vue is in a front end folder and rails in a backend folder) or to use webpacker in rails
I'll try and answer your three questions at once, hope that's ok!
There are three common ways you can integrate Vue.js (or any JavaScript) library into a Rails application:
You can load it through sprockets, which is the built-in Rails asset packaging library. You would do this if your application is mostly going to be statically rendered and NOT a SPA (Single Paged Application). Sprockets is very tightly integrated with Rails, allowing you to refer to assets in your Rails templates and code. However, Sprockets is quite far behind Webpack in terms of the actual bundling/packaging, optimizations, and ease of use for any complex JS project. So you would only want to do this if you're adding a tiny amount of interactivity to a mostly static website.
You can load it through Webpacker. The idea of Webpacker is to give you a really easy way to start building a SPA with a rails backend. So you receive all the benefits of Webpack but don't need to tweek many configurations (at the start), or set up a separate web server, or worry about CORS. It's great for starting a new project/prototyping in or progressively integrating Vue.js into a larger static website. The downside is that Webpacker uses webpacker.yml for its default configuration which adds unnecessary confusion when you will eventually need to have more complex configs.
Finally you can load it through a separate web server ("standalone"), and in the long run, this will give you the greatest degree of freedom through less Webpacker middleware and access to the most updated webpack version. It also offers you the ability to scale, secure, and do fancy stuff like Server Side Rendering. The downside is this does require the most upfront setup and long-term maintenance time. AFAIK this is the most common way companies serve SPAs.

Can a Rails application template include generator parameters?

When I start a new Rails application, I often use certain parameters to rails new like --skip-spring or --skip-turbolinks.
I understand I can put these in ~/.railsrc, but when using multiple dev computers, it can be a bit tedious to keep railsrc files in sync across all of them.
Can I use application templates to add these parameters to the generator? That would be great, because application templates can be referenced by a url and downloaded on the fly.
(Also I know I can use templates to make changes after the application is created, so I can remove spring or turbolinks or whatever, but it would be much nicer to never generate the app that way.)
To answer my own question after looking into how this works, I think the short answer is that it's not possible to add generator parameters from an application template to rails new itself. The reason is that the template code only runs when the application is already generated.

What is the cleanest way to integrate Ruby on Rails with AngularJS?

I am creating a Ruby on Rails application and I would like to use AngularJS in front-end.
I am planning to use the following file structure (from here) for the front-end:
angularapp/
----- shared/ // acts as reusable components or partials of our site
---------- sidebar/
--------------- sidebarDirective.js
--------------- sidebarView.html
---------- article/
--------------- articleDirective.js
--------------- articleView.html
----- components/ // each component is treated as a mini Angular app
---------- home/
--------------- homeController.js
--------------- homeService.js
--------------- homeView.html
---------- blog/
--------------- blogController.js
--------------- blogService.js
--------------- blogView.html
----- app.module.js
----- app.routes.js
I have three main concerns:
1 - I would like my whole angular app to go through asset pipelining feature of Ruby on Rails
2 - I do not want to break the structure of my angular app and move it's files to the corresponding Rails asset folder.
3 - I do not want to put my angular application under assets/javascripts folder since it does not consist of only javascript files.
What is the cleanest way to integrate this structure with my Ruby on Rails application?
I did some research. Turns out the cleanest way to integrate AngularJS with Ruby on Rails is to actually not integrate them in the context of one Ruby on Rails application. Instead, it is best to keep them completely separated as #Minhail suggested.
As Brad Carlton suggests in this article:
One downside to having your entire project tucked neatly into one of
today’s monster frameworks like Rails, Django, or MVC, is that it can
be very difficult for a frontend developer to work on the project.
While it might be simple for a seasoned Ruby dev to setup rvm, gem
install all of the ruby dependencies, deal with native extensions and
cross platform issues. These things are probably not what your
frontend developer is best suited for.
Later on the article he suggests that a better architecture would be to keep front-end and back-end completely seperated:
It also promotes making the frontend a real first class application
and ensuring that it’s truly robust. Hopefully, the frontend
developer is now encouraged to code for the inevitable scenario when
the backend goes down.
What a better user experience to say, “Hey, we’re having some issues
with the server right now, try back later” or even better “The search
service appears to be having issues at the moment, but you can still
view your profile and current projects.”
According to an other article here, Single Page Application and Api Driven Development are two web development trends of 2015. This is a fact which I think strongly encourages the idea of front-end and back-end separation.
A Great example of this with step-by-step walkthrough:
A Complete RESTful Rails-api
An AngularJS Front-End For The Api Above
I have one Ruby on Rails application using AngularJS. I use feature structure too and all of my files are in /assets/javascript/. You should add all js and css files in /config/environments/production.rb to precompile them for production. Example of production.rb:
config.assets.precompile += %w{
topbar/topbar-controller.js
topbar/topbar-service.js
topbar/topbar-directives.js
topbar/topbar.css
# ... other features
}

Is it good to serve static files under Rails' public folder?

I have a front-end single page app written in AngularJS, and I want to use Rails to build a back-end API.
I wonder if it is OK to put all front-end files in Rails' public folder, and mount all Rails' API routes on api/xxxx. In this way, I can save developers' efforts so they don't need to run two server when developing (front-end server and back-end rails' server). Also, I don't need to care about Rails' assets pipeline. I can use totally pure front-end solution to optimize fron-tend application. All I need to do is just put them into one single applications.
Is there any drawback of such design?
Yes, that perfectly fine. In fact, this is what rake assets:precompile does.

Using WordPress + Jekyll for the static pages in a Rails project

I have a website that has product pages(dynamic) and corporate pages(fairly static). Both are coded in Rails. This means the marketing team has to involve the dev team to make any changes to the static pages. To address this issue, I am thinking about migrating the static pages to a CMS.
I am considering following approaches:
WordPress + Jekyll
Marketing team maintains the static pages at a private Wordpress site (eg: foo.wordpress.com).
When the marketing team is ready to deploy the change, a command line tool converts the WP pages to static pages using Jekyll and commits the changes to a git repository.
The changes to the git repository are uploaded to the Amazon S3 bucket associated with the raills application asset.
Marketing team is already familiar with WP and it is quite easy to find a good WP theming resource.
Rails based CMS : Locomotive/Radiant
The CMS resides within the rails app.
Has anybody done this type of deployment. In your opinion which approach is better of the two and why?
I prefer having the CMS integrated in with the Rails app. This way you only have one layout and set of stylesheets to maintain.
Finding a Rails based CMS that makes it easy to integrate into your own application and works the way you want it to might be a bit more challenging. Most of the ones I've seen, the CMS is the application and you have to use its hooks to customize things.
With that said, it isn't that hard to build a simple CMS to use within your own application. Wordpress uses tinymce as the wysiwyg, which isn't that hard to get working in a Rails app. Also, take a look at Refinery, as it may fit your needs just fine.

Resources