lib/generators/devise/config/config_generator.rb in cream-0.7.6 vs lib/generators/devise/config/config_generator.rb in cream-0.7.7
- old
+ new
@@ -10,15 +10,20 @@
desc "Configure Devise"
# ORM to use
class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
- class_option :gems, :type => :boolean, :default => false, :desc => "Add gems to gemfile?"
+ class_option :gems, :type => :boolean, :default => true, :desc => "Add gems to gemfile?"
def configure_devise
logger.add_logfile :logfile => logfile if logfile
- devise_gems if gems?
+ if gems?
+ devise_gems
+ else
+ say "WARNING: Not configuring devise gems for #{orm}", :yellow
+ end
+
devise_install
[:orm, :mailer, :protection].each{|m| send(:"#{m}_configure!", orm) }
end
protected
@@ -26,89 +31,170 @@
include Rails3::Assist::BasicLogger
extend Rails3::Assist::UseMacro
use_helpers :controller, :app, :special, :file
- def gems?
- options[:gems]
- end
-
- def logfile
- options[:logfile]
- end
-
- def orm
- options[:orm]
- end
-
# rails generate ...
def rgen command
execute "rails g #{command}"
end
def execute command
logger.debug command
run command
end
+
+ def devise_initializer
+ initializer_file(:devise)
+ end
+
+ def devise_initializer?
+ initializer_file?(:devise)
+ end
+
+ def devise_initializer_content
+ File.new(devise_initializer).read
+ end
- def devise_install
- logger.debug "initializer_file? #{initializer_file?(:devise)}"
- return if initializer_file?(:devise)
+ def devise_install
+ if devise_initializer?
+ logger.debug "initializers/devise.rb was found so devise:install will not be run"
+ return
+ end
+ logger.debug "initializers/devise.rb was NOT found so devise:install will now be run"
rgen 'devise:install'
end
- def bundle_install *gems
- run "bundle install #{gems.join(' ')}"
+ def bundle_install #*gems
+ # run "bundle install #{gems.join(' ')}"
+ run "bundle install"
end
def devise_gems
- logger.debug 'devise_gems'
- gem 'devise'
+ say "Configuring devise gems for #{orm}", :green
+ add_gem 'devise'
orm_gem = nil
# Devise ORM integration
case orm.to_sym
when :mongoid
- say "Please configure Devise for Mongoid as similar to Rails 3 example app: http://github.com/fortuity/rails3-mongoid-devise"
+ say "Please configure Devise for Mongoid similar to Rails 3 example app: http://github.com/fortuity/rails3-mongoid-devise"
+ add_gem 'mongoid', '2.0.0.beta.19'
+ add_gem 'bson_ext', '1.1.4'
+ # copy_mongoid_config
when :mongo_mapper
orm_gem = 'mm-devise'
- gem 'mm-devise'
+ add_gem 'mm-devise'
when :data_mapper
orm_gem = 'dm-devise'
- gem 'dm-devise'
+ add_gem 'dm-devise'
when :couch_db
orm_gem = 'devise_couch'
- gem 'devise_couch'
+ add_gem 'devise_couch'
say "Please note that Couch DB does not currently have a Roles implementation. Feel free to provide one."
say "Look at Roles DataMapper (roles_data_mapper) for an example ;)"
else
say "Orm #{orm} is not currently supported by Cream. You are most welcome to provide a Cream adapter for that ORM ;)"
end
- bundle_install 'devise', orm_gem
+ bundle_install #'devise', orm_gem
+ if orm.to_sym == :mongoid
+ rgen 'mongoid:config'
+ rgen "devise mongoid"
+ end
end
def protection_configure! orm
- logger.debug "config protection"
+ logger.debug "Configuring: devise authentication filter"
## Add Devise protection to Application controller:
insert_into_controller :application do
"before_filter :authenticate_user!"
end
end
# inside 'config/initializers/devise.rb' change to:
# require 'devise/orm/mongo_mapper'
def orm_configure! orm
- return if orm == :active_record
- logger.debug "config orm: #{orm}"
- found = File.read(initializer_file(:devise)) =~/devise\/orm\/w+/
- logger.debug "found?: #{found}"
+ return if orm == 'active_record'
+ logger.debug "Configuring orm: [#{orm}]"
- File.replace_content_from initializer_file(:devise), :where => /devise\/orm\/\w+/, :with => "devise/orm/#{orm}"
+ if !devise_initializer?
+ say "WARNING: initializer/devise.rb not found", :yellow
+ return
+ end
+
+ if !has_statement?(orm_replacement)
+ logger.debug "require 'devise/orm/#{orm}' already in devise.rb initializer"
+ return
+ end
+
+ if !has_devise_orm_statement?
+ say "WARNING: devise/orm statement not found in devise.rb initializer", :yellow
+ return
+ end
+
+ File.replace_content_from devise_initializer, :where => orm_statement, :with => orm_replacement
end
def mailer_configure! orm
- logger.debug "config mailer"
+ logger.debug "Configuring: devise mailer"
insert_application_config "action_mailer.default_url_options = { :host => 'localhost:3000' }"
end
+
+ private
+
+ def add_gem_version name, version
+ if !has_gem_version?(name, version)
+ logger.debug "Adding gem: #{name}, #{version}"
+ gem(name, version)
+ else
+ logger.debug "gem: #{name}, #{version} already in Gemfile"
+ end
+ end
+
+ def add_gem name, version = nil
+ add_gem_version(name, version) and return if version
+ if !has_gem? name
+ logger.debug "Adding gem: #{name}"
+ gem name
+ else
+ logger.debug "gem: #{name} already in Gemfile"
+ end
+ end
+
+ # def copy_mongoid_config
+ # file File.dirname(__FILE__) + '/mongoid.yml', 'config/mongoid.yml'
+ # end
+
+ def gems?
+ options[:gems]
+ end
+
+ def logfile
+ options[:logfile]
+ end
+
+ def orm
+ options[:orm]
+ end
+
+ def has_devise_orm_statement?
+ devise_initializer_content =~ orm_statement
+ end
+
+ def has_statement? statement
+ devise_initializer_content =~ /#{Regexp.escape(statement)}/
+ end
+
+ def has_devise_orm_replacement?
+ has_statement? orm_replacement
+ end
+
+ def orm_statement
+ /devise\/orm\/\w+/
+ end
+
+ def orm_replacement
+ "devise/orm/#{orm}"
+ end
end
end
end
\ No newline at end of file