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
@@ -8,6 +8,7 @@ module Cream
8
8
  include Executor
9
9
  include Args
10
10
  include Strategy
11
+ include Gemfile
11
12
  end
12
13
  end
13
14
  end
@@ -1,10 +1,6 @@
1
1
  module Cream
2
2
  module GeneratorHelper
3
3
  module Executor
4
- def add_gem name
5
- gem name if !has_gem? name
6
- end
7
-
8
4
  # rails generate ...
9
5
  def rgen command
10
6
  execute "rails g #{command}"
@@ -0,0 +1,28 @@
1
+ module Cream
2
+ module GeneratorHelper
3
+ module Gemfile
4
+ def add_gem_version name, version
5
+ if !has_gem? name
6
+ logger.debug "Adding gem: #{name}, #{version}"
7
+ gem name, :version => version
8
+ else
9
+ logger.debug "gem: #{name}, #{version} already in Gemfile"
10
+ end
11
+ end
12
+
13
+ def add_gem name, version = nil
14
+ if version
15
+ add_gem_version name, version
16
+ return
17
+ end
18
+
19
+ if !has_gem? name
20
+ logger.debug "Adding gem: #{name}"
21
+ gem name
22
+ else
23
+ logger.debug "gem: #{name} already in Gemfile"
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -45,13 +45,17 @@ module Cream
45
45
  data_mapper.include? name
46
46
  end
47
47
 
48
+ def is_default_devise_orm?
49
+ [:active_record, :mongoid].include? orm
50
+ end
51
+
48
52
  def get_orm name
49
53
  return :active_record if is_active_record? name
50
54
  return :mongo_mapper if is_mongo_mapper? name
51
55
  return :data_mapper if is_data_mapper? name
52
56
 
53
- return :couch_db if orm_name == :couch_db
54
- return :mongoid if orm_name == :mongoid
57
+ return :couch_db if name == :couch_db
58
+ return :mongoid if name == :mongoid
55
59
 
56
60
  raise ArgumentError, "ERROR: Cream does not currently support the orm: #{name}"
57
61
  end
@@ -18,6 +18,34 @@ module Cream
18
18
  @strategies << document_store_strategies if document_store?
19
19
  @strategies
20
20
  end
21
+
22
+ def role_ref_strategy?
23
+ role_ref_strategies.include? strategy
24
+ end
25
+
26
+ def embed_strategy?
27
+ role_embed_strategies.include? strategy
28
+ end
29
+
30
+ def document_store_strategy?
31
+ document_store_strategies.include? strategy
32
+ end
33
+
34
+ def inline_strategy?
35
+ document_store_strategies.include? strategy
36
+ end
37
+
38
+ def role_ref_strategies
39
+ [:one_role, :many_roles]
40
+ end
41
+
42
+ def role_embed_strategies
43
+ [:embed_one_role, :embed_many_roles]
44
+ end
45
+
46
+ def inline_strategies
47
+ valid_strategies - role_ref_strategies - role_embed_strategies
48
+ end
21
49
 
22
50
  def basic_strategies
23
51
  [:admin_flag, :role_string, :one_role, :many_roles, :roles_mask, :roles_string]
@@ -24,11 +24,10 @@ module Cream
24
24
  require 'tmpdir'
25
25
  html_root = "#{self.class.source_root}/cream"
26
26
 
27
- Dir.mktmpdir("cream-haml.") do |haml_root|
28
- Dir["#{html_root}/**/*"].each do |path|
29
- relative_path = path.sub(html_root, "")
27
+ Dir.mktmpdir("cream-haml.") do |haml_root|
28
+ Dir["#{html_root}/**/*"].each do |path|
29
+ relative_path = path.sub(html_root, "")
30
30
  source_path = (haml_root + relative_path).sub(/erb$/, "haml")
31
-
32
31
  if File.directory?(path)
33
32
  FileUtils.mkdir_p(source_path)
34
33
  else
@@ -1,19 +1,19 @@
1
1
  require 'sugar-high/path'
2
- require 'generators/views/haml_util'
2
+ require 'generators/cream/views/haml_util'
3
3
 
4
4
  module Cream
5
5
  module Generators
6
- class ViewsGenerator < Rails::Generators::Base
6
+ class ViewsGenerator < ::Rails::Generators::Base
7
7
  desc "Copies all Cream views to your application."
8
-
9
- argument :scope, :required => false, :default => nil, :desc => "The scope to copy views to"
10
-
11
- class_option :haml, :type => :boolean, :default => "erb", :desc => "Use HAML"
12
-
8
+
9
+ argument :scope, :required => false, :default => nil, :desc => "The scope to copy views to"
10
+
11
+ class_option :haml, :type => :boolean, :default => "erb", :desc => "Use HAML"
12
+
13
13
  def self.source_root
14
- @_devise_source_root ||= File.expand_path("app/views".up(4), __FILE__)
14
+ @_devise_source_root ||= File.expand_path("app/views".path.up(5), __FILE__)
15
15
  end
16
-
16
+
17
17
  def copy_views
18
18
  if options[:haml]
19
19
  create_and_copy_haml_views
@@ -21,13 +21,13 @@ module Cream
21
21
  copy_erb_views
22
22
  end
23
23
  end
24
-
24
+
25
25
  protected
26
-
26
+
27
27
  def copy_erb_views
28
- directory "cream", "app/views/#{scope || 'devise'}"
28
+ directory "cream", "app/views/#{scope || 'cream'}"
29
29
  end
30
-
30
+
31
31
  include Cream::Generators::HamlUtil
32
32
  end
33
33
  end
@@ -29,7 +29,7 @@ module DeviseConfigGenerator
29
29
  return
30
30
  end
31
31
 
32
- File.replace_content_from devise_initializer, :where => orm_statement, :with => orm_replacement
32
+ replace_initializer_content :devise, :where => orm_statement, :with => orm_replacement
33
33
  end
34
34
 
35
35
  def mailer_configure!
@@ -35,9 +35,7 @@ module Devise
35
35
  include Rails3::Assist::BasicLogger
36
36
  extend Rails3::Assist::UseMacro
37
37
 
38
- include Cream::GeneratorHelper::Orm
39
- include Cream::GeneratorHelper::Executor
40
- include Cream::GeneratorHelper::Args
38
+ include Cream::GeneratorHelper
41
39
 
42
40
  use_helpers :controller, :app, :special, :file
43
41
 
@@ -36,28 +36,5 @@ module DeviseConfigGenerator
36
36
  add_gem 'devise_couch'
37
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
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
39
  end
63
40
  end
@@ -0,0 +1,49 @@
1
+ require 'sugar-high/file'
2
+ require 'sugar-high/module'
3
+ require 'cream'
4
+ require 'rails3_artifactor'
5
+ require 'logging_assist'
6
+ require 'generators/cream/helpers/all'
7
+
8
+ require_all File.dirname(__FILE__) # + '/helpers'
9
+
10
+ module Devise
11
+ module Generators
12
+ class CustomizeGenerator < ::Rails::Generators::Base
13
+ desc "Customize Devise"
14
+
15
+ argument :user_class, :type => :string, :default => 'User', :desc => "User class name"
16
+
17
+ # ORM to use
18
+ class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
19
+ class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
20
+
21
+ class_option :user_name, :type => :boolean, :default => true, :desc => "Add username as login option"
22
+ class_option :login_type, :type => :string, :default => 'generic', :desc => "How to login: 'email', 'username', 'generic' (i.e 'username' or 'email')"
23
+
24
+ def main_flow
25
+ logger.add_logfile :logfile => logfile if logfile
26
+ logger.debug 'customizing devise...'
27
+
28
+ Devise::Customizers::UserName.new(orm, user_class, login_attribute).add_to_user_class if add_user_name?
29
+ end
30
+
31
+ protected
32
+
33
+ include Cream::GeneratorHelper
34
+ include Rails3::Assist::BasicLogger
35
+
36
+ def login_attribute
37
+ att = options[:login_attribute] || 'generic'
38
+ return 'login' if att.to_sym == :generic
39
+ return att if [:username, :email].include? att.to_sym
40
+ raise ArgumentError, "Unknown login attribute strategy #{att}"
41
+ end
42
+
43
+ def add_user_name?
44
+ options[:user_name] && is_default_devise_orm?
45
+ end
46
+ end
47
+ end
48
+ end
49
+
@@ -0,0 +1,52 @@
1
+ require "colorize"
2
+
3
+ module Devise
4
+ module CustomizeMessage
5
+ class << self
6
+ def say msg, options = nil
7
+ options = case options
8
+ when Symbol
9
+ {:color => options}
10
+ when Hash
11
+ option
12
+ end
13
+ puts msg.colorize(options) if options
14
+ puts msg if !options
15
+ end
16
+
17
+ def retrieve_password
18
+ say %q{Currently Cream only supports a username/password retrieval strategy for :active_record and :mongoid
19
+ Please help add a strategy for your ORM of choice by adding a FindRecord.#[orm] method to the Cream 'app_generator.rb' file. Thanks!
20
+ See: https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign_in-using-their-username-or-email-address for how to do it!}, :yellow
21
+ end
22
+
23
+ def find_record
24
+ say %Q{Currently Cream only supports generic login strategy for :active_record and :mongoid.
25
+ Please help add a strategy for your ORM of choice by adding a FindRecord#[name of your orm] method to the Cream 'app_generator.rb' file.
26
+
27
+ How: Add a self#[orm]_find_record method in your User class, make it work, then submit it as a patch to Cream. It should be very simple :)
28
+
29
+ The example for mongoid:
30
+ ...
31
+ module FindRecord
32
+ def self.mongoid
33
+ #{FindRecord.mongoid}
34
+ end
35
+ end
36
+
37
+ See: https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign_in-using-their-username-or-email-address for how to do it!}, :yellow
38
+ end
39
+
40
+ def locales_update
41
+ say %q{
42
+ Modify config/locales/en.yml to contain something like:
43
+
44
+ activemodel:
45
+ attributes:
46
+ user:
47
+ login: "Username or email"
48
+ }, :green
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,43 @@
1
+ module Devise
2
+ module QueryCustomizers
3
+ module FindRecord
4
+ class << self
5
+ def active_record
6
+ %q{where(attributes).where(["username = :value OR email = :value", { :value => login }]).first}
7
+ end
8
+
9
+ def mongoid
10
+ %q{where("function() {return this.username == '#{login}' || this.email == '#{login}'}").first}
11
+ end
12
+
13
+ def mongo_mapper
14
+ %q{where("function() {return this.username == '#{login}' || this.email == '#{login}'}").first}
15
+ end
16
+ end
17
+ end
18
+
19
+ module UserAuth
20
+ class << self
21
+ def active_record
22
+ %q{
23
+ # protected
24
+
25
+ def self.find_for_database_authentication(conditions)
26
+ login = conditions.delete(:login)
27
+ where(conditions).where(["username = :value OR email = :value", { :value => login }]).first
28
+ end
29
+ }
30
+ end
31
+
32
+ def mongoid
33
+ %q{
34
+ def self.find_for_database_authentication(conditions)
35
+ login = conditions.delete(:login)
36
+ self.any_of({ :username => value }, { :email => login }).first
37
+ end
38
+ }
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,80 @@
1
+ require 'generators/cream/helpers/all'
2
+
3
+ module Devise
4
+ module Customizers
5
+ class RecoverLogin
6
+ include Cream::GeneratorHelper
7
+ extend Rails3::Assist::UseMacro
8
+ use_helpers :app, :special, :file, :model
9
+ # include Devise::UserCustomization
10
+
11
+ attr_accessor :orm
12
+
13
+ def initialize orm
14
+ @orm = orm
15
+ end
16
+
17
+ def retrieve_password
18
+ reset_password_instructions << find_recoverable_code
19
+ end
20
+
21
+ # should not be ORM dependent
22
+ def reset_password_instructions
23
+ %q{
24
+ # Attempt to find a user by it's email. If a record is found, send new
25
+ # password instructions to it. If not user is found, returns a new user
26
+ # with an email not found error.
27
+ def self.send_reset_password_instructions(attributes={})
28
+ recoverable = find_recoverable_or_initialize_with_errors(reset_password_keys, attributes, :not_found)
29
+ recoverable.send_reset_password_instructions if recoverable.persisted?
30
+ recoverable
31
+ end
32
+ }
33
+ end
34
+
35
+ def find_recoverable_code
36
+ %Q{
37
+ def self.find_recoverable_or_initialize_with_errors(required_attributes, attributes, error=:invalid)
38
+ case_insensitive_keys.each { |k| attributes[k].try(:downcase!) }
39
+
40
+ attributes = attributes.slice(*required_attributes)
41
+ attributes.delete_if { |key, value| value.blank? }
42
+
43
+ if attributes.size == required_attributes.size
44
+ if attributes.has_key?(:login)
45
+ login = attributes.delete(:login)
46
+ record = find_record(login)
47
+ else
48
+ record = where(attributes).first
49
+ end
50
+ end
51
+ user_record_not_found(required_attributes) unless record
52
+ record
53
+ end
54
+
55
+ #{user_record_not_found}
56
+
57
+ def self.find_record login
58
+ #{Devise::QueryCustomizers::FindRecord.send orm}
59
+ end
60
+ }
61
+ end
62
+
63
+ def user_record_not_found
64
+ %q{
65
+ # handle when the user record is not found
66
+ # adds error messages to the record
67
+ def self.user_record_not_found(required_attributes)
68
+ record = new
69
+
70
+ required_attributes.each do |key|
71
+ value = attributes[key]
72
+ record.send("#{key}=", value)
73
+ record.errors.add(key, value.present? ? error : :blank)
74
+ end
75
+ end
76
+ }
77
+ end
78
+ end
79
+ end
80
+ end