Create a rails gem
Choose a name for your gem :D. In this example, I named my gem is fastfood
Create skeleton:
fastfood |__fastfood.gemspec | |__lib | |__fastfood.rb | |__fastfood | |__engine.rb | |__vendor |__assets |__javascripts | |__ff.js | |__stylesheets |__ff.css // fastfood.gemspec: Gem dependency, require files, gem informations. // fastfood.rb: Main file // engine.rb: A rails module to manage your assets // vendor folder: Your assets
fastfood.gemspec
Gem::Specification.new do |s| s.name = 'fastfood' s.version = '0.0.1' s.date = '2010-10-10' s.summary = 'Summary...' s.description = 'Description...' s.authors = ["Eovy"] s.email = 'conficker1805@gmail.com' s.files = Dir.glob('{vendor,lib}/**/*') # Load files in vendor and lib folder s.homepage = 'http://rubygems.org/gems/fastfood' s.license = 'MIT' s.require_paths = ['lib'] s.add_development_dependency 'rails', '~> 5.1' end
fastfood.rb
require "fastfood/engine" module Fastfood def self.hi puts 'Hello world' end end
engine.rb
module Fastfood class Engine < ::Rails::Engine # Rails will handle your assets automatically! end end
Drop some lines for testing
# ff.js alert('JS is loaded')
# ff.css .disappear { display: none; }
OK! now you can build and install your gem
Please note that you must re-build your gem if anything changed!
After building, it will generate a compiled file.
gem build fastfood.gemspec # Successfully built RubyGem # Name: fastfood # Version: 0.0.1 # File: fastfood-0.0.1.gem
Check your gem with a Rails project
Open a Rails project and add your gem to Gemfile
gem 'fastfood', path: '/Users/conficker/workspace/fastfood/' # Local path
and run
bundle install
Run rails s
and check your first gem :D. Happy coding!