ruby-on-rails-4rubygemsrails-generators

building generator in gem for rails 4


I have looked and tried various things in regards to creating a gem with a generator. Maybe my tiredness is causing me to forget something or maybe its just my lack of experience. Either way I am trying to understand how to build a gem that is a simple generator so that I can reuse the code in future projects. Yes I am building something that already exists but as a learner I am more interested in understanding how to build a gem so that I may be able to contribute something more meaningful in the future rather than just using already made gems without knowing what is really going on. So with out further ado my code looks like this:

tree for simpauth
-lib
 -generators
  -simpauth
   -templates
    sessions.rb
   install_generator.rb
 -simpauth
 simpauth.rb

here is my code for generators/simpauth/install_generator.rb

require 'rails/generators'

module Simpauth
  class InstallGenerator < ::Rails::Generators::Base
    source_root File.expand_path('../templates', __FILE__)
    desc "Creating simple and customizable authentication"

    def add_session
        copy_file "sessions.rb", "app/controllers/sessions_controller.rb"
    end
  end
end

my generators/simpauth/templates/sessions.rb

class SessionsController < ApplicationController
  def new
  end

  def create
    user = User.find_by(email: params[:session][:email])
    if user && user.authenticate(params[:session][:password])
        #login user and redirect to user_path
        log_in user
        params[:session][:remember_me] == '1' ? remember(user) : forget(user)
        redirect_to user
    else
        flash.now[:danger] = "invalid email and/or password"
        render 'new'
    end
  end

  def destroy
    log_out if logged_in?
    redirect_to root_url
  end

end

and lib/simpauth.rb

require "simpauth/version"
require 'rails'

module Simpauth
  class Engine < Rails::Engine
  end
end

also simpauth.gemspec

# coding: utf-8
$:.push File.expand_path('../lib', __FILE__)
require 'simpauth/version'

Gem::Specification.new do |spec|
  spec.name          = "simpauth"
  spec.version       = Simpauth::VERSION
  spec.authors       = ["My Name"]
  spec.email         = ["my_email@example.com"]
  spec.summary       = %q{Simplified authentication}
  spec.description   = %q{}
  spec.homepage      = ""
  spec.license       = "MIT"

  spec.files         = `git ls-files -z`.split("\x0")
  spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
  spec.test_files    = spec.files.grep(%r{^(test|spec|features)/})
  spec.require_paths = ["lib"]

  spec.add_development_dependency "bundler", "~> 1.7"
  spec.add_development_dependency "rake", "~> 10.0"
end

Any help would be greatly appreciated.

Edit - this code works as expected inside a rails app. I just can't get rails to recognize the generator when installed as a gem.


Solution

  • I was able to figure out that my problem was related to my gemspec file. Specifically with the spec.file assignment. I changed:

    spec.file = `git ls-files -z`.split("\x0")
    

    to

    spec.file = Dir["{lib,vendor}/**/*"]
    

    which resolved my issues.