cream 0.8.6 → 0.8.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/Changelog.txt +6 -1
  2. data/Design Ideas.textile +463 -0
  3. data/Gemfile +1 -0
  4. data/README.textile +158 -34
  5. data/Rakefile +8 -7
  6. data/VERSION +1 -1
  7. data/app/views/cream/menu/_admin_login_items.html.erb +2 -2
  8. data/app/views/cream/menu/_login_items.html.erb +2 -2
  9. data/app/views/cream/menu/_registration_items.html.erb +2 -2
  10. data/config/locales/cream.da.yml +16 -0
  11. data/cream.gemspec +38 -27
  12. data/lib/cream.rb +1 -0
  13. data/lib/cream/configure/after_init/role_config.rb +25 -21
  14. data/lib/cream/configure/rails.rb +8 -5
  15. data/lib/cream/controller/application_controller.rb +22 -0
  16. data/lib/cream/helper/role.rb +102 -11
  17. data/lib/generators/cream/app/app_generator.rb +50 -26
  18. data/lib/generators/cream/full_config/full_config_generator.rb +44 -9
  19. data/lib/generators/cream/helpers/all.rb +1 -0
  20. data/lib/generators/cream/helpers/execute_helper.rb +0 -4
  21. data/lib/generators/cream/helpers/gemfile_helper.rb +28 -0
  22. data/lib/generators/cream/helpers/orm_helper.rb +6 -2
  23. data/lib/generators/cream/helpers/strategy_helper.rb +28 -0
  24. data/lib/generators/cream/views/haml_util.rb +3 -4
  25. data/lib/generators/cream/views/views_generator.rb +13 -13
  26. data/lib/generators/devise/config/app_helper.rb +1 -1
  27. data/lib/generators/devise/config/config_generator.rb +1 -3
  28. data/lib/generators/devise/config/{gem_helper.rb → gem_config_helper.rb} +0 -23
  29. data/lib/generators/devise/customize/customize_generator.rb +49 -0
  30. data/lib/generators/devise/customize/customize_messages.rb +52 -0
  31. data/lib/generators/devise/customize/helpers/query_customizers.rb +43 -0
  32. data/lib/generators/devise/customize/helpers/recover_login.rb +80 -0
  33. data/lib/generators/devise/customize/helpers/username_helper.rb +149 -0
  34. data/lib/generators/devise/users/users_generator.rb +1 -3
  35. data/lib/generators/permits/config/config_generator.rb +1 -3
  36. data/lib/generators/roles/config/config_generator.rb +26 -4
  37. data/sandbox/any_user.rb +98 -0
  38. data/{lib/generators → sandbox}/cream_refactor.rb +0 -0
  39. data/sandbox/str_test.rb +50 -0
  40. data/spec/cream/configure/rails_role_spec.rb +1 -1
  41. data/spec/cream/helper/role_spec.rb +71 -21
  42. data/wiki/Cream-generators-overview.textile +79 -0
  43. data/wiki/How to gollum wiki.txt +13 -0
  44. metadata +107 -72
  45. data/wiki/CONFIG_GENERATOR.txt +0 -21
  46. data/wiki/DESIGN.txt +0 -21
  47. data/wiki/INSTALLATION.txt +0 -6
  48. data/wiki/PERMITS.txt +0 -32
  49. data/wiki/ROLE_STRATEGIES.txt +0 -40
  50. data/wiki/SPEC_NOTES.txt +0 -6
  51. data/wiki/VIEWS_GENERATOR.txt +0 -35
  52. data/wiki/VIEW_HELPERS.txt +0 -162
data/lib/cream.rb CHANGED
@@ -23,3 +23,4 @@ require_all File.dirname(__FILE__) + '/cream/rails'
23
23
  require 'cream/roles_config'
24
24
 
25
25
 
26
+
@@ -1,18 +1,24 @@
1
1
  module Cream::View
2
- module Role
3
- # admin?, guest? ...
4
- if defined? Cream::Role
5
- Cream::Role.available.each do |role|
6
- class_eval %{
7
- def #{role}_area &block
8
- area_for_roles(#{role}, &block)
9
- end
10
-
11
- def for_#{role}(&block)
12
- for_roles(#{role}, &block)
13
- end
14
- }
2
+ module Role
3
+ def self.roles_available
4
+ begin
5
+ return Cream::Role.available if defined? Cream::Role
6
+ rescue
15
7
  end
8
+ [:guest, :admin]
9
+ end
10
+
11
+ # admin?, guest? ...
12
+ Cream::View::Role.roles_available.each do |role|
13
+ class_eval %{
14
+ def #{role}_area &block
15
+ area_for_role(:#{role}, &block)
16
+ end
17
+
18
+ def for_#{role}(&block)
19
+ for_role(:#{role}, &block)
20
+ end
21
+ }
16
22
  end
17
23
  end
18
24
  end
@@ -20,14 +26,12 @@ end
20
26
  module Cream::Helper
21
27
  module Role
22
28
  # admin?, guest? ...
23
- if defined? Cream::Role
24
- Cream::Role.available.each do |role|
25
- class_eval %{
26
- def #{role}?
27
- has_role? :#{role}
28
- end
29
- }
30
- end
29
+ Cream::View::Role.roles_available.each do |role|
30
+ class_eval %{
31
+ def #{role}?
32
+ has_role? :#{role}
33
+ end
34
+ }
31
35
  end
32
36
  end
33
37
  end
@@ -1,6 +1,8 @@
1
1
  require 'active_support/railtie'
2
2
  require 'r3_plugin_toolbox'
3
3
 
4
+ require_all File.dirname(__FILE__) + '/after_init'
5
+
4
6
  Rails3::Plugin::Extender.new do
5
7
  # extend action_controller with methods from some modules
6
8
 
@@ -16,11 +18,12 @@ Rails3::Plugin::Extender.new do
16
18
  extend_from_module Cream::Helper, :role
17
19
  end
18
20
 
19
- after :initialize do
20
- require_all File.dirname(__FILE__) + '/after_init'
21
-
22
- # Rails3.with_configuration do
23
- # load_paths += %W(#{Rails.root}/app/permits)
21
+ after :initialize do
22
+ load File.dirname(__FILE__) + '/after_init/role_config.rb'
23
+ #
24
+ # extend_rails :view do
25
+ # extend_from_module Cream::View, :role
26
+ # extend_from_module Cream::Helper, :role
24
27
  # end
25
28
  end
26
29
  end
@@ -0,0 +1,22 @@
1
+ class ApplicationController < ActionController::Base
2
+ def current_user
3
+ cu = super
4
+ if defined? Cream::Role
5
+ # Try to get instance of any of the available roles as the current user
6
+ Cream::Role.available.each do |role|
7
+ method = :"current_#{role}"
8
+ cu ||= send method if respond_to?(method)
9
+ end
10
+ end
11
+ # if all else fails, the user is likely not signed in so create a Guest user for now
12
+ cu ||= Guest.create if defined?(Guest)
13
+ end
14
+
15
+ def user_signed_in?
16
+ current_user && !current_user.has_role?(:guest)
17
+ end
18
+
19
+ def user_session
20
+ user_signed_in? ? super : session
21
+ end
22
+ end
@@ -1,20 +1,38 @@
1
1
  module Cream::Helper
2
2
  module Role
3
+
4
+ # for_any_user :signed_in
5
+ # for_any_user :not_logged_in
6
+ # for_any_user :not_logged_in => true
7
+
8
+ def for_any_user options = nil, &block
9
+ yield if Labels.state_check Labels.extract(options), current_user
10
+ end
11
+
12
+ # not_for_any_user :signed_in
13
+ # not_for_any_user :logged_out
14
+ # not_for_any_user :logged_in => true
15
+
16
+ def not_for_any_user options = nil, &block
17
+ return if Labels.state_check Labels.extract(options), current_user
18
+ yield
19
+ end
20
+
3
21
  # does the user have ANY of the given roles?
4
22
  # Uses generic roles API
5
- def has_role?(*roles)
6
- current_user.has_role? roles.flatten
23
+ def has_role? user_role
24
+ current_user && current_user.has_role?(user_role)
7
25
  end
8
26
 
9
27
  # does the user have ALL of the given roles?
10
28
  # Uses generic roles API
11
- def has_roles?(*roles)
12
- current_user.has_roles? roles.flatten
29
+ def has_roles? *roles
30
+ current_user && current_user.has_roles?(roles.flat_uniq)
13
31
  end
14
32
 
15
33
  # returns true if the current user owns the object
16
34
  # tries default 'owner' relations if none given as an argument
17
- def owner?(obj, relation=nil)
35
+ def owner? obj, relation=nil
18
36
  if relation
19
37
  return true if user_relation?(obj, relation)
20
38
  end
@@ -25,24 +43,97 @@ module Cream::Helper
25
43
  end
26
44
 
27
45
  # execute block if user DOES have any of the given roles
28
- def for_roles(*user_roles, &block)
46
+ def for_roles *user_roles, &block
47
+ user_roles = user_roles.flatten
48
+ yield if has_roles?(user_roles) && block
49
+ end
50
+
51
+ def for_any_role *user_roles, &block
29
52
  user_roles = user_roles.flatten
30
53
  yield if has_role?(user_roles) && block
31
54
  end
32
- alias_method :for_role, :for_roles
33
55
 
56
+ def for_role user_role, &block
57
+ if is_negation_role?(user_role)
58
+ not_for_role(user_role, &block)
59
+ return
60
+ end
61
+ yield if has_role?(user_role) && block
62
+ end
63
+
34
64
  # execute block if user DOES NOT have any of the given roles
35
65
  def not_for_roles(*user_roles, &block)
36
66
  user_roles = user_roles.flatten
37
- yield if !has_role?(user_roles) && block
67
+ yield if !has_roles?(user_roles) && block
68
+ end
69
+
70
+ def not_for_role(user_role, &block)
71
+ if is_negation_role?(user_role)
72
+ for_role(user_role, &block)
73
+ return
74
+ end
75
+ yield if !has_role?(user_role) && block
38
76
  end
39
- alias_method :not_for_role, :not_for_roles
40
77
 
41
- protected
78
+ protected
79
+
80
+ def is_negation_role? user_role
81
+ !(:user_role.to_s =~ /^not_/).nil?
82
+ end
42
83
 
43
84
  def user_relation? obj, relation
44
85
  raise ArgumentError, "User method must be a Symbol or String" if !relation.kind_of_label?
45
- current_user == obj.send(relation) if obj.respond_to? relation
86
+ current_user && is_owner?(current_user, obj, relation)
87
+ end
88
+
89
+ def is_owner? user, obj, relation
90
+ user == obj.send(relation) if obj.respond_to? relation
91
+ end
92
+
93
+ private
94
+
95
+ module Labels
96
+ class << self
97
+ def extract options
98
+ case options
99
+ when Symbol
100
+ return :logged_in if logged_in_labels.include?(options)
101
+ return :logged_out if logged_out_labels.include?(options)
102
+ when Hash
103
+ return :logged_in if logged_in_labels.any? {|lab| options[lab] }
104
+ return :logged_out if logged_out_labels.any? {|lab| options[lab] }
105
+ end
106
+ raise ArgumentException, "Unknown option #{options}"
107
+ end
108
+
109
+ def state_check state, current_user
110
+ logged_in_check(state, current_user) || logged_out_check(state, current_user)
111
+ end
112
+
113
+ def logged_in_check state, current_user
114
+ state == :logged_in && is_not_guest?(current_user)
115
+ end
116
+
117
+ def logged_out_check state, current_user
118
+ state == :logged_out && is_guest?(current_user)
119
+ end
120
+
121
+ def is_not_guest? current_user
122
+ !current_user || (current_user && !current_user.is?(:guest))
123
+ end
124
+
125
+ def is_guest? current_user
126
+ current_user && current_user.is?(:guest)
127
+ end
128
+
129
+ def logged_in_labels
130
+ [:logged_in, :signed_in, :not_logged_out, :not_signed_out]
131
+ end
132
+
133
+ def logged_out_labels
134
+ [:logged_out, :signed_out, :not_logged_in, :not_signed_in]
135
+ end
136
+ end
46
137
  end
47
138
  end
48
139
  end
@@ -3,6 +3,7 @@ require 'sugar-high/module'
3
3
  require 'cream'
4
4
  require 'rails3_artifactor'
5
5
  require 'logging_assist'
6
+ require 'generators/cream/helpers/all'
6
7
 
7
8
  module Cream
8
9
  module Generators
@@ -10,42 +11,31 @@ module Cream
10
11
  desc "Configure Cream App"
11
12
 
12
13
  # 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
-
14
+ class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
15
+ class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
16
+ class_option :guest_user, :type => :boolean, :default => true, :desc => "Create guest user"
17
+
16
18
  def configure_application
17
19
  logger.add_logfile :logfile => logfile if logfile
18
- app_orm
20
+
21
+ app_orm unless active_record?
22
+
23
+ set_orm
24
+
19
25
  app_routes
20
26
  app_layout
27
+
28
+ create_guest_user if guest_user?
21
29
  end
22
30
 
23
31
  protected
24
32
 
33
+ include Cream::GeneratorHelper
25
34
  include Rails3::Assist::BasicLogger
26
35
  extend Rails3::Assist::UseMacro
27
- use_helpers :app, :special, :file, :view
28
-
29
- def logfile
30
- options[:logfile]
31
- end
36
+ use_helpers :app, :special, :file, :view, :model
32
37
 
33
- def orm
34
- options[:orm]
35
- end
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 app_orm
48
- return if orm == 'active_record'
38
+ def app_orm
49
39
  File.replace_content_from application_file, :where => "require 'rails/all'" do
50
40
  %q{
51
41
  require "action_controller/railtie"
@@ -61,8 +51,8 @@ require "rails/test_unit/railtie"
61
51
  # if no :welcome controller
62
52
  if !(routes =~ /root\s+:/)
63
53
  # create one with an 'index' view.
64
- File.insert_into routes_file, :after => 'do', :content => 'root :to => "welcome#index"'
65
54
  rgen "controller Welcome index"
55
+ File.insert_into routes_file, :after => 'do', :content => 'root :to => "welcome#index"'
66
56
  end
67
57
  end
68
58
 
@@ -79,6 +69,40 @@ require "rails/test_unit/railtie"
79
69
  insert_into_view :layouts => :application, :after => '<body>' do
80
70
  %Q{<p class="#{name}"><%= #{name} %></p>}
81
71
  end
72
+ end
73
+
74
+ def create_guest_user
75
+ remove_model :guest if has_model?(:guest)
76
+ create_model :guest do
77
+ %Q{
78
+ def self.create
79
+ Guest.new
80
+ end
81
+
82
+ def is? role
83
+ role == :guest
84
+ end
85
+
86
+ def has_role? role
87
+ is? role
88
+ end
89
+
90
+ def has_roles? *roles
91
+ false
92
+ end
93
+
94
+ def has_any_role? *roles
95
+ roles.flat_uniq..to_symbols.include? :guest
96
+ end
97
+ }
98
+ end
99
+ say "A Guest user model has been created. Users that have not signed in will get an instance of this class as the current_user."
100
+ end
101
+
102
+ protected
103
+
104
+ def guest_user?
105
+ options[:gust_user]
82
106
  end
83
107
  end
84
108
  end
@@ -27,12 +27,17 @@ module Cream
27
27
  # ORM to use
28
28
  class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
29
29
 
30
+ class_option :locales, :type => :array, :default => ['all'], :desc => "List of locales - 'all' means ALL locales"
30
31
  class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
31
32
 
32
33
  class_option :configure, :type => :array, :default => [], :desc => "Finetune which generators to run: app, permits, roles, devise, cancan"
33
34
  class_option :gems, :type => :boolean, :default => true, :desc => "Add gems to gemfile?"
34
35
  class_option :migrations, :type => :boolean, :default => false, :desc => "Autorun database migrations?", :aliases => '-m'
35
36
 
37
+ # Devise customize
38
+ class_option :user_name, :type => :boolean, :default => true, :desc => "Add username as login option"
39
+ class_option :login_type, :type => :string, :default => 'generic', :desc => "How to login: 'email', 'username', 'generic' (i.e 'username' or 'email')"
40
+
36
41
  def main
37
42
  execute_generator if validate_orm && validate_strategy
38
43
  end
@@ -49,6 +54,7 @@ module Cream
49
54
 
50
55
  def execute_generator
51
56
  cream_initializer
57
+ cream_locales if locales
52
58
  run_generators
53
59
  run_migrations if run_migrations?
54
60
  end
@@ -85,6 +91,7 @@ module Cream
85
91
  def run_devise
86
92
  rgen "devise:config #{user_class} --orm #{orm} #{admin_user_option}"
87
93
  rgen "devise:users --orm #{orm} --roles #{roles_list} #{admin_user_option} --no-gems"
94
+ rgen "devise:customize #{user_class} --orm #{orm} --login-type #{login_type} #{user_name_option}"
88
95
  end
89
96
 
90
97
  def run_cancan
@@ -92,7 +99,7 @@ module Cream
92
99
  end
93
100
 
94
101
  def run_roles
95
- rgen "roles:config --orm #{orm} --roles #{roles_list} --strategy #{strategy}"
102
+ rgen "roles:config #{user_class} --orm #{orm} --roles #{roles_list} --strategy #{strategy}"
96
103
  end
97
104
 
98
105
  def run_permits
@@ -103,23 +110,51 @@ module Cream
103
110
  create_initializer :cream do
104
111
  %Q{Cream.setup do |config|
105
112
  config.roles = #{sym_roles.inspect}
106
- end}
113
+ end
114
+
115
+ require 'cream/configure/rails'
116
+ }
107
117
  end
108
118
  end
109
119
 
110
- def cream_locale
111
- src = File.expand_path "config/locales/cream.en.yml".path.up(2)
112
- # src = "config/locales/en.yml"
113
- logger.debug "configure_locale, copy from: #{src}"
114
- copy_file src, "config/locales/cream.en.yml"
120
+ def login_type
121
+ options[:login_type]
122
+ end
123
+
124
+ def username?
125
+ options[:user_name]
126
+ end
127
+
128
+ def user_name_option
129
+ " --user-name " if username?
130
+ end
131
+
132
+ def locales
133
+ options[:locales]
134
+ end
135
+
136
+ def supported_locales
137
+ [:en, :da]
138
+ end
139
+
140
+ def locales_to_generate
141
+ supported_locales - locales
142
+ end
143
+
144
+ def cream_locales
145
+ return if locales_to_generate.empty?
146
+ logger.debug "Generate Cream locale files"
147
+ locales_to_generate.each do |locale|
148
+ src = File.expand_path "config/locales/cream.#{locale}.yml".path.up(5), __FILE__
149
+ copy_file src, "config/locales/cream.#{locale}.yml"
150
+ end
115
151
  end
116
152
 
117
153
  private
118
154
 
119
155
  def run_migrations?
120
156
  options[:migrations]
121
- end
122
-
157
+ end
123
158
  end
124
159
  end
125
160
  end