cream 0.6.4 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -64,7 +64,7 @@ module Cream::Generators
64
64
  end
65
65
 
66
66
  def admin_user?
67
- options[:admin]
67
+ options[:admin_user]
68
68
  end
69
69
 
70
70
  def here
@@ -0,0 +1,105 @@
1
+ require 'sugar-high/file'
2
+ require 'sugar-high/module'
3
+ require 'cream'
4
+ require 'rails3_artifactor'
5
+ require 'logging_assist'
6
+
7
+ module Devise
8
+ module Generators
9
+ class ConfigGenerator < Rails::Generators::Base
10
+ desc "Configure Devise"
11
+
12
+ # ORM to use
13
+ class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
14
+ class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
15
+
16
+ def configure_devise
17
+ logger.add_logfile :logfile => logfile if logfile
18
+ devise_gems
19
+ devise_install
20
+ [:orm, :mailer, :protection].each{|m| send(:"#{m}_configure!", orm) }
21
+ end
22
+
23
+ protected
24
+
25
+ include Rails3::Assist::BasicLogger
26
+ extend Rails3::Assist::UseMacro
27
+
28
+ use_helpers :controller, :app, :special, :file
29
+
30
+ def logfile
31
+ options[:logfile]
32
+ end
33
+
34
+ def orm
35
+ options[:orm]
36
+ end
37
+
38
+ # rails generate ...
39
+ def rgen command
40
+ execute "rails g #{command}"
41
+ end
42
+
43
+ def execute command
44
+ logger.debug command
45
+ run command
46
+ end
47
+
48
+ def devise_install
49
+ logger.debug "initializer_file? #{initializer_file?(:devise)}"
50
+ return if initializer_file?(:devise)
51
+ rgen 'devise:install'
52
+ end
53
+
54
+ def bundle_install
55
+ run "bundle install"
56
+ end
57
+
58
+ def devise_gems
59
+ logger.debug 'devise_gems'
60
+ gem 'devise'
61
+
62
+ # Devise ORM integration
63
+ case orm.to_sym
64
+ when :mongoid
65
+ say "Please configure Devise for Mongoid as similar to Rails 3 example app: http://github.com/fortuity/rails3-mongoid-devise"
66
+ when :mongo_mapper
67
+ gem 'mm-devise'
68
+ when :data_mapper
69
+ gem 'dm-devise'
70
+ when :couch_db
71
+ gem 'devise_couch'
72
+ say "Please note that Couch DB does not currently have a Roles implementation. Feel free to provide one."
73
+ say "Look at Roles DataMapper (roles_data_mapper) for an example ;)"
74
+ else
75
+ say "Orm #{orm} is not currently supported by Cream. You are most welcome to provide a Cream adapter for that ORM ;)"
76
+ end
77
+ # bundle_install
78
+ end
79
+
80
+ def protection_configure! orm
81
+ logger.debug "config protection"
82
+ ## Add Devise protection to Application controller:
83
+ insert_into_controller :application do
84
+ "before_filter :authenticate_user!"
85
+ end
86
+ end
87
+
88
+ # inside 'config/initializers/devise.rb' change to:
89
+ # require 'devise/orm/mongo_mapper'
90
+ def orm_configure! orm
91
+ return if orm == :active_record
92
+ logger.debug "config orm: #{orm}"
93
+ found = File.read(initializer_file(:devise)) =~/devise\/orm\/w+/
94
+ logger.debug "found?: #{found}"
95
+
96
+ File.replace_content_from initializer_file(:devise), :where => /devise\/orm\/\w+/, :with => "devise/orm/#{orm}"
97
+ end
98
+
99
+ def mailer_configure! orm
100
+ logger.debug "config mailer"
101
+ insert_application_config "action_mailer.default_url_options = { :host => 'localhost:3000' }"
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,150 @@
1
+ require 'sugar-high/file'
2
+ require 'sugar-high/module'
3
+ require 'cream'
4
+ require 'rails3_artifactor'
5
+ require 'logging_assist'
6
+
7
+ module Devise
8
+ module Generators
9
+ class UsersGenerator < Rails::Generators::Base
10
+ desc "Configure Devise with Users"
11
+
12
+ # Create Admin user
13
+ class_option :admin_user, :type => :boolean, :default => false, :desc => "Create admin user"
14
+ # ORM to use
15
+ class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
16
+ class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
17
+
18
+ def configure_devise_users
19
+ logger.add_logfile :logfile => logfile if logfile
20
+ logger.debug "Configure Devise Users"
21
+
22
+ devise_gems
23
+ devise_default_user if !has_model? :user
24
+
25
+ # # if User model is NOT configured with devise strategy
26
+ insert_devise_strategy :user, :defaults if !has_devise_user? :user
27
+
28
+ devise_admin_user if admin_user?
29
+ end
30
+
31
+ protected
32
+
33
+ extend Rails3::Assist::UseMacro
34
+ include Rails3::Assist::BasicLogger
35
+
36
+ use_helpers :model
37
+
38
+ def logfile
39
+ options[:logfile]
40
+ end
41
+
42
+ def devise_gems
43
+ gem 'devise'
44
+ # bundle_install
45
+ end
46
+
47
+ def bundle_install
48
+ run "bundle install"
49
+ end
50
+
51
+ def devise_default_user
52
+ create_devise_model :user
53
+ end
54
+
55
+ def devise_admin_user
56
+ # if app does NOT have a Admin model
57
+ create_admin_user if !has_model? :admin
58
+ end
59
+
60
+ def create_admin_user
61
+ logger.debug 'create_admin_user'
62
+ create_user_model :admin
63
+ # remove any current inheritance
64
+ remove_inheritance :admin
65
+
66
+ # insert default devise Admin strategy
67
+ insert_devise_strategy :admin, :defaults if has_model? :admin
68
+
69
+ # and make Admin model inherit from User model
70
+ inherit_model :user => :admin
71
+ end
72
+
73
+ # Helpers
74
+
75
+ def admin_user?
76
+ options[:admin_user]
77
+ end
78
+
79
+ def user_generator
80
+ active_record? ? 'devise' : "#{orm}:devise"
81
+ end
82
+
83
+ def active_record?
84
+ orm == 'active_record'
85
+ end
86
+
87
+ # rails generate ...
88
+ def rgen command
89
+ execute "rails g #{command}"
90
+ end
91
+
92
+ def execute command
93
+ logger.debug command
94
+ run command
95
+ end
96
+
97
+ def orm
98
+ options[:orm]
99
+ end
100
+
101
+ def create_user_model user = :user
102
+ rgen "#{user_generator} #{user}"
103
+ end
104
+
105
+ def devise_users?
106
+ has_devise_user?(:user) && has_devise_user?(:admin)
107
+ end
108
+
109
+ def has_devise_user? user
110
+ return true if user == :admin && !admin_user?
111
+ begin
112
+ read_model(user) =~ /devise/
113
+ rescue Exception => e
114
+ logger.info "Exception for #has_devise_user? #{user}: #{e.message}"
115
+ false
116
+ end
117
+ end
118
+
119
+ # Must be ORM specific!
120
+ def create_devise_model user = :user
121
+ rgen "#{user_generator} #{user}"
122
+ end
123
+
124
+ def remove_inheritance user
125
+ File.remove_content_from model_file_name(user), :where => /<\s*ActiveRecord::Base/
126
+ end
127
+
128
+ def inherit_model hash
129
+ superclass = hash.keys.first.to_s.camelize
130
+ subclass = hash.values.first
131
+ logger.debug "subclass: #{subclass}"
132
+ logger.debug "superclass: #{superclass}"
133
+ File.replace_content_from model_file_name(subclass), :where => /class Admin/, :with => "class Admin < #{superclass}"
134
+ end
135
+
136
+ def insert_devise_strategy model_name, *names
137
+ names = devise_default_strategies if names.first == :defaults
138
+ namestr = names.map{|n| ":#{n}"}.join(', ')
139
+ insert_into_model model_name do
140
+ "devise #{namestr}"
141
+ end
142
+ end
143
+
144
+ def devise_default_strategies
145
+ [:database_authenticatable, :confirmable, :recoverable, :rememberable, :trackable, :validatable]
146
+ end
147
+
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,68 @@
1
+ require 'sugar-high/file'
2
+ require 'sugar-high/module'
3
+ require 'cream'
4
+ require 'rails3_artifactor'
5
+ require 'logging_assist'
6
+
7
+ module Permits
8
+ module Generators
9
+ class ConfigGenerator < Rails::Generators::Base
10
+ desc "Configure Permits"
11
+
12
+ # ORM to use
13
+ class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
14
+ class_option :roles, :type => :array, :default => ['guest', 'admin'], :desc => "Roles for permits"
15
+ class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
16
+
17
+ def configure_permits
18
+ logger.add_logfile :logfile => logfile if logfile
19
+ logger.debug "Configure Permits"
20
+
21
+ permits_gems
22
+ permits_initializer
23
+
24
+ # Run permits generator to generate permit for each role
25
+ rgen "permits --roles #{roles}"
26
+ end
27
+
28
+ protected
29
+
30
+ include Rails3::Assist::BasicLogger
31
+ extend Rails3::Assist::UseMacro
32
+
33
+ use_helpers :app, :file, :special
34
+
35
+ # rails generate ...
36
+ def rgen command
37
+ execute "rails g #{command}"
38
+ end
39
+
40
+ def execute command
41
+ logger.debug command
42
+ run command
43
+ end
44
+
45
+ def logfile
46
+ options[:logfile]
47
+ end
48
+
49
+ def roles
50
+ options[:roles]
51
+ end
52
+
53
+ def orm
54
+ options[:orm]
55
+ end
56
+
57
+ def permits_gems
58
+ gem 'cancan-permits'
59
+ end
60
+
61
+ def permits_initializer
62
+ create_initializer :permits do
63
+ "Permits::Ability.orm = :#{options[:orm]}"
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,113 @@
1
+ require 'sugar-high/file'
2
+ require 'sugar-high/module'
3
+ require 'cream'
4
+ require 'rails3_artifactor'
5
+ require 'logging_assist'
6
+
7
+ module Roles
8
+ module Generators
9
+ class ConfigGenerator < Rails::Generators::Base
10
+ desc "Configure Roles"
11
+
12
+ # ORM to use
13
+ class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
14
+ class_option :strategy, :type => :string, :default => 'role_string', :desc => "Roles strategy to use"
15
+ class_option :roles, :type => :array, :default => ['guest', 'admin'], :desc => "Valid roles to use"
16
+ class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
17
+ class_option :default_roles, :type => :boolean, :default => true, :desc => "Create default roles :admin and :guest"
18
+
19
+ def configure_roles
20
+ logger.add_logfile :logfile => logfile if logfile
21
+ roles_gems
22
+ create_roles
23
+ set_valid_roles_cream
24
+ use_roles_strategy
25
+ end
26
+
27
+ protected
28
+
29
+ include Rails3::Assist::BasicLogger
30
+ extend Rails3::Assist::UseMacro
31
+
32
+ use_helpers :model, :file
33
+
34
+ def logfile
35
+ options[:logfile]
36
+ end
37
+
38
+ # rails generate ...
39
+ def rgen command
40
+ execute "rails g #{command}"
41
+ end
42
+
43
+ def execute command
44
+ logger.debug command
45
+ run command
46
+ end
47
+
48
+ def orm
49
+ options[:orm]
50
+ end
51
+
52
+ def strategy
53
+ options[:strategy]
54
+ end
55
+
56
+ def roles
57
+ options[:roles].join(' ')
58
+ end
59
+
60
+ def default_roles?
61
+ options[:default_roles]
62
+ end
63
+
64
+ def roles_gems
65
+ gem "roles_#{orm}"
66
+ # bundle_install
67
+ end
68
+
69
+ def bundle_install
70
+ run "bundle install"
71
+ end
72
+
73
+ def roles_generator
74
+ "#{orm}:roles"
75
+ end
76
+
77
+ def create_roles
78
+ rgen "#{roles_generator} --strategy #{strategy} --roles #{roles} #{default_roles}"
79
+ end
80
+
81
+ def set_valid_roles_cream
82
+ user_exist?
83
+ if initializer_file? :cream
84
+ if read_model(:user) =~ /valid_roles_are/
85
+ replace_in_model_file :user, :where => /valid_roles_are\s+[(.*)]/, :with => 'valid_roles_are Cream.roles'
86
+ else
87
+ insert_into_model :user do
88
+ "valid_roles_are Cream.roles"
89
+ end
90
+ end
91
+ else
92
+ say "Missing initializer file for cream. Please run Cream config generator to create this initializer"
93
+ end
94
+ end
95
+
96
+ def user_exist?
97
+ raise "User model missing. Please create a User model before running this generator" if !model_file?(:user)
98
+ end
99
+
100
+ def use_roles_strategy
101
+ user_exist?
102
+
103
+ unless read_model(:user) =~ /use_roles_strategy/
104
+ inject_into_file model_file(:user), "use_roles_strategy :admin_flag\n\n", :before => "class"
105
+ end
106
+ end
107
+
108
+ def default_roles
109
+ default_roles? ? '--default-roles' : '--no-default-roles'
110
+ end
111
+ end
112
+ end
113
+ end
@@ -1,3 +1,5 @@
1
+ require 'spec_helper'
2
+
1
3
  Cream.setup do |config|
2
4
  # ==> Mailer Configuration
3
5
  # Configure the e-mail address which will be shown in DeviseMailer.