cream 0.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +39 -0
- data/.rspec +1 -0
- data/Changelog.txt +8 -0
- data/Gemfile +27 -0
- data/LICENSE +20 -0
- data/README.markdown +196 -0
- data/Rakefile +40 -0
- data/VERSION +1 -0
- data/app/views/auth_assist/menu/_admin_login_items.html.erb +11 -0
- data/app/views/auth_assist/menu/_login_items.html.erb +11 -0
- data/app/views/auth_assist/menu/_registration_items.html.erb +10 -0
- data/config/locales/en.yml +14 -0
- data/cream.gemspec +169 -0
- data/features/FEATURE_NOTES.txt +6 -0
- data/features/permission/adds_permission.feature +0 -0
- data/features/role_strategy/adds_role_strategy.feature +0 -0
- data/features/role_strategy/clears_role_strategy.feature +0 -0
- data/init.rb +1 -0
- data/lib/cream.rb +21 -0
- data/lib/cream/configure.rb +3 -0
- data/lib/cream/configure/after_init/role_config.rb +29 -0
- data/lib/cream/configure/rails.rb +23 -0
- data/lib/cream/controller/ability.rb +7 -0
- data/lib/cream/helper/authlabels.rb +21 -0
- data/lib/cream/helper/host.rb +11 -0
- data/lib/cream/helper/role.rb +48 -0
- data/lib/cream/namespaces.rb +5 -0
- data/lib/cream/role.rb +7 -0
- data/lib/cream/view/host_area.rb +12 -0
- data/lib/cream/view/role_area.rb +38 -0
- data/lib/cream/view/user_action_menu.rb +21 -0
- data/lib/generators/cream/config/DESIGN NOTES.markdown +61 -0
- data/lib/generators/cream/config/config_generator.rb +72 -0
- data/lib/generators/cream/config/modules/cancan_config.rb +22 -0
- data/lib/generators/cream/config/modules/cream_config.rb +23 -0
- data/lib/generators/cream/config/modules/devise_config.rb +108 -0
- data/lib/generators/cream/config/modules/helper.rb +57 -0
- data/lib/generators/cream/config/modules/permits_config.rb +15 -0
- data/lib/generators/cream/config/modules/roles_config.rb +15 -0
- data/lib/generators/cream/views/haml_util.rb +44 -0
- data/lib/generators/cream/views/views_generator.rb +34 -0
- data/lib/generators/cream_refactor.rb +82 -0
- data/log/development.log +0 -0
- data/sandbox/test.rb +40 -0
- data/spec/cream/configure/rails_spec.rb +51 -0
- data/spec/cream/helper/host_spec.rb +68 -0
- data/spec/cream/helper/role_spec.rb +187 -0
- data/spec/cream/view/host_area_spec.rb +61 -0
- data/spec/cream/view/role_area_spec.rb +124 -0
- data/spec/cream/view/role_ext_spec.rb +36 -0
- data/spec/generator_spec_helper.rb +26 -0
- data/spec/generators/cream/config/devise/existing_devise_users.rb +61 -0
- data/spec/generators/cream/config/empty_app/default_args_spec.rb +51 -0
- data/spec/generators/cream/config/permits/existing_permits_spec.rb +0 -0
- data/spec/generators/cream/config/permits/no_permits_spec.rb +0 -0
- data/spec/generators/cream/config/roles/default_roles.rb +51 -0
- data/spec/generators/cream/config/roles/roles_spec.rb +60 -0
- data/spec/generators/cream/shared_examples.rb +18 -0
- data/spec/generators/cream/views_generator_spec.rb +30 -0
- data/spec/spec_helper.rb +18 -0
- data/wiki/CONFIG_GENERATOR.txt +21 -0
- data/wiki/DESIGN.txt +21 -0
- data/wiki/INSTALLATION.txt +6 -0
- data/wiki/PERMITS.txt +32 -0
- data/wiki/ROLE_STRATEGIES.txt +40 -0
- data/wiki/SPEC_NOTES.txt +6 -0
- data/wiki/VIEWS_GENERATOR.txt +35 -0
- data/wiki/VIEW_HELPERS.txt +162 -0
- metadata +374 -0
@@ -0,0 +1,23 @@
|
|
1
|
+
module Cream::Generators
|
2
|
+
module Config
|
3
|
+
module Cream
|
4
|
+
def cream_gems
|
5
|
+
gem_name = ORM_MAP[orm]
|
6
|
+
gem gem_name if gem_name
|
7
|
+
|
8
|
+
gem 'devise'
|
9
|
+
gem 'cancan'
|
10
|
+
gem 'cream'
|
11
|
+
run "bundle install"
|
12
|
+
end
|
13
|
+
|
14
|
+
def configure_locale
|
15
|
+
src = File.expand_path "config/locales/en.yml".path.up(2)
|
16
|
+
# src = "config/locales/en.yml"
|
17
|
+
logger.debug "configure_locale, copy from: #{src}"
|
18
|
+
copy_file src, "config/locales/cream.en.yml"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,108 @@
|
|
1
|
+
module Cream::Generators
|
2
|
+
module Config
|
3
|
+
module Devise
|
4
|
+
|
5
|
+
def configure_devise
|
6
|
+
if !has_model? :user
|
7
|
+
# run devise generator to create User model
|
8
|
+
create_devise_model :user
|
9
|
+
end
|
10
|
+
|
11
|
+
# if User model is NOT configured with devise strategy
|
12
|
+
if !has_devise_strategy? :user
|
13
|
+
insert_devise_strategy :user, :defaults
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
configure_admin_user if configure_admin_user?
|
18
|
+
end
|
19
|
+
|
20
|
+
def configure_admin_user
|
21
|
+
# if app does NOT have a Admin model
|
22
|
+
if !has_model? :admin
|
23
|
+
create_admin_user
|
24
|
+
end
|
25
|
+
if has_model? :admin
|
26
|
+
# insert default devise Admin strategy
|
27
|
+
insert_devise_strategy :user, :defaults
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def devise_gems
|
32
|
+
gem 'devise'
|
33
|
+
|
34
|
+
# Devise ORM integration
|
35
|
+
case orm.to_sym
|
36
|
+
when :mongoid
|
37
|
+
say "Please configure Devise for Mongoid as similar to Rails 3 example app: http://github.com/fortuity/rails3-mongoid-devise"
|
38
|
+
when :mongo_mapper
|
39
|
+
gem 'mm-devise'
|
40
|
+
when :data_mapper
|
41
|
+
gem 'dm-devise'
|
42
|
+
when :couch_db
|
43
|
+
gem 'devise_couch'
|
44
|
+
say "Please note that Couch DB does not currently have a Roles implementation. Feel free to provide one."
|
45
|
+
say "Look at Roles DataMapper (roles_data_mapper) for an example ;)"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def init_devise
|
50
|
+
execute 'devise_install'
|
51
|
+
end
|
52
|
+
|
53
|
+
def devise_users?
|
54
|
+
has_user?(:user) && has_user?(:admin)
|
55
|
+
end
|
56
|
+
|
57
|
+
def handle_devise_users
|
58
|
+
return notify_create_users if !init_devise?
|
59
|
+
create_users
|
60
|
+
end
|
61
|
+
|
62
|
+
def notify_create_users
|
63
|
+
logger.debug 'notify_create_users'
|
64
|
+
say "You must first run devise generators:"
|
65
|
+
say "rails g devise User"
|
66
|
+
say "rails g devise Admin" if admin_user?
|
67
|
+
end
|
68
|
+
|
69
|
+
def create_admin_user
|
70
|
+
logger.debug 'create_admin_user'
|
71
|
+
create_model :admin
|
72
|
+
# remove any current inheritance
|
73
|
+
remove_inheritance :admin
|
74
|
+
# and make Admin model inherit from User model
|
75
|
+
inherit_model :user => :admin
|
76
|
+
end
|
77
|
+
|
78
|
+
def remove_inheritance user
|
79
|
+
File.remove_from model_file user, :content => /<\s*ActiveRecord::Base/
|
80
|
+
end
|
81
|
+
|
82
|
+
def inherit_model hash
|
83
|
+
subclass = hash.keys.first
|
84
|
+
superclass = hash.values.first.to_s.camelize
|
85
|
+
File.replace_content_from model_file subclass, :where => /class Admin/, :with => "class Admin < #{superclass}"
|
86
|
+
end
|
87
|
+
|
88
|
+
# Must be ORM specific!
|
89
|
+
def create_devise_model user
|
90
|
+
execute "rails g devise #{user}"
|
91
|
+
end
|
92
|
+
|
93
|
+
def create_user user
|
94
|
+
execute "rails g model #{user}"
|
95
|
+
end
|
96
|
+
|
97
|
+
def has_user? user
|
98
|
+
return true if user == :admin && !admin_user?
|
99
|
+
begin
|
100
|
+
read_model(user) =~ /devise/
|
101
|
+
rescue Exception => e
|
102
|
+
logger.info "Exception for has_user? #{user}: #{e.message}"
|
103
|
+
false
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Cream::Generators
|
2
|
+
module Config
|
3
|
+
module Helpers
|
4
|
+
|
5
|
+
ORM_MAP = {
|
6
|
+
:data_mapper => 'dm-devise',
|
7
|
+
:mongo_mapper => 'mm-devise',
|
8
|
+
:mongoid => 'rails3-mongoid-devise'
|
9
|
+
}
|
10
|
+
|
11
|
+
def execute command
|
12
|
+
logger.debug command
|
13
|
+
run command
|
14
|
+
end
|
15
|
+
|
16
|
+
def strategy
|
17
|
+
options[:strategy]
|
18
|
+
end
|
19
|
+
|
20
|
+
def logfile
|
21
|
+
options[:logfile]
|
22
|
+
end
|
23
|
+
|
24
|
+
def default_roles
|
25
|
+
options[:default_roles] ? '--default-roles' : '--no-default-roles'
|
26
|
+
end
|
27
|
+
|
28
|
+
def roles
|
29
|
+
options[:roles].join(' ')
|
30
|
+
end
|
31
|
+
|
32
|
+
def orm
|
33
|
+
options[:orm]
|
34
|
+
end
|
35
|
+
|
36
|
+
def roles_config?
|
37
|
+
options[:roles_config]
|
38
|
+
end
|
39
|
+
|
40
|
+
def permission_config?
|
41
|
+
options[:permission_config]
|
42
|
+
end
|
43
|
+
|
44
|
+
def admin_user?
|
45
|
+
options[:admin]
|
46
|
+
end
|
47
|
+
|
48
|
+
def here
|
49
|
+
File.dirname(__FILE__)
|
50
|
+
end
|
51
|
+
|
52
|
+
def root_dir
|
53
|
+
File.dirname(__FILE__).up(4)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Cream::Generators
|
2
|
+
module Config
|
3
|
+
module Permits
|
4
|
+
def permits_gems
|
5
|
+
gem 'cancan-permits'
|
6
|
+
end
|
7
|
+
|
8
|
+
def configure_permits
|
9
|
+
logger.debug "Configure Permits"
|
10
|
+
# Run permits generator to generate permit for each role
|
11
|
+
execute "rails g permits --roles #{roles}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Cream::Generators
|
2
|
+
module Config
|
3
|
+
module Roles
|
4
|
+
def configure_roles_gems
|
5
|
+
gem "roles_#{orm}"
|
6
|
+
end
|
7
|
+
|
8
|
+
def configure_roles
|
9
|
+
command = "rails g #{orm}:roles --strategy #{strategy} --roles #{roles} #{default_roles}"
|
10
|
+
logger.debug command
|
11
|
+
run command
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Cream
|
2
|
+
module Generators
|
3
|
+
module HamlUtil
|
4
|
+
def verify_haml_existence
|
5
|
+
begin
|
6
|
+
require 'haml'
|
7
|
+
rescue LoadError
|
8
|
+
say "HAML is not installed, or it is not specified in your Gemfile."
|
9
|
+
exit
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def verify_haml_version
|
14
|
+
unless Haml.version[:major] == 2 and Haml.version[:minor] >= 3 or Haml.version[:major] >= 3
|
15
|
+
say "To generate HAML templates, you need to install HAML 2.3 or above."
|
16
|
+
exit
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_and_copy_haml_views
|
21
|
+
verify_haml_existence
|
22
|
+
verify_haml_version
|
23
|
+
|
24
|
+
require 'tmpdir'
|
25
|
+
html_root = "#{self.class.source_root}/cream"
|
26
|
+
|
27
|
+
Dir.mktmpdir("cream-haml.") do |haml_root|
|
28
|
+
Dir["#{html_root}/**/*"].each do |path|
|
29
|
+
relative_path = path.sub(html_root, "")
|
30
|
+
source_path = (haml_root + relative_path).sub(/erb$/, "haml")
|
31
|
+
|
32
|
+
if File.directory?(path)
|
33
|
+
FileUtils.mkdir_p(source_path)
|
34
|
+
else
|
35
|
+
`html2haml -r #{path} #{source_path}`
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
directory haml_root, "app/views/#{scope || 'devise'}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'generators/cream/string_ext'
|
2
|
+
require 'generators/views/haml_util'
|
3
|
+
|
4
|
+
module Cream
|
5
|
+
module Generators
|
6
|
+
class ViewsGenerator < Rails::Generators::Base
|
7
|
+
desc "Copies all AuthAssist 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
|
+
|
13
|
+
def self.source_root
|
14
|
+
@_devise_source_root ||= File.expand_path("app/views".up(4), __FILE__)
|
15
|
+
end
|
16
|
+
|
17
|
+
def copy_views
|
18
|
+
if options[:haml]
|
19
|
+
create_and_copy_haml_views
|
20
|
+
else
|
21
|
+
copy_erb_views
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
|
27
|
+
def copy_erb_views
|
28
|
+
directory "cream", "app/views/#{scope || 'devise'}"
|
29
|
+
end
|
30
|
+
|
31
|
+
include Cream::Generators::HamlUtil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Cream
|
2
|
+
module MigrationHelper
|
3
|
+
module CodeRefactor
|
4
|
+
|
5
|
+
# erase
|
6
|
+
def clear_relations(model_name)
|
7
|
+
model_name = model_name.to_s
|
8
|
+
erase_in(model_name, has_roles_through_assignments)
|
9
|
+
erase_in(model_name, has_roles)
|
10
|
+
erase_in(model_name, has_role_assignments)
|
11
|
+
end
|
12
|
+
|
13
|
+
def erase_in(model_name, txt)
|
14
|
+
model_name = model_name.to_s
|
15
|
+
file = File.new(model_file(model_name))
|
16
|
+
return if !(file.read =~ /#{txt}/)
|
17
|
+
gsub_file model_file(model_name), /#{Regexp.escape(txt + "\n")}/, ''
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
# insert
|
22
|
+
def write_model_file(model_name, content)
|
23
|
+
model_name = model_name.to_s
|
24
|
+
File.open(model_file(model_name), 'w+') do |f|
|
25
|
+
f.write(content)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def insert_relation(model_name, relation)
|
30
|
+
model_name = model_name.to_s
|
31
|
+
file = File.new(model_file(model_name))
|
32
|
+
return if (file.read =~ /#{relation}/)
|
33
|
+
gsub_file model_file(model_name), /class #{model.camelize} < ActiveRecord::Base/ do |match|
|
34
|
+
match << "\n #{relation}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def remove_relation(model_name, relation)
|
39
|
+
erase_in(model_name.to_s, relation)
|
40
|
+
end
|
41
|
+
|
42
|
+
def belongs_to_user
|
43
|
+
'belongs_to :user'
|
44
|
+
end
|
45
|
+
|
46
|
+
def belongs_to_role
|
47
|
+
'belongs_to :role'
|
48
|
+
end
|
49
|
+
|
50
|
+
# refactor code
|
51
|
+
def has_role_assignments
|
52
|
+
'has_many :role_assignments'
|
53
|
+
end
|
54
|
+
|
55
|
+
def has_roles_through_assignments
|
56
|
+
'has_many :roles, :through => :role_assignments'
|
57
|
+
end
|
58
|
+
|
59
|
+
def has_roles
|
60
|
+
'has_many :roles'
|
61
|
+
end
|
62
|
+
|
63
|
+
def role_file_content
|
64
|
+
%Q{
|
65
|
+
class Role < ActiveRecord::Base
|
66
|
+
#{has_role_assignments}
|
67
|
+
#{has_roles_through_assignments}
|
68
|
+
end
|
69
|
+
}
|
70
|
+
end
|
71
|
+
|
72
|
+
def role_assignment_file_content
|
73
|
+
%Q{
|
74
|
+
class RoleAssignment < ActiveRecord::Base
|
75
|
+
#{belongs_to_user}
|
76
|
+
#{belongs_to_role}
|
77
|
+
end
|
78
|
+
}
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/log/development.log
ADDED
File without changes
|
data/sandbox/test.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
module RolePermit
|
2
|
+
|
3
|
+
class Base
|
4
|
+
attr_accessor :ability
|
5
|
+
|
6
|
+
def initialize(ability)
|
7
|
+
@ability = ability
|
8
|
+
end
|
9
|
+
|
10
|
+
def permit?(user)
|
11
|
+
puts "Base Permit"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Admin < Base
|
16
|
+
def initialize(ability)
|
17
|
+
super
|
18
|
+
end
|
19
|
+
|
20
|
+
def permit?(user)
|
21
|
+
super
|
22
|
+
puts "Admin"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Guest < Base
|
27
|
+
def initialize(ability)
|
28
|
+
super
|
29
|
+
end
|
30
|
+
|
31
|
+
def permit?(user)
|
32
|
+
super
|
33
|
+
puts "User"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
RolePermit::Admin.new(0).permit? 0
|
40
|
+
RolePermit::Guest.new(0).permit? 0
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'r3_plugin_toolbox'
|
3
|
+
|
4
|
+
require 'active_support'
|
5
|
+
require 'action_controller'
|
6
|
+
require 'action_view'
|
7
|
+
require 'active_support/railtie'
|
8
|
+
|
9
|
+
module Minimal
|
10
|
+
class Application < Rails::Application
|
11
|
+
config.active_support.deprecation = :log
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Role
|
16
|
+
attr_accessor :name
|
17
|
+
|
18
|
+
def initialize name
|
19
|
+
self.name = name
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.all
|
23
|
+
[Role.new('admin'), Role.new('guest')]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
require 'cream/role'
|
28
|
+
require 'cream/configure'
|
29
|
+
|
30
|
+
Rails.application = Minimal::Application
|
31
|
+
|
32
|
+
Minimal::Application.initialize!
|
33
|
+
|
34
|
+
describe "Rails extension" do
|
35
|
+
describe 'View extension' do
|
36
|
+
it "should extend Action View with Roles helper instance_methods and specific role methods" do
|
37
|
+
extend Rails3::Plugin::Extender::Macro
|
38
|
+
|
39
|
+
after_init :view do |view|
|
40
|
+
view.should be_extended_with Cream::View, :roles, :area, :user_action_menu
|
41
|
+
[:admin, :guest].each do |role|
|
42
|
+
view.should respond_to? :"for_#{role}"
|
43
|
+
view.should respond_to? :"#{role}_area"
|
44
|
+
view.should respond_to? :"#{role}?"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
Minimal::Application.initialize!
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|