Writing a Gem -- How to manage configuration settings? - ruby-on-rails

I'm new to the gem-writing world, and I'm trying to make sure that my gem is as flexible as it can be out of the box. I've got a couple configuration options that will need to be set for things like testing, but I'd like for those options to be able to be overridden at the Ruby on Rails level.
I know that certain gems like Devise do this already, but I'm not sure exactly how it should be accomplished (primarily from the "put these files here, put those files there" kind of perspective). Can anyone give me any tips or suggestions?
Edit: What I'm really looking to know is how these gems manage having configuration settings defined locally to the gem and having them defined in Rails, and defining for the Rails application which takes precedence.

there are gems that help you create rails initializer style configuration.
an example is https://github.com/phoet/confiture/

Related

Is it possible to create a ruby gem that when added to a rails project simply appends code to its initializers?

I've got some helper code (monkey patches) I tend to carry around from rails project to rails project via simply copying files or pasting code into the initializers folder.
I think a clean way to deploy different categories of my initalizer code would be to simply add gems to my project Gemfile that would do the equivalent of this for me. I'm not very skilled at creating gems or the deeper side of ruby on rails engineering so I just wanted to know if this were possible before I got started with it.
Yes, this is definitely possible. It should be probably enough to make a simple gem, put your monkey-patches there and require them in the gem entry point (e.g., if the gem is called foobar, then bundler will require lib/foobar.rb by default).
There is a guide on creating gems available in Bundler docs. In case you need to interact with Rails application configuration, you can take a look at Rails Engines.

How to convert a large gem to standalone rails app

I'm building a social network and have been using the gem community_engine but have been having trouble implementing the large amount of customization that I need for my app. I figure this will make it easier for me to override and add methods, as well as help me to better understand and learn from the code since I will be able to actually see all of it in action.
So far in my attempt I downloaded the source code, added the default bin directory and config files that were missing, as well ass all the gem dependencies.
What else do I need to do to get the app to work? I realize that there may still be a lot and that it might not be easy to explain, but at the very least is there any sort of documentation out there that might help me understand how to convert the gem to a Rails application?
Heres the community_engine repo: https://github.com/bborn/communityengine
Because this idea may draw some criticism, I'll add that I was originally building the app without any huge plugins accept for devise however I'm running out of time to finish this.
More stuff I've tried:
Moving files to a new rails app, got server to run but encountered many seemingly random errors, fixed a few but more just seem to pop up that I cant figure out:
I also took a look at http://guides.rubyonrails.org/plugins.html but this gem seems to go beyond that.
I would suggest that you clone the gem and begin copying files from the gem into your a new Rails application.
The engine gem probably has a similar structure to a Rails application, so you should be able to move the files from the corresponding folder to the same folder in your Rails root folder.
You may need to move gem files out of modules, change namespaces etc. Relevant folders to look at files you'll want to include might include app/ config/ db/, any gem dependencies in Gemfile or the gemspec file, as well as spec/ or test/.
Beyond that I think there's no silver bullet answer to your question, you're just going to have to work through problems until you have this up and running, and perhaps ask subsequent questions if you hit on an obstacle that you don't get beyond.
I think what you're looking for is a way to hook your Rails Engine into a rails app. The Hooking Into an Application section of the Getting Started with Rails Engines guide should be exactly what you're looking for.
Here are two additional resources on Rails Engines.
A Guide to Rails Engines in the Wild
Rails::Engine - Ruby on Rails API

Allow user to configure Rails application for first use (ActiveRecord etc)

Before I start the process of building something from scratch I was curious if anyone else had come across a way of providing a first-time configuration wizard for a Rails application that allows the user (sysadmin) to configure aspects of the application such as the ActiveRecord configuration and the ActionMailer setup (SMTP, sender etc).
We'd like to provide a clean easy way for new installs to get setup, rather than asking clients to edit files, or run scripts on the command line.
Essentially you should be able to download the application, extract it, start it, and when you access it via the browser you're guided through the initial setup steps.
My question isn't so much around how to do this, but more has anyone already done this. My quick search for gems, plugins etc around this idea didn't turn up much.
Edit/Clarification
To clarify the scenario - this is to support "shrink-wrapped" products that are downloaded and installed on-premises by the customer's sys admin.
The first time they access our application after deploying it behind their firewall we want a friendly way of configuring the specific settings for their install, such as database etc.
An example of such a process is JIRA's setup wizard:
https://confluence.atlassian.com/display/JIRA/Running+the+Setup+Wizard
This isn't for reating new rails applications, or systemising our development process.
I like Rails Templater a lot. It is a command line tool to build a rails app. It asks you questions like "Would you like to use rspec? Pry instead of IRB?"
I have created a fork that adds authentication, twitter bootstrap and backbone.js and some other options. It is pretty easy to hack on if you have specific needs. I use it all the time and I would hate not having it.
It is of course a command line app and not usable via the browser but maybe it will still fit your needs. Or the codebase could be integrated into a client web application.
Update after comment
You may be able to bootstrap a sqlite database so the app can boot, then just using a form or a wizard to set up something that writes your database.yml and other configs, maybe by means of thor (makes appending/replacing text in files simple and is part of rails). You would need to somehow restart the rails app and migrate the database. You could keep the pg or mysql2 gem (or both) in your Gemfile or again use thor to edit them from your wizard/form.
I also recommend using rails_config as Michael suggested. With the above solution.
If I had more time to think about the problem, I may come up with something cleaner but this is how I would do it if I had to right now.
http://railswizard.org/ lets you select which gems to use. It's a very innovative use of the ui and the isotope gem to select the components. Similarly another viewpoint is this top 10 list: http://blog.teamtreehouse.com/10-must-have-ruby-gems
You'll never find a definitive list of gems as need change from person to person and over time, e.g. which database your company uses and which is the 'current' best authentication gem are both variable.
For configuration specific settings you can make the configuration less of a chore by using practices that reduce it. For example the database.yml file is usually one of the 'must be edited locally' files. Our approach for this to to exclude database.yml from source control by putting its name in our .gitignore file. We add an database.yml.example file which we copy locally to database.yml and in that file we use anchors (&) and references (*) as detailed here: http://blog.geekdaily.org/2010/08/advanced-yaml-tricking-out-your-databaseyml.html
More options for configuring other variables for each environment: http://kpumuk.info/ruby-on-rails/flexible-application-configuration-in-ruby-on-rails/
The rails config gem may also help you as detailed here: How to define custom configuration variables in rails

Reference checklist for starting a new Rails application?

It's easy to create a new Rails application using "rails new myapp" (or start with a template from Rails Wizard or the RailsApps project).
But after that, what do you do? What gems do you always add? What decisions do you make before beginning development? What important items are often overlooked before deploying an app?
I'd like to put together a reference checklist to share. Any suggestions?
For example:
Create a new RVM gemset
Modify the .gitignore file
Switch from TestUnit to RSpec
Set up Guard to run tests in the background during development
Add the viewport metatag to the default application layout
Don't forget to create a sitemap.xml file
Add a Google Analytics snippet
What else?
Starting with a Rails template.
You should look theses resources :
http://railswizard.org/
https://github.com/RailsApps/rails3-application-templates
http://railsapps.github.com/rails-application-templates.html
https://github.com/quickleft/prologue
For me usual process involves:
Add CSS framework (grids, text, forms)
Add Cells
Add Slim (www.slim-lang.com)
Remove Test::Unit for RSpec
Add application config settings (config.yml)
Add Cucumber
Add FactoryGirl
Add Spork
Add Guard (guard-rspec, guard-cucumber, guard-sass, guard-livereload, guard-spork)
Add Git, Github space, + amend .gitignore
Add Heroku (stage + production) spaces
I'll usually copy over my google_analytics helpers and sitemap_controller from other projects during the development process instead of being organised enough to do it from the start. I like to the the testing and deployment options setup from the get go so I can start developing and releasing early and often.
Dave
create rvm gemset, create .rvmrc, modify .gitignore
Then add gems
gem 'pg'
gem 'thin'
gem 'ruby-debug19', :require => 'ruby-debug'
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'capybara'
then depending on the project, I often use aws3, paperclip, resque, will_paginate and haml (although I try not to use it on new projects anymore)
Most of the time:
Configuration
add .rvmrc
amberbit-config gem (avaible at GH)
modify .gitignore
Views
haml to sass/coffee stack
rdiscount
Tests
rspec instead of unit tests
capybara, factory_gril, spork, timecop
Development
guard-livereload, with guard, libnotify etc.
active_reload for faster development with assets pipeline
annotate if relational db
pry
I almost forgot to mention: mix of html5 boilerplate for rails with twitter bootstrap it's good combo.
The first think that I do is head to http://railswizard.org/ and create a template, before "rails new app".
I always want to set up Factory Girl under /fixtures, and setup
Cucumber along Rspec. Sometimes I use shoulda too.
Initialize the project as a git repository and link it to
github. Set up the app to use PostgreSQL instead of SQLite.
And last I can think of is that I often make an entry, from the
beginning, to load .rb files form /lib automatically.
I don't add anything. Things get added if project requires them.
I don't load up CSS framework just because there's a need for two columns and a rounded button somewhere.
I don't load FactoryGirl because rails test fixtures actually do a fine job as long as you know how to use them. (Hint: you don't need 100 instances of User in your fixtures)
I don't load RSpec/Cucumber/etc because UnitTest is just as good and I prefer keeping things simple.
There's absolutely no reason to bloat project with things just because you "might need it"
I got tired of having to remember and repeat the mundane tasks required for every new app.
If you're looking for guidance on getting started, we've recently (yesterday!) released a tool to speed up the process, with all sorts of tried and tested libraries for the various aspects of a Rails web app, as well as cleanup scripts to get the fresh app just the way it should be.
Check out http://drone.bz to build an app the same way we do. Under the hood, it uses the app_drone gem to resolve dependencies and build the actual template, but the UI is probably all you need to get started.
There are several similar tools out there, but I decided to be highly opinionated in my recipe choices, and the end result is a solid foundation on which to start developing :)
P.S. Even if you don't use the drones, you can take a look at the steps that are common enough in Rails dev to be automated.

Building a ruby gem for Rails applications

As a Rails developer I feel a bit stupid asking this question but hopefully I will learn something new and someone can put me out of my misery! In my rails applications I use (other peoples) gems all the time, I also use plugins from the community or my own.
I understand the benefits of use gems over plugins as they are version-able, segmented, system wide, easier to manage and share etc etc but I don't really know how to go about making a gem for my rails apps!?
Do you always start with a plugin and convert it to a gem, I've seen the words 'package it as Gem'. Also the gem I'm thinking of building would be no good in a normal ruby program, it's only useful to rails apps. I'm not even sure if the semantics of that make sense, 'RubyGem' that will only work in a rails application!?
I would like to create a gem (if that's what I should use?) for a discrete piece of functionality for my rails apps. It will need to add a database migration, new routes and provide controllers and views or useful view helpers. I'm know I can achieve this via a plug-in but would just like to know how/why to do it as a 'Ruby Gem'?
To avoid the risk of Over-engineering, I usually start with the feature I need directly into the application. Then, as soon as I need to use the same feature into another project, I check whether it is worth to extract it into a plugin or even a separate application providing an API.
Plugins and Gems are often interchangeable. Gems provides several significant advantages in terms of reusability and maintainability.
On the other side, there are some specific known issue. For instance, a Rails app actually can't load rake tasks defined into a plugin packaged as a Gem.
Almost every Rails plugin can be packaged as a Gem.
For instance, take my tabs_on_rails plugin.
You can install it as a Gem specifying the dependency on environment.rb. Or you can use script/plugin install command as you would expect.
If you want to achieve the same result, make sure to follow the standard Gem layout and provide the init.rb initialization script required by Rails.
Also, you might want to create an install.rb and uninstall.rb file to include the post-install and post-uninstall hooks when the plugin is installed as a standard Rails plugin.
Last but not least, if you package a plugin as Gem you can reuse it in non-Rails projects and provide Rails-specific initializations using the init.rb file. Non-Rails applications will simply ignore it.
If you want to make a plugin for Rails, https://peepcode.com/products/rails-2-plugin-patterns gives you a good start. After that, make the plugin into a gem.
To make a gem, this resource http://railscasts.com/episodes/183-gemcutter-jeweler will be helpful.
As of 2013 you'll want to use Bundler and the following tutorials:
#245 New Gem with Bundler -
RailsCasts
Make your own gem - RubyGems
Guides
Take a look at Jeweler. Jeweler gives you a set of rake tasks to make gem versioning and building very easy.

Resources