cream 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. data/.document +5 -0
  2. data/.gitignore +39 -0
  3. data/.rspec +1 -0
  4. data/Changelog.txt +8 -0
  5. data/Gemfile +27 -0
  6. data/LICENSE +20 -0
  7. data/README.markdown +196 -0
  8. data/Rakefile +40 -0
  9. data/VERSION +1 -0
  10. data/app/views/auth_assist/menu/_admin_login_items.html.erb +11 -0
  11. data/app/views/auth_assist/menu/_login_items.html.erb +11 -0
  12. data/app/views/auth_assist/menu/_registration_items.html.erb +10 -0
  13. data/config/locales/en.yml +14 -0
  14. data/cream.gemspec +169 -0
  15. data/features/FEATURE_NOTES.txt +6 -0
  16. data/features/permission/adds_permission.feature +0 -0
  17. data/features/role_strategy/adds_role_strategy.feature +0 -0
  18. data/features/role_strategy/clears_role_strategy.feature +0 -0
  19. data/init.rb +1 -0
  20. data/lib/cream.rb +21 -0
  21. data/lib/cream/configure.rb +3 -0
  22. data/lib/cream/configure/after_init/role_config.rb +29 -0
  23. data/lib/cream/configure/rails.rb +23 -0
  24. data/lib/cream/controller/ability.rb +7 -0
  25. data/lib/cream/helper/authlabels.rb +21 -0
  26. data/lib/cream/helper/host.rb +11 -0
  27. data/lib/cream/helper/role.rb +48 -0
  28. data/lib/cream/namespaces.rb +5 -0
  29. data/lib/cream/role.rb +7 -0
  30. data/lib/cream/view/host_area.rb +12 -0
  31. data/lib/cream/view/role_area.rb +38 -0
  32. data/lib/cream/view/user_action_menu.rb +21 -0
  33. data/lib/generators/cream/config/DESIGN NOTES.markdown +61 -0
  34. data/lib/generators/cream/config/config_generator.rb +72 -0
  35. data/lib/generators/cream/config/modules/cancan_config.rb +22 -0
  36. data/lib/generators/cream/config/modules/cream_config.rb +23 -0
  37. data/lib/generators/cream/config/modules/devise_config.rb +108 -0
  38. data/lib/generators/cream/config/modules/helper.rb +57 -0
  39. data/lib/generators/cream/config/modules/permits_config.rb +15 -0
  40. data/lib/generators/cream/config/modules/roles_config.rb +15 -0
  41. data/lib/generators/cream/views/haml_util.rb +44 -0
  42. data/lib/generators/cream/views/views_generator.rb +34 -0
  43. data/lib/generators/cream_refactor.rb +82 -0
  44. data/log/development.log +0 -0
  45. data/sandbox/test.rb +40 -0
  46. data/spec/cream/configure/rails_spec.rb +51 -0
  47. data/spec/cream/helper/host_spec.rb +68 -0
  48. data/spec/cream/helper/role_spec.rb +187 -0
  49. data/spec/cream/view/host_area_spec.rb +61 -0
  50. data/spec/cream/view/role_area_spec.rb +124 -0
  51. data/spec/cream/view/role_ext_spec.rb +36 -0
  52. data/spec/generator_spec_helper.rb +26 -0
  53. data/spec/generators/cream/config/devise/existing_devise_users.rb +61 -0
  54. data/spec/generators/cream/config/empty_app/default_args_spec.rb +51 -0
  55. data/spec/generators/cream/config/permits/existing_permits_spec.rb +0 -0
  56. data/spec/generators/cream/config/permits/no_permits_spec.rb +0 -0
  57. data/spec/generators/cream/config/roles/default_roles.rb +51 -0
  58. data/spec/generators/cream/config/roles/roles_spec.rb +60 -0
  59. data/spec/generators/cream/shared_examples.rb +18 -0
  60. data/spec/generators/cream/views_generator_spec.rb +30 -0
  61. data/spec/spec_helper.rb +18 -0
  62. data/wiki/CONFIG_GENERATOR.txt +21 -0
  63. data/wiki/DESIGN.txt +21 -0
  64. data/wiki/INSTALLATION.txt +6 -0
  65. data/wiki/PERMITS.txt +32 -0
  66. data/wiki/ROLE_STRATEGIES.txt +40 -0
  67. data/wiki/SPEC_NOTES.txt +6 -0
  68. data/wiki/VIEWS_GENERATOR.txt +35 -0
  69. data/wiki/VIEW_HELPERS.txt +162 -0
  70. metadata +374 -0
@@ -0,0 +1,6 @@
1
+ # Notes and Status
2
+
3
+ Currently these features are a work in progress.
4
+ I am first building up a skeleton structure for the features and then plan to gradually implement them as described in the DESIGN.txt document :)
5
+
6
+ You are most welcome to help in the effort!
File without changes
File without changes
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'cream'
data/lib/cream.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'active_support/inflector'
2
+ require 'require_all'
3
+
4
+ require 'rails'
5
+ # require 'devise'
6
+ require 'devise-links'
7
+
8
+ require 'cancan'
9
+ require 'cancan-rest-links'
10
+ require 'cancan-permits'
11
+
12
+ require 'cream/namespaces'
13
+
14
+ require 'sugar-high/alias'
15
+ require 'sugar-high/kind_of'
16
+ require 'sugar-high/array'
17
+
18
+ require_all File.dirname(__FILE__) + '/cream/controller'
19
+ require_all File.dirname(__FILE__) + '/cream/helper'
20
+ require_all File.dirname(__FILE__) + '/cream/view'
21
+
@@ -0,0 +1,3 @@
1
+ require 'require_all'
2
+ require 'cream/namespaces'
3
+ require_all File.dirname(__FILE__) + '/configure'
@@ -0,0 +1,29 @@
1
+ module Cream::View
2
+ module Role
3
+ # admin?, guest? ...
4
+ Cream::Role.available.each do |role|
5
+ class_eval %{
6
+ def #{role}_area &block
7
+ area_for_roles(#{role}, &block)
8
+ end
9
+
10
+ def for_#{role}(&block)
11
+ for_roles(#{role}, &block)
12
+ end
13
+ }
14
+ end
15
+ end
16
+ end
17
+
18
+ module Cream::Helper
19
+ module Role
20
+ # admin?, guest? ...
21
+ AuthAssistant::Role.available.each do |role|
22
+ class_eval %{
23
+ def #{role}?
24
+ has_role? :#{role}
25
+ end
26
+ }
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ require 'active_support/railtie'
2
+ require 'r3_plugin_toolbox'
3
+
4
+ Rails3::Plugin::Extender.new do
5
+ # extend action_controller with methods from some modules
6
+ # extend_rails(:controller) do
7
+ # extend_from_module Cream, :role
8
+ # extend_from_module Cream::Controller, :ability
9
+ # extend_from_module Cream::Helper, :role, :local_host, :auth_label
10
+ # extend_from_module Cream::Link, :session, :registration, :rest, :session
11
+ # end
12
+
13
+ # extend action_view with methods from some modules
14
+ extend_rails :view do
15
+ extend_from_module Cream::View, :role, :user_action_menu
16
+ extend_from_module Cream::Helper, :role
17
+ end
18
+
19
+ after :initialize do
20
+ puts "Rails app initialized"
21
+ require_all File.dirname(__FILE__) + '/after_init'
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ module Cream::Controller
2
+ module Ability
3
+ def current_ability
4
+ @current_ability ||= Ability.new(current_user, request)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ module Cream::Helper
2
+ module AuthLabel
3
+ def auth_labels
4
+ @auth_labels ||= translate_labels
5
+ end
6
+
7
+ def translate_labels
8
+ ns_actions = 'cream.actions'
9
+ labels = {}
10
+ %w{new edit delete show sign_in sign_out sign_up edit_registration}.each do |action|
11
+ labels[action.to_sym] = t "#{ns_actions}.#{action}"
12
+ end
13
+ labels[:confirm] = t 'cream.confirm'
14
+ labels
15
+ end
16
+
17
+ def self.included(base)
18
+ base.helper_method :auth_labels
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ module Cream::Helper
2
+ module Host
3
+ def localhost?
4
+ ['localhost', '127.0.0.1'].include?(request.host)
5
+ end
6
+
7
+ def publichost?
8
+ !localhost?
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,48 @@
1
+ module Cream::Helper
2
+ module Role
3
+ # does the user have ANY of the given roles?
4
+ # Uses generic roles API
5
+ def has_role?(*roles)
6
+ current_user.has_role? roles.flatten
7
+ end
8
+
9
+ # does the user have ALL of the given roles?
10
+ # Uses generic roles API
11
+ def has_roles?(*roles)
12
+ current_user.has_roles? roles.flatten
13
+ end
14
+
15
+ # returns true if the current user owns the object
16
+ # tries default 'owner' relations if none given as an argument
17
+ def owner?(obj, relation=nil)
18
+ if relation
19
+ return true if user_relation?(obj, relation)
20
+ end
21
+ [:user, :owner, :author].each do |relation|
22
+ return true if user_relation?(obj, relation)
23
+ end
24
+ false
25
+ end
26
+
27
+ # execute block if user DOES have any of the given roles
28
+ def for_roles(*user_roles, &block)
29
+ user_roles = user_roles.flatten
30
+ yield if has_role?(user_roles) && block
31
+ end
32
+ alias_method :for_role, :for_roles
33
+
34
+ # execute block if user DOES NOT have any of the given roles
35
+ def not_for_roles(*user_roles, &block)
36
+ user_roles = user_roles.flatten
37
+ yield if !has_role?(user_roles) && block
38
+ end
39
+ alias_method :not_for_role, :not_for_roles
40
+
41
+ protected
42
+
43
+ def user_relation? obj, relation
44
+ 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
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ require 'sugar-high/module'
2
+
3
+ module Cream
4
+ modules :view, :controller, :model, :link, :helper
5
+ end
data/lib/cream/role.rb ADDED
@@ -0,0 +1,7 @@
1
+ module AuthAssistant
2
+ module Role
3
+ def self.available
4
+ ::Role.all.map(&:name).to_symbols
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module Cream::View
2
+ module Host
3
+ def for_localhost(&block)
4
+ yield block if localhost?
5
+ end
6
+
7
+ def for_public(&block)
8
+ # with_output_buffer(&block) if publichost?
9
+ yield block if publichost?
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,38 @@
1
+ require 'sugar-high/arguments'
2
+
3
+ module Cream::View
4
+ module Role
5
+ # for users WITH the given roles create a div area
6
+ # with optional class given in options hash and render block
7
+ # within this div
8
+ def area_for_roles(*user_roles, &block)
9
+ options = last_option(user_roles)
10
+
11
+ return area(&block) if user_roles.empty?
12
+
13
+ not_for_roles(user_roles, &block) if user_roles.first == false
14
+
15
+ for_roles user_roles do
16
+ area(options, &block)
17
+ end
18
+ end
19
+ alias_method :area_for_role, :area_for_roles
20
+
21
+ # for users WITHOUT the given roles create a div area
22
+ # with optional class given in options hash and render block
23
+ # within this div
24
+ def area_not_for_roles(*user_roles, &block)
25
+ options = last_option(user_roles)
26
+ not_for_roles user_roles do
27
+ clazz = options[:class] || 'special'
28
+ area(clazz, &block)
29
+ end
30
+ end
31
+ alias_method :area_not_for_role, :area_not_for_roles
32
+
33
+ def area(options=nil, &block)
34
+ content = yield #with_output_buffer(&block)
35
+ content_tag :div, content, options
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,21 @@
1
+ module Cream::View
2
+ module UserActionMenu
3
+
4
+ MENU_ITEMS = {
5
+ :sign_out => :logout,
6
+ :sign_in => :login,
7
+ :sign_up => :register,
8
+ :edit_registration => [:edit_user, :edit_account]
9
+ }
10
+
11
+ MENU_ITEMS.keys.each do |name|
12
+ class_eval %{
13
+ def #{name}_menu_item tag = 'li'
14
+ content_tag tag, #{name}_link
15
+ end
16
+ }
17
+ end
18
+
19
+ multi_alias :menu_item, MENU_ITEMS
20
+ end
21
+ end
@@ -0,0 +1,61 @@
1
+ # Config generator
2
+
3
+ Each of the following should be a module in its own right!
4
+
5
+ 1. Create fresh Rails 3 app and set it up so that all the sub-systems work
6
+ 2. Develop the generator so that it can mutate a Rails 3 app that matches this example app
7
+
8
+ ## Code Design
9
+
10
+ * Create a module for each sub-system to configure
11
+ * Include modules depending on options
12
+ * For each module call the config_[module_name] method, which is responsible for configuring that module!
13
+
14
+ ## Devise Configuration
15
+
16
+ Gemfile
17
+ gem 'devise'
18
+ gem ORM 'devise'
19
+
20
+ if app does NOT have a User model
21
+ run devise generator to create User model
22
+ else
23
+ if User model is NOT configured with devise strategy
24
+ insert default devise User strategy
25
+ end
26
+ end
27
+
28
+ if --admin option set
29
+ if app does NOT have a Admin model
30
+ run devise generator to create Admin model
31
+ remove any current inheritance
32
+ and make Admin model inherit from User model
33
+ else
34
+ insert default devise Admin strategy
35
+ end
36
+ end
37
+
38
+ ## Cream configuration
39
+
40
+ copy locales and views (optional)
41
+
42
+ ## CanCan Configuration
43
+
44
+ Gemfile:
45
+ gem 'cancan'
46
+ gem 'cancan-rest-links'
47
+
48
+ CanCan access denied exception handling
49
+
50
+ ## Permits Configuration
51
+
52
+ Gemfile:
53
+ gem 'cancan-permits'
54
+
55
+ Run permits generator to generate permit for each role
56
+
57
+ ## Roles Configuration
58
+
59
+ gem ORM 'roles'
60
+
61
+ run roles generator for ORM chosen
@@ -0,0 +1,72 @@
1
+ require 'sugar-high/file'
2
+ require 'sugar-high/modules'
3
+ require 'cream'
4
+ require 'rails3_artifactor'
5
+ require 'logging_assist'
6
+
7
+ module Cream
8
+ module Generators
9
+ end
10
+ end
11
+
12
+ require_all File.dirname(__FILE__) + '/modules'
13
+
14
+ module Cream::Generators
15
+ class ConfigGenerator < Rails::Generators::Base
16
+ extend Rails3::Assist::UseMacro
17
+
18
+ desc "Configures Devise and Users"
19
+
20
+ source_root File.dirname(__FILE__)
21
+
22
+ # Role Strategy
23
+ class_option :strategy, :type => :string, :default => 'role_string', :desc => "Role strategy to use",
24
+
25
+ # Create Admin user
26
+ class_option :admin_user, :type => :boolean, :default => false, :desc => "Create admin user",
27
+
28
+ # Roles
29
+ class_option :default_roles, :type => :boolean, :default => true, :desc => "Create default roles :admin and :guest"
30
+ class_option :roles, :type => :array, :default => [], :desc => "Roles to create"
31
+
32
+ # Permits
33
+ class_option :permits, :type => :boolean, :default => true, :desc => "Configure permits or not"
34
+
35
+ # ORM to use
36
+ class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
37
+
38
+ class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
39
+
40
+ def main_flow
41
+ configure_logger
42
+ configure_gems
43
+
44
+ MODULES.each do |name|
45
+ send :"configure_#{name}"
46
+ end
47
+ end
48
+
49
+ # -----------------
50
+ protected
51
+
52
+ MODULES = [:devise] #, :cancan, :roles, :permits, :cream]
53
+
54
+ includes Cream::Generators::Config, :helper, MODULES #, :cancan, :roles, :permits, :cream
55
+
56
+ include Rails3::Assist::BasicLogger
57
+
58
+ use_helpers :model, :controller, :permit
59
+
60
+ def configure_logger
61
+ logger.add_logfile :logfile => logfile
62
+ logger.debug 'main flow'
63
+ end
64
+
65
+ def configure_gems
66
+ MODULES.each do |name|
67
+ send :"#{name}_gems"
68
+ end
69
+ run "bundle install"
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,22 @@
1
+ module Cream::Generators
2
+ module Config
3
+ module CanCan
4
+ def configure_devise_gems
5
+ gem 'cancan'
6
+ gem 'cancan-rest-links'
7
+ end
8
+
9
+ # CanCan access denied exception handling
10
+ def configure_exception_handling
11
+ insert_into_controller :application, :after => "ActionController::Base\n" do
12
+ %{
13
+ rescue_from CanCan::AccessDenied do |exception|
14
+ flash[:error] = exception.message
15
+ redirect_to root_url
16
+ end
17
+ }
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end