rails-maker 0.1.1 → 0.1.7
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.
- checksums.yaml +7 -0
- data/lib/rails-maker/cli.rb +2 -1
- data/lib/rails-maker/version.rb +1 -1
- data/templates/default/bootstrap.rb +34 -60
- data/templates/default/lib/admin.rb +9 -9
- data/templates/default/lib/admin/layout.rb +49 -62
- data/templates/default/lib/application_layout.rb +64 -81
- data/templates/default/lib/authentication.rb +17 -104
- data/templates/default/lib/authentication/header_login_items.rb +24 -0
- data/templates/default/lib/authentication/migrations.rb +50 -0
- data/templates/default/lib/authentication/omniauth.rb +0 -0
- data/templates/default/lib/authentication/user_auth_model.rb +94 -0
- data/templates/default/lib/authentication/user_model.rb +62 -0
- data/templates/default/lib/authorization.rb +20 -52
- data/templates/default/lib/css.rb +2 -0
- data/templates/default/lib/db.rb +25 -2
- data/templates/default/lib/gemfile.rb +60 -64
- data/templates/default/lib/{rails_clean.rb → general.rb} +8 -5
- data/templates/default/lib/git.rb +39 -0
- data/templates/default/lib/home_controller.rb +3 -9
- data/templates/default/lib/initializers.rb +9 -0
- data/templates/default/lib/routes.rb +30 -0
- data/templates/default/lib/test_suite.rb +2 -9
- metadata +14 -27
- data/templates/default/lib/admin/sass.rb +0 -43
- data/templates/default/lib/haml_generator.rb +0 -13
@@ -1,3 +1,5 @@
|
|
1
|
+
say '## AUTHENTICATION >>'
|
2
|
+
|
1
3
|
require 'hpricot'
|
2
4
|
require 'ruby_parser'
|
3
5
|
|
@@ -43,123 +45,34 @@ RUBY
|
|
43
45
|
end
|
44
46
|
|
45
47
|
run 'rails generate devise User'
|
46
|
-
run 'rm app/models/user.rb'
|
47
|
-
|
48
|
-
create_file 'app/models/user.rb' do
|
49
|
-
<<-RUBY
|
50
|
-
class User < ActiveRecord::Base
|
51
|
-
devise :database_authenticatable, :token_authenticatable, :recoverable, :rememberable, :trackable, :confirmable
|
52
|
-
default_scope :conditions => { :deleted_at => nil }
|
53
|
-
validates_presence_of :name, :email
|
54
|
-
validates_presence_of :password, :on => :create
|
55
|
-
validates_confirmation_of :password, :on => :create
|
56
|
-
validates_length_of :password, :within => 6..30, :allow_blank => true
|
57
|
-
validates_uniqueness_of :email, :case_sensitive => false, :scope => :deleted_at
|
58
|
-
validates_format_of :email, :with => Devise::email_regexp
|
59
|
-
|
60
|
-
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
|
61
|
-
|
62
|
-
def destroy
|
63
|
-
self.update_attribute(:deleted_at, Time.now.utc)
|
64
|
-
end
|
65
|
-
|
66
|
-
def self.find_with_destroyed *args
|
67
|
-
self.with_exclusive_scope { find(*args) }
|
68
|
-
end
|
69
|
-
|
70
|
-
def self.find_only_destroyed
|
71
|
-
self.with_exclusive_scope :find => { :conditions => "deleted_at IS NOT NULL" } do
|
72
|
-
all
|
73
|
-
end
|
74
|
-
end
|
75
48
|
|
76
|
-
|
77
|
-
|
78
|
-
|
49
|
+
apply File.expand_path("../authentication/user_model.rb", __FILE__)
|
50
|
+
apply File.expand_path("../authentication/user_auth_model.rb", __FILE__)
|
51
|
+
#apply File.expand_path("../authentication/migrations.rb", __FILE__)
|
79
52
|
|
80
53
|
generate(:migration, "AddNameToUsers name:string")
|
81
|
-
generate(:migration, "AddCachedSlugToUsers cached_slug:string")
|
82
54
|
generate(:migration, "AddDeletedAtToUsers deleted_at:datetime")
|
83
55
|
|
84
|
-
|
85
|
-
<<-'FILE'
|
86
|
-
- if user_signed_in?
|
87
|
-
%li
|
88
|
-
= link_to('Logout', destroy_user_session_path)
|
89
|
-
- else
|
90
|
-
%li
|
91
|
-
= link_to('Login', new_user_session_path)
|
92
|
-
%li
|
93
|
-
User:
|
94
|
-
- if current_user
|
95
|
-
= current_user.name
|
96
|
-
- else
|
97
|
-
(not logged in)
|
98
|
-
FILE
|
99
|
-
end
|
56
|
+
apply File.expand_path("../authentication/omniauth.rb", __FILE__)
|
100
57
|
|
101
|
-
|
102
|
-
<<-'FILE'
|
103
|
-
%ul#user_nav
|
104
|
-
= render 'devise/menu/login_items'
|
105
|
-
FILE
|
106
|
-
end
|
58
|
+
apply File.expand_path("../authentication/header_login_items.rb", __FILE__)
|
107
59
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
class
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
t.string :encrypted_password, :null => false, :default => ""
|
117
|
-
|
118
|
-
## Recoverable
|
119
|
-
t.string :reset_password_token
|
120
|
-
t.datetime :reset_password_sent_at
|
121
|
-
|
122
|
-
## Rememberable
|
123
|
-
t.datetime :remember_created_at
|
124
|
-
|
125
|
-
## Trackable
|
126
|
-
t.integer :sign_in_count, :default => 0
|
127
|
-
t.datetime :current_sign_in_at
|
128
|
-
t.datetime :last_sign_in_at
|
129
|
-
t.string :current_sign_in_ip
|
130
|
-
t.string :last_sign_in_ip
|
131
|
-
|
132
|
-
## Confirmable
|
133
|
-
t.string :confirmation_token
|
134
|
-
t.datetime :confirmed_at
|
135
|
-
t.datetime :confirmation_sent_at
|
136
|
-
t.string :unconfirmed_email
|
137
|
-
|
138
|
-
## Lockable
|
139
|
-
t.integer :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
|
140
|
-
t.string :unlock_token # Only if unlock strategy is :email or :both
|
141
|
-
t.datetime :locked_at
|
142
|
-
|
143
|
-
## Token authenticatable
|
144
|
-
t.string :authentication_token
|
145
|
-
|
146
|
-
t.timestamps
|
147
|
-
end
|
148
|
-
|
149
|
-
add_index :users, :email, :unique => true
|
150
|
-
add_index :users, :reset_password_token, :unique => true
|
151
|
-
add_index :users, :confirmation_token, :unique => true
|
152
|
-
add_index :users, :unlock_token, :unique => true
|
153
|
-
add_index :users, :authentication_token, :unique => true
|
154
|
-
end
|
60
|
+
run 'rm app/controllers/application_controller.rb'
|
61
|
+
create_file 'app/controllers/application_controller.rb' do
|
62
|
+
<<-RUBY
|
63
|
+
class ApplicationController < ActionController::Base
|
64
|
+
protect_from_forgery
|
65
|
+
before_filter :authenticate_user!
|
66
|
+
end
|
67
|
+
RUBY
|
155
68
|
end
|
156
|
-
FILE
|
157
69
|
|
158
70
|
append_file 'db/seeds.rb' do
|
159
71
|
<<-FILE
|
160
72
|
# Setup initial user so we can get in
|
161
|
-
user = User.create! :name => "
|
73
|
+
user = User.create! :name => "admin", :email => "admin@local.host", :password => "admin123", :password_confirmation => "admin123"
|
162
74
|
user.confirmed_at = user.confirmation_sent_at
|
75
|
+
|
163
76
|
user.save
|
164
77
|
FILE
|
165
78
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
|
2
|
+
create_file 'app/views/devise/menu/_login_items.html.haml' do
|
3
|
+
<<-'FILE'
|
4
|
+
%div.pull-right{ style: "padding-top: 9px" }
|
5
|
+
- if current_user.present?
|
6
|
+
= link_to current_user.username, user_path(current_user)
|
7
|
+
|
|
8
|
+
|
9
|
+
- if current_user.admin?
|
10
|
+
= link_to "admin", admin_root_path
|
11
|
+
|
|
12
|
+
|
13
|
+
= link_to "logout", destroy_user_session_path
|
14
|
+
- else
|
15
|
+
= link_to "login", new_user_session_path
|
16
|
+
FILE
|
17
|
+
end
|
18
|
+
|
19
|
+
append_file 'app/views/shared/_header.html.haml' do
|
20
|
+
<<-'FILE'
|
21
|
+
%ul#user_nav
|
22
|
+
= render 'devise/menu/login_items'
|
23
|
+
FILE
|
24
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
|
2
|
+
devise_migration = Dir['db/migrate/*_devise_create_users.rb'].first
|
3
|
+
|
4
|
+
gsub_file devise_migration, /./, <<-FILE
|
5
|
+
class DeviseCreateUsers < ActiveRecord::Migration
|
6
|
+
def change
|
7
|
+
create_table(:users) do |t|
|
8
|
+
## Database authenticatable
|
9
|
+
t.string :email, :null => false, :default => ""
|
10
|
+
t.string :encrypted_password, :null => false, :default => ""
|
11
|
+
|
12
|
+
## Recoverable
|
13
|
+
t.string :reset_password_token
|
14
|
+
t.datetime :reset_password_sent_at
|
15
|
+
|
16
|
+
## Rememberable
|
17
|
+
t.datetime :remember_created_at
|
18
|
+
|
19
|
+
## Trackable
|
20
|
+
t.integer :sign_in_count, :default => 0
|
21
|
+
t.datetime :current_sign_in_at
|
22
|
+
t.datetime :last_sign_in_at
|
23
|
+
t.string :current_sign_in_ip
|
24
|
+
t.string :last_sign_in_ip
|
25
|
+
|
26
|
+
## Confirmable
|
27
|
+
t.string :confirmation_token
|
28
|
+
t.datetime :confirmed_at
|
29
|
+
t.datetime :confirmation_sent_at
|
30
|
+
t.string :unconfirmed_email
|
31
|
+
|
32
|
+
## Lockable
|
33
|
+
t.integer :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
|
34
|
+
t.string :unlock_token # Only if unlock strategy is :email or :both
|
35
|
+
t.datetime :locked_at
|
36
|
+
|
37
|
+
## Token authenticatable
|
38
|
+
t.string :authentication_token
|
39
|
+
|
40
|
+
t.timestamps
|
41
|
+
end
|
42
|
+
|
43
|
+
add_index :users, :email, :unique => true
|
44
|
+
add_index :users, :reset_password_token, :unique => true
|
45
|
+
add_index :users, :confirmation_token, :unique => true
|
46
|
+
add_index :users, :unlock_token, :unique => true
|
47
|
+
add_index :users, :authentication_token, :unique => true
|
48
|
+
end
|
49
|
+
end
|
50
|
+
FILE
|
File without changes
|
@@ -0,0 +1,94 @@
|
|
1
|
+
|
2
|
+
run 'mkdir app/models/user'
|
3
|
+
|
4
|
+
create_file 'app/models/user/auth.rb' do
|
5
|
+
|
6
|
+
<<-RUBY
|
7
|
+
module User::Auth
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
def new_with_session(params, session)
|
12
|
+
super.tap do |user|
|
13
|
+
data = session["devise.omniauth"] && session["devise.omniauth"]["extra"]["raw_info"]
|
14
|
+
|
15
|
+
if data
|
16
|
+
user.email = data["email"]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def find_first_by_auth_conditions(warden_conditions)
|
22
|
+
conditions = warden_conditions.dup
|
23
|
+
|
24
|
+
login = conditions.delete(:login)
|
25
|
+
|
26
|
+
if login
|
27
|
+
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
|
28
|
+
else
|
29
|
+
where(conditions).first
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.included(base)
|
35
|
+
base.has_many :authentications, dependent: :delete_all
|
36
|
+
|
37
|
+
# Include default devise modules. Others available are:
|
38
|
+
# :token_authenticatable, :encryptable, :lockable, :timeoutable,
|
39
|
+
base.devise :database_authenticatable, :registerable, :confirmable,
|
40
|
+
:recoverable, :rememberable, :trackable, :omniauthable, :validatable
|
41
|
+
|
42
|
+
base.extend(ClassMethods)
|
43
|
+
end
|
44
|
+
|
45
|
+
def apply_omniauth(auth_hash)
|
46
|
+
|
47
|
+
name, email, token, info = nil, nil, nil, nil
|
48
|
+
|
49
|
+
puts auth_hash.inspect
|
50
|
+
|
51
|
+
name = auth_hash.info.name
|
52
|
+
email = auth_hash.extra.raw_info.email
|
53
|
+
token = auth_hash.credentials.token
|
54
|
+
info = auth_hash.extra.raw_info
|
55
|
+
|
56
|
+
self.email = email
|
57
|
+
self.username = make_pretty_username name
|
58
|
+
self.authentications.build(
|
59
|
+
provider: auth_hash['provider'],
|
60
|
+
uid: auth_hash['uid'],
|
61
|
+
token: token,
|
62
|
+
info: info.to_json
|
63
|
+
)
|
64
|
+
end
|
65
|
+
|
66
|
+
protected
|
67
|
+
|
68
|
+
def make_pretty_username name
|
69
|
+
#strip the string
|
70
|
+
ret = name.strip
|
71
|
+
|
72
|
+
#blow away apostrophes
|
73
|
+
ret.gsub! /['`]/,""
|
74
|
+
|
75
|
+
# @ --> at, and & --> and
|
76
|
+
ret.gsub! /\s*@\s*/, " at "
|
77
|
+
ret.gsub! /\s*&\s*/, " and "
|
78
|
+
|
79
|
+
#replace all non alphanumeric, underscore or periods with underscore
|
80
|
+
ret.gsub! /\s*[^A-Za-z0-9\.\-]\s*/, '_'
|
81
|
+
|
82
|
+
#convert double underscores to single
|
83
|
+
ret.gsub! /_+/,"_"
|
84
|
+
|
85
|
+
#strip off leading/trailing underscore
|
86
|
+
ret.gsub! /\A[_\.]+|[_\.]+\z/,""
|
87
|
+
|
88
|
+
ret
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
RUBY
|
93
|
+
|
94
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
run 'rm app/models/user.rb'
|
2
|
+
create_file 'app/models/user.rb' do
|
3
|
+
|
4
|
+
<<-RUBY
|
5
|
+
|
6
|
+
class User < ActiveRecord::Base
|
7
|
+
include User::Auth
|
8
|
+
|
9
|
+
rolify
|
10
|
+
|
11
|
+
default_scope :conditions => { :deleted_at => nil }
|
12
|
+
validates_presence_of :name, :email
|
13
|
+
validates_presence_of :password, :on => :create
|
14
|
+
validates_confirmation_of :password, :on => :create
|
15
|
+
validates_length_of :password, :within => 6..30, :allow_blank => false
|
16
|
+
validates_uniqueness_of :email, :case_sensitive => false, :scope => :deleted_at
|
17
|
+
validates_format_of :email, :with => Devise::email_regexp
|
18
|
+
|
19
|
+
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
|
20
|
+
|
21
|
+
def destroy
|
22
|
+
self.update_attribute(:deleted_at, Time.now.utc)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.find_with_destroyed *args
|
26
|
+
self.with_exclusive_scope { find(*args) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.find_only_destroyed
|
30
|
+
self.with_exclusive_scope :find => { :conditions => "deleted_at IS NOT NULL" } do
|
31
|
+
all
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def password_required?
|
36
|
+
(authentications.empty? || !password.blank?) && super
|
37
|
+
end
|
38
|
+
|
39
|
+
def mark_as_confirmed
|
40
|
+
self.confirmation_token = nil
|
41
|
+
self.confirmed_at = Time.now
|
42
|
+
end
|
43
|
+
|
44
|
+
def admin?
|
45
|
+
self.has_role? :admin
|
46
|
+
end
|
47
|
+
|
48
|
+
def possible_name
|
49
|
+
if self.firstname.blank? && self.lastname.blank? && self.nickname.blank?
|
50
|
+
'<noname user>' # TODO
|
51
|
+
else
|
52
|
+
if self.firstname.blank? && self.lastname.blank?
|
53
|
+
self.nickname
|
54
|
+
else
|
55
|
+
[self.firstname, self.lastname].join(' ').strip
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
RUBY
|
61
|
+
|
62
|
+
end
|
@@ -1,34 +1,25 @@
|
|
1
|
-
say
|
2
|
-
generate(:model, "role name:string")
|
3
|
-
generate(:migration, "UsersHaveAndBelongToManyRoles")
|
4
|
-
habtm_roles = Dir['db/migrate/*_users_have_and_belong_to_many_roles.rb'].first
|
5
|
-
inject_into_file habtm_roles, :after => "def self.up\n" do
|
6
|
-
<<-RUBY
|
7
|
-
create_table :roles_users, :id => false do |t|
|
8
|
-
t.references :role, :user
|
9
|
-
end
|
10
|
-
RUBY
|
11
|
-
end
|
1
|
+
say '## AUTHORIZATION >>'
|
12
2
|
|
13
|
-
|
14
|
-
<<-RUBY
|
15
|
-
drop_table :roles_users
|
16
|
-
RUBY
|
17
|
-
end
|
3
|
+
say 'Building roles..'
|
18
4
|
|
19
|
-
|
5
|
+
run 'rails g rolify:role Role User'
|
6
|
+
|
7
|
+
inject_into_file 'app/models/user.rb', :after => "include User::Auth\n" do
|
20
8
|
<<-RUBY
|
21
|
-
|
9
|
+
rolify
|
22
10
|
RUBY
|
23
11
|
end
|
24
12
|
|
25
|
-
|
13
|
+
run 'rm app/models/role.rb'
|
14
|
+
create_file 'app/models/role.rb' do
|
26
15
|
<<-RUBY
|
27
|
-
|
16
|
+
class Role < ActiveRecord::Base
|
28
17
|
|
29
|
-
|
30
|
-
|
31
|
-
|
18
|
+
has_and_belongs_to_many :users, :join_table => :users_roles
|
19
|
+
belongs_to :resource, :polymorphic => true
|
20
|
+
|
21
|
+
scopify
|
22
|
+
end
|
32
23
|
RUBY
|
33
24
|
end
|
34
25
|
|
@@ -59,16 +50,6 @@ end
|
|
59
50
|
RUBY
|
60
51
|
end
|
61
52
|
|
62
|
-
inject_into_file 'app/models/user.rb', :before => "def destroy\n" do
|
63
|
-
<<-RUBY
|
64
|
-
|
65
|
-
def role?(role)
|
66
|
-
return !!self.roles.find_by_name( Role.sanitize role )
|
67
|
-
end
|
68
|
-
|
69
|
-
RUBY
|
70
|
-
end
|
71
|
-
|
72
53
|
inject_into_file 'app/controllers/application_controller.rb', :before => "end\n" do
|
73
54
|
<<-RUBY
|
74
55
|
|
@@ -79,21 +60,13 @@ inject_into_file 'app/controllers/application_controller.rb', :before => "end\n"
|
|
79
60
|
RUBY
|
80
61
|
end
|
81
62
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
.form_row
|
86
|
-
- Role.find(:all, :order => "name").each do |role|
|
87
|
-
.check_box_item
|
88
|
-
= check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role), :id => "user_role_#{role.id}"
|
89
|
-
%label{:for => "user_role_#{role.id}"}= role.name.humanize
|
90
|
-
= hidden_field_tag "user[role_ids][]", ""
|
91
|
-
RUBY
|
92
|
-
end
|
63
|
+
inject_into_file 'db/seeds.rb', :before => "user.save" do
|
64
|
+
<<-RUBY
|
65
|
+
user.add_role, :admin
|
93
66
|
|
94
|
-
|
67
|
+
RUBY
|
95
68
|
end
|
96
|
-
|
69
|
+
=begin
|
97
70
|
append_file 'db/seeds.rb' do
|
98
71
|
<<-FILE
|
99
72
|
Role.create! :name => 'Admin'
|
@@ -104,9 +77,4 @@ user1.role_ids = [1,2]
|
|
104
77
|
user1.save
|
105
78
|
FILE
|
106
79
|
end
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
80
|
+
=end
|