Skip to content

Commit

Permalink
Add generator option '--skip-devise' to skip devise installation
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed Jun 24, 2013
1 parent 3ee1975 commit d0af338
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 32 deletions.
71 changes: 40 additions & 31 deletions lib/generators/rails_admin/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class InstallGenerator < Rails::Generators::Base
include Generators::Utils::InstanceMethods
extend Generators::Utils::ClassMethods

class_option :skip_devise, :type => :boolean, :aliases => '-D',
:desc => "Skip installation and setup of devise gem."
argument :_model_name, :type => :string, :required => false, :desc => "Devise user model name"
argument :_namespace, :type => :string, :required => false, :desc => "RailsAdmin url namespace"
desc "RailsAdmin installation generator"
Expand All @@ -22,46 +24,53 @@ def install

display "Hello, RailsAdmin installer will help you set things up!", :blue
display "I need to work with Devise, let's look at a few things first:"
display "Checking for a current installation of devise..."
unless defined?(Devise)
display "Adding devise gem to your Gemfile:"
append_file "Gemfile", "\n", :force => true
gem 'devise'
Bundler.with_clean_env do
run "bundle install"
end
else
display "Found it!"
end
unless File.exists?(Rails.root.join("config/initializers/devise.rb"))
display "Looks like you don't have devise installed! We'll install it for you:"
generate "devise:install"
if options[:skip_devise]
display "Skipping devise installation..."
else
display "Looks like you've already installed it, good!"
display "Checking for a current installation of devise..."
unless defined?(Devise)
display "Adding devise gem to your Gemfile:"
append_file "Gemfile", "\n", :force => true
gem 'devise'
Bundler.with_clean_env do
run "bundle install"
end
else
display "Found it!"
end
unless File.exists?(Rails.root.join("config/initializers/devise.rb"))
display "Looks like you don't have devise installed! We'll install it for you:"
generate "devise:install"
else
display "Looks like you've already installed it, good!"
end
end

namespace = ask_for("Where do you want to mount rails_admin?", "admin", _namespace)
gsub_file "config/routes.rb", /mount RailsAdmin::Engine => \'\/.+\', :as => \'rails_admin\'/, ''
route("mount RailsAdmin::Engine => '/#{namespace}', :as => 'rails_admin'")

unless routes.index("devise_for")
model_name = ask_for("What would you like the user model to be called?", "user", _model_name)
display "Now setting up devise with user model name '#{model_name}':"
generate "devise", model_name
else
display "And you already set it up, good! We just need to know about your user model name..."
guess = routes.match(/devise_for +:(\w+)/)[1].try(:singularize)
display("We found '#{guess}' (should be one of 'user', 'admin', etc.)")
model_name = ask_for("Correct Devise model name if needed.", guess, _model_name)
unless guess == model_name
unless options[:skip_devise]
unless routes.index("devise_for")
model_name = ask_for("What would you like the user model to be called?", "user", _model_name)
display "Now setting up devise with user model name '#{model_name}':"
generate "devise", model_name
else
display "Ok, Devise looks already set up with user model name '#{model_name}':"
display "And you already set it up, good! We just need to know about your user model name..."
guess = routes.match(/devise_for +:(\w+)/)[1].try(:singularize)
display("We found '#{guess}' (should be one of 'user', 'admin', etc.)")
model_name = ask_for("Correct Devise model name if needed.", guess, _model_name)
unless guess == model_name
display "Now setting up devise with user model name '#{model_name}':"
generate "devise", model_name
else
display "Ok, Devise looks already set up with user model name '#{model_name}':"
end
end
end
display "Now you'll need an initializer..."
@model_name = model_name
@current_user_method = model_name ? "current_#{model_name.to_s.underscore}" : ""
@model_name = model_name || '<your user class>'
unless initializer
template "initializer.erb", "config/initializers/rails_admin.rb"
else
Expand All @@ -70,11 +79,11 @@ def install
config_tag = initializer.match(/RailsAdmin\.config.+\|(.+)\|/)[1] rescue nil
if config_tag
if initializer.index(::Regexp.new("#{config_tag}\.current_user_method.?\{.+?\}"))
display "current_user_method found and updated with 'current_#{model_name}'", :green
gsub_file Rails.root.join("config/initializers/rails_admin.rb"), ::Regexp.new("#{config_tag}\.current_user_method.?\{.+?\}"), "#{config_tag}.current_user_method { current_#{model_name} }"
display "current_user_method found and updated with '#{@current_user_method}'", :green
gsub_file Rails.root.join("config/initializers/rails_admin.rb"), ::Regexp.new("#{config_tag}\.current_user_method.?\{.+?\}"), "#{config_tag}.current_user_method { #{@current_user_method} }"
else
display "current_user_method not found. Added one with 'current_#{model_name}'!", :yellow
insert_into_file Rails.root.join("config/initializers/rails_admin.rb"), "\n\n #{config_tag}.current_user_method { current_#{model_name} } #auto-generated", :after => /^RailsAdmin\.config.+$/
display "current_user_method not found. Added one with '#{@current_user_method}'!", :yellow
insert_into_file Rails.root.join("config/initializers/rails_admin.rb"), "\n\n #{config_tag}.current_user_method { #{@current_user_method} } #auto-generated", :after => /^RailsAdmin\.config.+$/
end
else
display "Couldn't parse your config file: current_user_method couldn't be updated", :red
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/rails_admin/templates/initializer.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ RailsAdmin.config do |config|
# config.main_app_name = Proc.new { |controller| [Rails.application.engine_name.titleize, controller.params['action'].titleize] }

# RailsAdmin may need a way to know who the current user is]
config.current_user_method { current_<%= @model_name.underscore %> } # auto-generated
config.current_user_method { <%= @current_user_method %> } # auto-generated

# If you want to track changes on your models:
# config.audit_with :history, '<%= @model_name.classify %>'
Expand Down

0 comments on commit d0af338

Please sign in to comment.