Sha256: 5fb331a6bfae0e6c5a57fcf789f31caf254af9c252035d13a1a8380c7297e736

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

require 'rails/generators/active_record'

class ClearanceGenerator < ActiveRecord::Generators::Base
  desc "Setup the basic stuff needed for Clearance"

  argument :name, :type => :string, :default => "migration_source_name"

  def self.source_root
    @_clearance_source_root ||= File.join(File.dirname(__FILE__), "clearance_templates")
  end

  def install
    template "clearance.rb", "config/initializers/clearance.rb"

    inject_into_class "app/controllers/application_controller.rb", ApplicationController do
      "  include Clearance::Authentication\n"
    end

    user_model = "app/models/user.rb"
    if File.exists?(user_model)
      inject_into_class user_model, User do
        "include Clearance::User"
      end
    else
      template "user.rb", user_model
    end

    if File.exists?("spec")
      template "factories.rb", "spec/factories/clearance.rb"
    else
      template "factories.rb", "test/factories/clearance.rb"
    end

    migration_template "migrations/#{migration_source_name}.rb",
                       "db/migrate/clearance_#{migration_target_name}"

    #readme "README"
  end

  private

  def schema_version_constant
    if upgrading_clearance_again?
      "To#{schema_version.gsub('_', '')}"
    end
  end

  def migration_source_name
    if ActiveRecord::Base.connection.table_exists?(:users)
      'update_users'
    else
      'create_users'
    end
  end

  def migration_target_name
    if upgrading_clearance_again?
      "update_users_to_#{schema_version}"
    else
      'create_users'
    end
  end

  def schema_version
    IO.read(File.join(File.dirname(__FILE__), '..', '..', 'VERSION')).strip.gsub(/[^\d]/, '_')
  end

  def upgrading_clearance_again?
    ActiveRecord::Base.connection.table_exists?(:users)
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
clearance-0.9.0.rc5 lib/rails/generators/clearance_generator.rb
clearance-0.9.0.rc4 lib/rails/generators/clearance_generator.rb
clearance-0.9.0.rc3 lib/rails/generators/clearance_generator.rb