cream 0.8.5 → 0.8.6

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog.txt CHANGED
@@ -1,3 +1,7 @@
1
+ 0.8.5 - Jan 4, 2011
2
+ -----
3
+ Major refactoring to extract common logic in generators in order to make the logic more transparent and more stable and consistent!
4
+
1
5
  0.8.4 - Jan 1, 2011
2
6
  -----
3
7
  Fixed argument error in #routes_configure! in devise:config generator
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.5
1
+ 0.8.6
data/cream.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cream}
8
- s.version = "0.8.5"
8
+ s.version = "0.8.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = %q{2011-01-03}
12
+ s.date = %q{2011-01-04}
13
13
  s.description = %q{Provides assistance for setting up Session, Role and Permission systems for a Rails 3 app. Support for multiple ORMs}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -52,10 +52,19 @@ Gem::Specification.new do |s|
52
52
  "lib/generators/cancan/config/config_generator.rb",
53
53
  "lib/generators/cream/app/app_generator.rb",
54
54
  "lib/generators/cream/full_config/full_config_generator.rb",
55
+ "lib/generators/cream/helpers/all.rb",
56
+ "lib/generators/cream/helpers/args_helper.rb",
57
+ "lib/generators/cream/helpers/execute_helper.rb",
58
+ "lib/generators/cream/helpers/orm_helper.rb",
59
+ "lib/generators/cream/helpers/strategy_helper.rb",
55
60
  "lib/generators/cream/views/haml_util.rb",
56
61
  "lib/generators/cream/views/views_generator.rb",
57
62
  "lib/generators/cream_refactor.rb",
63
+ "lib/generators/devise/config/app_helper.rb",
58
64
  "lib/generators/devise/config/config_generator.rb",
65
+ "lib/generators/devise/config/gem_helper.rb",
66
+ "lib/generators/devise/users/helper.rb",
67
+ "lib/generators/devise/users/routes_helper.rb",
59
68
  "lib/generators/devise/users/users_generator.rb",
60
69
  "lib/generators/permits/config/config_generator.rb",
61
70
  "lib/generators/roles/config/config_generator.rb",
@@ -4,6 +4,7 @@ require 'cream'
4
4
  require 'rails3_artifactor'
5
5
  require 'logging_assist'
6
6
  require 'active_support'
7
+ require 'generators/cream/helpers/all'
7
8
 
8
9
  module Cream
9
10
  module Generators
@@ -14,7 +15,10 @@ module Cream
14
15
  class_option :strategy, :type => :string, :default => 'role_string', :desc => "Role strategy to use"
15
16
 
16
17
  # Create Admin user
17
- class_option :admin_user, :type => :boolean, :default => false, :desc => "Create admin user"
18
+ class_option :user_class, :type => :string, :default => 'User', :desc => "User class"
19
+ class_option :admin_class, :type => :string, :default => nil, :desc => "Admin user class"
20
+ class_option :role_class, :type => :string, :default => 'Role', :desc => "Role class"
21
+ class_option :user_role_class, :type => :string, :default => 'UserRole', :desc => "UserRole class"
18
22
 
19
23
  # Roles
20
24
  class_option :default_roles, :type => :boolean, :default => true, :desc => "Create default roles :admin and :guest"
@@ -25,44 +29,38 @@ module Cream
25
29
 
26
30
  class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
27
31
 
28
- class_option :configure, :type => :array, :default => [], :desc => "Which generators to run"
32
+ class_option :configure, :type => :array, :default => [], :desc => "Finetune which generators to run: app, permits, roles, devise, cancan"
29
33
  class_option :gems, :type => :boolean, :default => true, :desc => "Add gems to gemfile?"
30
34
  class_option :migrations, :type => :boolean, :default => false, :desc => "Autorun database migrations?", :aliases => '-m'
31
35
 
32
- def main_flow
33
- return nil if !validate_orm
34
- cream_initializer
35
- # cream_gems if gems?
36
- run_generators
37
- run_migrations if run_migrations?
36
+ def main
37
+ execute_generator if validate_orm && validate_strategy
38
38
  end
39
39
 
40
- # -----------------
40
+ # -----------------
41
41
  protected
42
42
 
43
43
  include Rails3::Assist::BasicLogger
44
44
  extend Rails3::Assist::UseMacro
45
45
 
46
- use_helpers :app, :special, :file
46
+ include Cream::GeneratorHelper
47
47
 
48
- def validate_orm
49
- if !valid_orms.include?(orm)
50
- say "ORM #{orm} is not currently supported. Please use one of: #{valid_orms}", :red
51
- false
52
- end
53
- true
54
- end
55
-
56
- def run_migrations?
57
- options[:migrations]
58
- end
48
+ use_helpers :app, :special, :file
59
49
 
60
- def logfile
61
- options[:logfile]
50
+ def execute_generator
51
+ cream_initializer
52
+ run_generators
53
+ run_migrations if run_migrations?
62
54
  end
63
55
 
64
- def gems?
65
- options[:gems]
56
+ # use this to define which part of the app this generator should configure (which sub-generators to run!)
57
+ def configures
58
+ return default_configures if options[:configure].empty?
59
+ options[:configure].map{|c| c.to_sym}
60
+ end
61
+
62
+ def default_configures
63
+ [:app, :permits, :roles, :devise, :cancan]
66
64
  end
67
65
 
68
66
  def run_generators
@@ -85,9 +83,8 @@ module Cream
85
83
  end
86
84
 
87
85
  def run_devise
88
- # rgen "devise:config --orm #{orm} --no-gems"
89
- rgen "devise:config --orm #{orm} #{admin_user_option}"
90
- rgen "devise:users --orm #{orm} #{admin_user_option} --no-gems"
86
+ rgen "devise:config #{user_class} --orm #{orm} #{admin_user_option}"
87
+ rgen "devise:users --orm #{orm} --roles #{roles_list} #{admin_user_option} --no-gems"
91
88
  end
92
89
 
93
90
  def run_cancan
@@ -101,90 +98,7 @@ module Cream
101
98
  def run_permits
102
99
  rgen "permits:config --orm #{orm} --roles #{roles_list} --no-gems"
103
100
  end
104
-
105
- def admin_user_option
106
- admin_user? ? '--admin-user' : ''
107
- end
108
-
109
- def strategy
110
- options[:strategy]
111
- end
112
-
113
- def admin_user?
114
- options[:admin_user]
115
- end
116
-
117
- def orm
118
- @orm ||= get_orm options[:orm].to_s.underscore.to_sym
119
- end
120
-
121
- def valid_orms
122
- active_record + data_mapper + mongo_mapper + [:couch_db, :mongoid]
123
- end
124
-
125
- def active_record
126
- [:ar, :active_record]
127
- end
128
-
129
- def mongo_mapper
130
- [:mm, :mongo_mapper]
131
- end
132
-
133
- def data_mapper
134
- [:dm, :data_mapper]
135
- end
136
-
137
- def get_orm orm_name
138
- return :active_record if active_record.include? orm_name
139
- return :mongo_mapper if mongo_mapper.include? orm_name
140
- return :data_mapper if data_mapper.include? orm_name
141
- return :couch_db if orm_name == :couch_db
142
- return :mongoid if orm_name == :mongoid
143
- end
144
-
145
- def default_roles?
146
- options[:default_roles]
147
- end
148
-
149
- def roles_list
150
- roles.join(' ')
151
- end
152
-
153
- def roles
154
- defaults = default_roles? ? ['guest', 'admin'] : []
155
- options[:roles] + defaults
156
- end
157
-
158
- def sym_roles
159
- roles.map(&:to_sym)
160
- end
161
-
162
- # rails generate ...
163
- def rgen command
164
- execute "rails g #{command}"
165
- end
166
-
167
- def execute command
168
- logger.debug command
169
- run command
170
- end
171
-
172
- def bundle_install
173
- run "bundle install"
174
- end
175
-
176
- def configures
177
- return [:app, :permits, :roles, :devise, :cancan] if options[:configure].empty?
178
- options[:configure].map{|c| c.to_sym}
179
- end
180
-
181
-
182
- # def cream_gems
183
- # if !has_gem? :cream
184
- # gem 'cream'
185
- # end
186
- # end
187
-
101
+
188
102
  def cream_initializer
189
103
  create_initializer :cream do
190
104
  %Q{Cream.setup do |config|
@@ -199,6 +113,13 @@ end}
199
113
  logger.debug "configure_locale, copy from: #{src}"
200
114
  copy_file src, "config/locales/cream.en.yml"
201
115
  end
116
+
117
+ private
118
+
119
+ def run_migrations?
120
+ options[:migrations]
121
+ end
122
+
202
123
  end
203
124
  end
204
125
  end
@@ -0,0 +1,14 @@
1
+ require_all File.dirname(__FILE__)
2
+
3
+ module Cream
4
+ module GeneratorHelper
5
+ def self.included(base)
6
+ base.class_eval do
7
+ include Orm
8
+ include Executor
9
+ include Args
10
+ include Strategy
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,78 @@
1
+ module Cream
2
+ module GeneratorHelper
3
+ module Args
4
+ def logfile
5
+ options[:logfile]
6
+ end
7
+
8
+ def default_roles?
9
+ options[:default_roles]
10
+ end
11
+
12
+ def roles
13
+ defaults = default_roles? ? ['guest', 'admin'] : []
14
+ (options[:roles] + defaults).flat_uniq
15
+ end
16
+
17
+ def has_user_model?
18
+ model_file?(user_class)
19
+ end
20
+
21
+ def set_orm
22
+ self.class.use_orm :"#{orm}"
23
+ end
24
+
25
+ def admin_user_option
26
+ admin_user? ? "--admin-user #{admin_user}" : ''
27
+ end
28
+
29
+ def roles_list
30
+ roles.join(' ')
31
+ end
32
+
33
+ def gems?
34
+ options[:gems]
35
+ end
36
+
37
+ def document_store?
38
+ [:mongoid, :mongo_mapper, :couch_db].include? orm
39
+ end
40
+
41
+ def active_record?
42
+ active_record.include? orm
43
+ end
44
+
45
+ def mongoid?
46
+ orm == :mongoid
47
+ end
48
+
49
+ def strategy
50
+ options[:strategy]
51
+ end
52
+
53
+ def admin_user?
54
+ options[:admin_user]
55
+ end
56
+
57
+ def sym_roles
58
+ roles.map(&:to_sym)
59
+ end
60
+
61
+ def user_class
62
+ options[:user_class] || 'User'
63
+ end
64
+
65
+ def admin_class
66
+ options[:admin_class] || 'Admin'
67
+ end
68
+
69
+ def role_class
70
+ options[:role_class] || 'Role'
71
+ end
72
+
73
+ def user_role_class
74
+ options[:user_role_class] || 'UserRole'
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,23 @@
1
+ module Cream
2
+ module GeneratorHelper
3
+ module Executor
4
+ def add_gem name
5
+ gem name if !has_gem? name
6
+ end
7
+
8
+ # rails generate ...
9
+ def rgen command
10
+ execute "rails g #{command}"
11
+ end
12
+
13
+ def execute command
14
+ logger.debug command
15
+ run command
16
+ end
17
+
18
+ def bundle_install
19
+ run "bundle install"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,60 @@
1
+ module Cream
2
+ module GeneratorHelper
3
+ module Orm
4
+ def validate_orm
5
+ if !valid_orms.include?(orm)
6
+ say "ERROR: ORM #{orm} is not currently supported. Please use one of: #{valid_orms_list}", :red
7
+ return false
8
+ end
9
+ true
10
+ end
11
+
12
+ def orm
13
+ @orm ||= get_orm options[:orm].to_s.underscore.to_sym
14
+ end
15
+
16
+ def valid_orms_list
17
+ valid_orms.map(&:to_s).join(', ')
18
+ end
19
+
20
+ def valid_orms
21
+ active_record + data_mapper + mongo_mapper + [:couch_db, :mongoid]
22
+ end
23
+
24
+ def active_record
25
+ [:ar, :active_record]
26
+ end
27
+
28
+ def is_active_record? name
29
+ active_record.include? name
30
+ end
31
+
32
+ def mongo_mapper
33
+ [:mm, :mongo_mapper]
34
+ end
35
+
36
+ def is_mongo_mapper? name
37
+ mongo_mapper.include? name
38
+ end
39
+
40
+ def data_mapper
41
+ [:dm, :data_mapper]
42
+ end
43
+
44
+ def is_data_mapper? name
45
+ data_mapper.include? name
46
+ end
47
+
48
+ def get_orm name
49
+ return :active_record if is_active_record? name
50
+ return :mongo_mapper if is_mongo_mapper? name
51
+ return :data_mapper if is_data_mapper? name
52
+
53
+ return :couch_db if orm_name == :couch_db
54
+ return :mongoid if orm_name == :mongoid
55
+
56
+ raise ArgumentError, "ERROR: Cream does not currently support the orm: #{name}"
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,31 @@
1
+ module Cream
2
+ module GeneratorHelper
3
+ module Strategy
4
+ def validate_strategy
5
+ if !valid_strategies.include?(strategy.to_sym)
6
+ say "ERROR: Strategy '#{strategy}' is not currently supported by Cream. Please use one of: #{valid_strategies_list}", :red
7
+ return false
8
+ end
9
+ true
10
+ end
11
+
12
+ def valid_strategies_list
13
+ valid_strategies.map(&:to_s).join(', ')
14
+ end
15
+
16
+ def valid_strategies
17
+ @strategies ||= basic_strategies
18
+ @strategies << document_store_strategies if document_store?
19
+ @strategies
20
+ end
21
+
22
+ def basic_strategies
23
+ [:admin_flag, :role_string, :one_role, :many_roles, :roles_mask, :roles_string]
24
+ end
25
+
26
+ def document_store_strategies
27
+ [:embed_one_role, :embed_many_roles, :role_strings]
28
+ end
29
+ end
30
+ end
31
+ end