Where to put code from a preliminary gem in a rails app? - ruby-on-rails

I'm working on a government transparency app. For the app I'm using two different APIs from the sunlight foundation. One already had a good gem wrapper that provided all the functionality I needed. The other didn't, so I wrote the code myself with the plan of packaging it as a gem and using it in my app. The code is done, but needs some refactoring as well some setup to properly turn it into a gem. Unfortunately, I need to finish the app quickly for a demo I'm doing in a couple of days and just want to add the code to my existing app for the moment and then extract it out later to make a gem.
I'm a new programmer(about 3 months), so I'm still getting some of the convention and what not. What I'm not sure is where to put the code in my rails app. Does it go in the model, controller, or do I build a separate helper module. The code for the app is here: https://github.com/gbcosgrove/sunlight-influence. It consists of an API call constructor and then separate files for each category. I'm still in the process of refactoring because I find it easier to just write code that works and then go back and iterate until its clean.
Any insight on where to put this code in an existing rails app not as a gem would be awesome. Thank you so much!!

Related

How to integrate a Rails application with Angular2 in a single application?

I know there are easy ways to get AngularJS working as the front end of a Ruby on Rails application such as the angularjs-rails gem, but I haven't been able to do the same thing with Angular 2. I know I could just make 2 different applications and just have them work together, but I'd rather have them in 1 app so I can just start the one app and it'll all run and do all the testing in one app at one time.
As no "rails-angular2" gem has been released yet, you can do the integration by yourself following this link: http://programming.sereale.fr/20...
It is an example of integration without npm, but if you want to use npm you may still find a few interesting clues.

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.

Setting up ruby on rails gem ga_events

I am relatively new to web dev and I am trying to set up the ruby on rails gem ga_events (https://github.com/Nix-wie-weg/ga_events). I am doing this because I need to have google analytics event tracking within controllers and this gem seems like a good way to do it. The issue I am having is where the readme says, "After requiring ga_events.js, you have to choose an adapter." I am using Google Universal Analytics and thus I would like to choose that one. It doesn't say specifically how to do this. Where do I put that code block (https://github.com/Nix-wie-weg/ga_events#google-universal-analytics-analyticsjs)? Do I need to make a new file somewhere?
I'm assuming it's obvious for a more experienced developer. More detailed instructions+explanation would be really helpful. Thanks.
I'm using Rails 4.0.13 and Ruby 2.0.0p643.
I was having the same issue here! Turns out, it is just javascript code.
I put mine under application.js since it's a code that need to be on every page.

What would be ideal way to share functionality between Rails apps?

I have a number of view helpers I use on almost every project, a set of useful rake tasks, minor extensions to active record, extensions to some gems (inherited_resources).
I'm wondering, what would be a good way to manage these 'snippets'? I wouldn't want to manage a gem for each snippet, or even a gem for each 'type' of snippet. Would it suffice to bundle this into a personal gem? Maybe add the option to specify which helpers/extensions to include in the project?
I could use a 'template' application which I could bundle with this code, but the problem here is if I update a snippet on one project, I want to be able to rollout that update on all projects with minimal effort (i.e. bundle update).
With your requirements, I would bundle it all in a base-zenph-gem and use it in every one o your projects, as it is the best way to have synchronized code over different projects.
Also, make a good documentation for it, as if anyone inherits one of those projects would love to know what is going on.
Instead of a gem with Rails you can create an engine which contains reusable functionality, then you can specify the use of the engine within your applications.
You can read more about it in the Rails documentation: Rails Engines

Running Rails as an embedded app inside of a gem

I'm trying to understand what exactly the above (in my question's Title) means? This is taken directly from the SpreeCommerce.com project:
If you’re an experienced Rails developer you may be wondering where your app directory is. Spree actually runs as an embedded Rails app inside of your gem. How do you customize things then? We’ll cover that later in extensions.
Source: http://spreecommerce.com/documentation/getting_started.html
Can someone further explain what exactly it means when a Rails app is run "inside of your gem"
With the gem spree, you can install your application and use it. A lot of application need download complete package to install it. When the gem spree, you don't. So it's more easier to install spree on your server.
The phrase you quote is poorly written and not particularly useful. What you should take away is that Spree is structured different from the majority of Rails plugins.
Typical plugin:
your rails app <-- plugin functionality
A Spree app:
spree rails app <-- your site specific code
Typically, most Rails plugins are installed in the vendor/plugins directory of your Rails app. Some additional functionality is added by classes and modules that you can then reference in your code (subclassing a ResourceController, for instance).
Spree does not work in this way. Because, presumably, there is so much configuration code for Spree, each Spree instance creates a separate Rails app -- one that's missing some of the more important parts of a Rails app (such as the app directory). All of your site specific code goes in the vendor/extensions/site directory. This means you don't have to worry about editing any of the Spree-specific code (since it's all in a different directory) and you can more easily put your own code under source control.

Resources