cream 0.8.5 → 0.8.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,74 @@
1
+ module DeviseConfigGenerator
2
+ module AppHelper
3
+ def protection_configure!
4
+ logger.debug "Configuring: devise authentication filter"
5
+ ## Add Devise protection to Application controller:
6
+ insert_into_controller :application do
7
+ "before_filter :authenticate_user!"
8
+ end
9
+ end
10
+
11
+ # inside 'config/initializers/devise.rb' change to:
12
+ # require 'devise/orm/mongo_mapper'
13
+ def orm_configure!
14
+ return if orm == :active_record
15
+ logger.debug "Configuring orm: [#{orm}]"
16
+
17
+ if !devise_initializer?
18
+ say "WARNING: initializer/devise.rb not found", :yellow
19
+ return
20
+ end
21
+
22
+ if !has_statement?(orm_replacement)
23
+ logger.debug "require 'devise/orm/#{orm}' already in devise.rb initializer"
24
+ return
25
+ end
26
+
27
+ if !has_devise_orm_statement?
28
+ say "WARNING: devise/orm statement not found in devise.rb initializer", :yellow
29
+ return
30
+ end
31
+
32
+ File.replace_content_from devise_initializer, :where => orm_statement, :with => orm_replacement
33
+ end
34
+
35
+ def mailer_configure!
36
+ logger.debug "Configuring: devise mailer"
37
+ insert_application_config "action_mailer.default_url_options = { :host => 'localhost:3000' }"
38
+ end
39
+
40
+ private
41
+
42
+ def devise_initializer
43
+ initializer_file(:devise)
44
+ end
45
+
46
+ def devise_initializer?
47
+ initializer_file?(:devise)
48
+ end
49
+
50
+ def devise_initializer_content
51
+ File.new(devise_initializer).read
52
+ end
53
+
54
+ def has_devise_orm_statement?
55
+ devise_initializer_content =~ orm_statement
56
+ end
57
+
58
+ def has_statement? statement
59
+ devise_initializer_content =~ /#{Regexp.escape(statement)}/
60
+ end
61
+
62
+ def has_devise_orm_replacement?
63
+ has_statement? orm_replacement
64
+ end
65
+
66
+ def orm_statement
67
+ /devise\/orm\/\w+/
68
+ end
69
+
70
+ def orm_replacement
71
+ "devise/orm/#{orm}"
72
+ end
73
+ end
74
+ end
@@ -3,28 +3,31 @@ require 'sugar-high/module'
3
3
  require 'cream'
4
4
  require 'rails3_artifactor'
5
5
  require 'logging_assist'
6
+ require 'generators/cream/helpers/all'
7
+
8
+ # include helpers
9
+ require_all File.dirname(__FILE__)
6
10
 
7
11
  module Devise
8
12
  module Generators
9
13
  class ConfigGenerator < Rails::Generators::Base
10
14
  desc "Configure Devise"
11
15
 
16
+ argument :user_class, :type => :string, :default => 'User', :desc => "User class"
17
+
12
18
  # ORM to use
13
19
  class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
14
20
  class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
15
21
  class_option :gems, :type => :boolean, :default => true, :desc => "Add gems to gemfile?"
16
- class_option :admin_user, :type => :boolean, :default => false, :desc => "Setup for admin user"
22
+ class_option :admin_user, :type => :boolean, :default => false, :desc => "Setup for admin user"
17
23
 
18
24
  def configure_devise
19
25
  logger.add_logfile :logfile => logfile if logfile
20
- if gems?
21
- devise_gems
22
- else
23
- say "WARNING: Not configuring devise gems for #{orm}", :yellow
24
- end
26
+ devise_gems if gems?
27
+ say "WARNING: Not configuring devise gems for #{orm}", :yellow if !gems?
25
28
 
26
- devise_install
27
- [:orm, :mailer, :protection, :routes].each{|m| send(:"#{m}_configure!", orm) }
29
+ devise_install
30
+ configure_devise!
28
31
  end
29
32
 
30
33
  protected
@@ -32,29 +35,14 @@ module Devise
32
35
  include Rails3::Assist::BasicLogger
33
36
  extend Rails3::Assist::UseMacro
34
37
 
35
- use_helpers :controller, :app, :special, :file
36
-
37
- # rails generate ...
38
- def rgen command
39
- execute "rails g #{command}"
40
- end
41
-
42
- def execute command
43
- logger.debug command
44
- run command
45
- end
46
-
47
- def devise_initializer
48
- initializer_file(:devise)
49
- end
38
+ include Cream::GeneratorHelper::Orm
39
+ include Cream::GeneratorHelper::Executor
40
+ include Cream::GeneratorHelper::Args
50
41
 
51
- def devise_initializer?
52
- initializer_file?(:devise)
53
- end
42
+ use_helpers :controller, :app, :special, :file
54
43
 
55
- def devise_initializer_content
56
- File.new(devise_initializer).read
57
- end
44
+ include DeviseConfigGenerator::AppHelper
45
+ include DeviseConfigGenerator::GemHelper
58
46
 
59
47
  def devise_install
60
48
  if devise_initializer?
@@ -65,171 +53,22 @@ module Devise
65
53
  rgen 'devise:install'
66
54
  end
67
55
 
68
- def bundle_install
69
- run "bundle install"
70
- end
71
-
72
- def gems_mongo_db
73
- add_gem 'bson_ext', '>= 1.1.4'
74
- end
75
-
76
- def is_active_record?
77
- [:ar, :active_record].include? orm.to_sym
78
- end
79
-
80
- def is_mongo_mapper?
81
- [:mm, :mongo_mapper].include? orm.to_sym
82
- end
83
-
84
- def is_data_mapper?
85
- [:dm, :data_mapper].include? orm.to_sym
56
+ # see app_helper.rb
57
+ def configure_devise!
58
+ [:orm, :mailer, :protection].each{|m| send(:"#{m}_configure!") }
86
59
  end
87
60
 
88
61
  def devise_gems
89
- say "Configuring devise gems for #{orm}", :green
62
+ say "Configuring devise gems for orm: #{orm}", :green
90
63
  add_gem 'devise'
91
64
 
92
- # Devise ORM integration
93
- case orm.to_sym
94
- when is_active_record?
95
- say "Configuring for Active Record"
96
- when is_mongo_mapper?
97
- say "Configuring for Mongo Mapper"
98
- add_gem 'mm-devise'
99
- gems_mongo_db
100
- when is_data_mapper?
101
- say "Configuring for Data Mapper"
102
- add_gem 'dm-devise'
103
- when :mongoid
104
- say "Configuring for Mongoid"
105
- say "Please configure Devise for Mongoid similar to Rails 3 example app: http://github.com/fortuity/rails3-mongoid-devise"
106
- add_gem 'mongoid', '>= 2.0.0.beta.20'
107
- gems_mongo_db
108
- when :couch_db
109
- say "Configuring for Couch DB"
110
- add_gem 'devise_couch'
111
- say "Please note that Couch DB does not currently have a complete Roles implementation. Please help implement strategies for this adapter."
112
- else
113
- say "Orm #{orm} is not currently supported by Cream. You are most welcome to provide a Cream adapter for that ORM ;)"
114
- end
65
+ send :"configure_gems_#{orm}" # see gem_helper.rb
66
+
115
67
  clean_gemfile
116
68
 
117
69
  bundle_install
118
- if orm.to_sym == :mongoid
119
- rgen 'mongoid:config'
120
- rgen "devise mongoid"
121
- end
122
- end
123
-
124
- def routes_configure! orm=nil
125
- insert_into_routes do
126
- "devise_for #{model_routes}"
127
- end
128
- end
129
-
130
- def admin_user?
131
- options[:admin_user]
132
- end
133
-
134
- def model_routes
135
- return ':users, :admins' if admin_user?
136
- ':users'
137
- end
138
-
139
-
140
- def protection_configure! orm=nil
141
- logger.debug "Configuring: devise authentication filter"
142
- ## Add Devise protection to Application controller:
143
- insert_into_controller :application do
144
- "before_filter :authenticate_user!"
145
- end
146
- end
147
-
148
- # inside 'config/initializers/devise.rb' change to:
149
- # require 'devise/orm/mongo_mapper'
150
- def orm_configure! orm
151
- return if orm == 'active_record'
152
- logger.debug "Configuring orm: [#{orm}]"
153
-
154
- if !devise_initializer?
155
- say "WARNING: initializer/devise.rb not found", :yellow
156
- return
157
- end
158
-
159
- if !has_statement?(orm_replacement)
160
- logger.debug "require 'devise/orm/#{orm}' already in devise.rb initializer"
161
- return
162
- end
163
-
164
- if !has_devise_orm_statement?
165
- say "WARNING: devise/orm statement not found in devise.rb initializer", :yellow
166
- return
167
- end
168
-
169
- File.replace_content_from devise_initializer, :where => orm_statement, :with => orm_replacement
170
- end
171
-
172
- def mailer_configure! orm=nil
173
- logger.debug "Configuring: devise mailer"
174
- insert_application_config "action_mailer.default_url_options = { :host => 'localhost:3000' }"
175
- end
176
-
177
- private
178
-
179
- def add_gem_version name, version
180
- if !has_gem_version?(name, version)
181
- logger.debug "Adding gem: #{name}, #{version}"
182
- gem name, :version => version
183
- else
184
- logger.debug "gem: #{name}, #{version} already in Gemfile"
185
- end
186
- end
187
-
188
- def add_gem name, version = nil
189
- if version
190
- add_gem_version name, version
191
- return
192
- end
193
-
194
- if !has_gem? name
195
- logger.debug "Adding gem: #{name}"
196
- gem name
197
- else
198
- logger.debug "gem: #{name} already in Gemfile"
199
- end
200
- end
201
-
202
- def gems?
203
- options[:gems]
204
- end
205
-
206
- def logfile
207
- options[:logfile]
208
- end
209
-
210
- def orm
211
- options[:orm]
212
- end
213
-
214
- def has_devise_orm_statement?
215
- devise_initializer_content =~ orm_statement
216
- end
217
-
218
- def has_statement? statement
219
- devise_initializer_content =~ /#{Regexp.escape(statement)}/
220
- end
221
-
222
- def has_devise_orm_replacement?
223
- has_statement? orm_replacement
224
- end
225
-
226
- def orm_statement
227
- /devise\/orm\/\w+/
228
- end
229
-
230
- def orm_replacement
231
- "devise/orm/#{orm}"
232
- end
70
+ devise_mongoid_setup if mongoid?
71
+ end
233
72
  end
234
73
  end
235
74
  end
@@ -0,0 +1,63 @@
1
+ module DeviseConfigGenerator
2
+ module GemHelper
3
+ def gems_mongo_db
4
+ add_gem 'bson_ext', '>= 1.1.4'
5
+ end
6
+
7
+ def devise_mongoid_setup
8
+ rgen 'mongoid:config'
9
+ rgen "devise mongoid"
10
+ end
11
+
12
+ def configure_gems_active_record
13
+ say "Configuring for Active Record"
14
+ end
15
+
16
+ def configure_gems_data_mapper
17
+ say "Configuring for Data Mapper"
18
+ add_gem 'dm-devise'
19
+ end
20
+
21
+ def configure_gems_mongo_mapper
22
+ say "Configuring for Mongo Mapper"
23
+ add_gem 'mm-devise'
24
+ gems_mongo_db
25
+ end
26
+
27
+ def configure_gems_mongoid
28
+ say "Configuring for Mongoid"
29
+ # say "Please configure Devise for Mongoid similar to Rails 3 example app: http://github.com/fortuity/rails3-mongoid-devise"
30
+ add_gem 'mongoid', '>= 2.0.0.beta.20'
31
+ gems_mongo_db
32
+ end
33
+
34
+ def configure_gems_couch_db
35
+ say "Configuring for Couch DB"
36
+ add_gem 'devise_couch'
37
+ say "WARNING: Couch DB does not currently have a complete Roles implementation (admin_flag only). Please help implement the Roles strategy adapter.", :yellow
38
+ end
39
+
40
+ def add_gem_version name, version
41
+ if !has_gem_version?(name, version)
42
+ logger.debug "Adding gem: #{name}, #{version}"
43
+ gem name, :version => version
44
+ else
45
+ logger.debug "gem: #{name}, #{version} already in Gemfile"
46
+ end
47
+ end
48
+
49
+ def add_gem name, version = nil
50
+ if version
51
+ add_gem_version name, version
52
+ return
53
+ end
54
+
55
+ if !has_gem? name
56
+ logger.debug "Adding gem: #{name}"
57
+ gem name
58
+ else
59
+ logger.debug "gem: #{name} already in Gemfile"
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,64 @@
1
+ module DeviseUserGenerator
2
+ module Helper
3
+ def devise_strategies
4
+ {
5
+ :default => [:database_authenticatable, :confirmable, :recoverable, :rememberable, :trackable, :validatable],
6
+ :admin => [:database_authenticatable, :confirmable, :recoverable, :rememberable, :trackable, :validatable]
7
+ }
8
+ end
9
+
10
+ def inherit_model hash
11
+ File.replace_content_from model_file_name(subclass hash), :where => /class #{admin_class_name}/, :with => "class #{admin_class_name} < #{superclass hash}"
12
+ end
13
+
14
+ def superclass hash
15
+ hash.values.first
16
+ end
17
+
18
+ def subclass hash
19
+ hash.keys.first.to_s.camelize
20
+ end
21
+
22
+ def remove_inheritance user
23
+ File.remove_content_from model_file_name(user), :where => /<\s*ActiveRecord::Base/
24
+ end
25
+
26
+ def has_admin_model?
27
+ has_model? admin_class
28
+ end
29
+
30
+ def has_user_model?
31
+ has_model? user_class
32
+ end
33
+
34
+ def user_generator
35
+ active_record? ? 'devise' : "#{orm}:devise"
36
+ end
37
+
38
+ def has_devise_user? user
39
+ return true if is_admin?(user) && !admin_class
40
+ begin
41
+ read_model(user) =~ /devise/
42
+ rescue Exception => e
43
+ logger.info "Exception for #has_devise_user? #{user}: #{e.message}"
44
+ false
45
+ end
46
+ end
47
+
48
+ def devise_users?
49
+ has_devise_user?(user_class) && has_devise_user?(admin_class)
50
+ end
51
+
52
+ def is_admin? name
53
+ name.to_s.underscore == admin_class
54
+ end
55
+
56
+ def admin_class_name
57
+ admin_class.to_s.classify
58
+ end
59
+
60
+ def create_user_model name
61
+ rgen "#{user_generator} #{name}"
62
+ end
63
+ end
64
+ end