module PaidUp class InstallGenerator < Rails::Generators::Base argument :user_model_name, :type => :string, :default => "User" source_root File.expand_path("../templates", __FILE__) require File.expand_path('../../utils', __FILE__) include Generators::Utils include Rails::Generators::Migration def hello output "PaidUp Installer will now install itself", :magenta end # all public methods in here will be run in order def install_devise output "To start with, Devise is used to authenticate users. No need to install it separately, I'll do that now.", :magenta gsub_file "config/routes.rb", /devise_for :users/, '' generate("devise:install") generate("devise User") end def install_cancan output "For authorization, PaidUp uses CanCan. Let's get you started with a customizable ability.rb file.", :magenta template "ability.rb", "app/models/ability.rb" end def install_rolify output "To provide varying roles for Users, we'll use Rolify. Let's set that up now.", :magenta generate("rolify", "Role User") end def add_initializer output "Next, you'll need an initializer. This is where you put your configuration options.", :magenta template "initializer.rb", "config/initializers/paid_up.rb" end def add_migrations output "Next come migrations.", :magenta rake 'paid_up:install:migrations' end def add_to_model output "Adding PaidUp to your User model", :magenta gsub_file "app/models/user.rb", /^\n subscriber$/, '' inject_into_file "app/models/user.rb", "\n subscriber", after: "class User < ActiveRecord::Base" end def add_route output "Adding PaidUp to your routes.rb file", :magenta gsub_file "config/routes.rb", /mount PaidUp::Engine => '\/.*', :as => 'paid_up'/, '' route("mount PaidUp::Engine => '/', :as => 'paid_up'") end def self.next_migration_number(dirname) if ActiveRecord::Base.timestamped_migrations unless @prev_migration_nr @prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i else @prev_migration_nr += 1 end @prev_migration_nr.to_s else "%.3d" % (current_migration_number(dirname) + 1) end end end end