Skip to content

Gem Extraction Cheat Sheet

fiedl edited this page Jul 22, 2012 · 12 revisions

Setup the Gem

  1. bundle gem gem_name
  2. edit gem_name.gemspec
  # ...
  gem.add_dependency "rails", ">= 3.2"
  gem.add_dependency "jquery-rails"

  gem.add_development_dependency 'sass-rails'
  gem.add_development_dependency 'coffee-rails'

  gem.add_development_dependency "rake"
  gem.add_development_dependency "bundler"
  gem.add_development_dependency "rspec-rails", ">= 2.8.0"
  gem.add_development_dependency "guard", "1.0.1"
  gem.add_development_dependency "nokogiri", ">= 1.5.0"
  gem.add_development_dependency "capybara"
  gem.add_development_dependency 'rspec-rails', '2.10.0'
  gem.add_development_dependency 'guard-rspec', '0.5.5'

  gem.add_development_dependency 'execjs'
  gem.add_development_dependency 'therubyracer'
  1. bundle install
  2. edit Rakefile:
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new( :spec )
task default: :spec
  1. edit lib/gem_name/version.rb (add .alpha)
  2. create spec/spec_helper.rb
require 'gem_name'
  1. edit README.md

Transfer code

Model Additions

  • module GemName::ModelAdditions
    # lib/gem_name/model_additions.rb
    module GemName
      module ModelAdditions
        def acts_as_something
        end
      end
    end
    ActiveRecord::Base.extend GemName::ModelAdditions
  • require that file in lib/gem_name.rb
  • instead of direct ActiveRecord::Base extension, may use railtie:
# lib/gem_name/railtie.rb
module GemName
  class Railtie < Rails::Railtie
    initializer 'gem_name.model_additions' do
      ActiveSupport.on_load :active_record do
        extend ModelAdditions
      end 
    end
  end 
end
  • add to lib/gem_name.rb:
require "spec_name/railtie" if defined? Rails

Adding a Test App

  • add the test app to a test_app folder:
~/rails/gem_name$ rails new test_app
  • require the test_app's environment in the specs, e.g. in the spec/spec_helper.rb
require File.expand_path('../../test_app/config/environment', __FILE__)
  • configure javascript engine in spec/spec_helper.rb
RSpec.configure do |config|
  # ...
  Capybara.javascript_driver = :webkit
end

Publish the Test App at Heroku

  • Add pg to the Gemfile:
gem 'sqlite3', group: :development
gem 'pg', group: :production
  • Upload to heroku
$ cd test_app
$ git init
$ git add .
$ git commit -m "initial commit"
$ heroku create --stack cedar
$ heroku rename gem-name-test-app
$ git push heroku master
$ heroku run rake db:migrate  # if database needed

Test the Gem Locally

  • add it to the Gemfile of some test app:
gem 'gem_name', path: 'path/to/gem'

Push the Gem to Github

  • add files to local repository if not done, yet.
$ cd gem_name
$ git add .
$ git commit -m "initial commit."
$ git remote add origin [email protected]:your_git_user_name/gem_name.git
$ git push -u origin master
  • in the Gemfile:
gem 'gem_name', git: 'git://github.com/your_git_user_name/gem_name.git'

Publish Gem

  • Version number in lib/gem_name/version.rb
  • Changelog
  • Commit changes: e.g. with message: release v0.0.1
  • Push it to http://rubygems.org:
$ rake release
  • now remove git option from Gemfile
  • bump version number in lib/gem_name/version.rb

Continuous Integration With Travis-CI

  • sign in on http://travis-ci.org
  • go to 'Profile' and turn on the switch of the corresponding github repository
  • get image-url from the status page of the current build

Modify Travis for Use With JavaScript and Database

# .travis.yml
env: "RAILS_ENV=test DISPLAY=:99.0"
before_script:
  - "sh -c 'cd test_app && bundle && bundle exec rake db:drop db:migrate'"
  - "sh -e /etc/init.d/xvfb start"

Trouble Shooting

  • add rake as an development dependency
  • only test in ruby 1.9: Add file .travis.yml:
rvm:
  - 1.9.3

Code Documentation

Literature

  • See, for example, Railscasts #301