Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add generator option '--skip-devise' to skip devise installation

  • Loading branch information...
commit d0af3380182ed7b991a9a23eae3c114b88fe0a14 1 parent 3ee1975
@mshibuya mshibuya authored
View
71 lib/generators/rails_admin/install_generator.rb
@@ -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"
@@ -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
@@ -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
View
2  lib/generators/rails_admin/templates/initializer.erb
@@ -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 %>'
Please sign in to comment.
Something went wrong with that request. Please try again.