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.
@@ -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
- end
77
- RUBY
78
- end
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
- create_file 'app/views/devise/menu/_login_items.html.haml' do
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
- append_file 'app/views/shared/_header.html.haml' do
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
- devise_migration = Dir['db/migrate/*_devise_create_users.rb'].first
109
-
110
- gsub_file devise_migration, /./, <<-FILE
111
- class DeviseCreateUsers < ActiveRecord::Migration
112
- def change
113
- create_table(:users) do |t|
114
- ## Database authenticatable
115
- t.string :email, :null => false, :default => ""
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 => "#{ENV['RAILSMAKER_USER_NAME']}", :email => "#{ENV['RAILSMAKER_USER_EMAIL']}", :password => "#{ENV['RAILSMAKER_USER_PASSWORD']}", :password_confirmation => "#{ENV['RAILSMAKER_USER_PASSWORD']}"
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
+ &nbsp; | &nbsp;
8
+
9
+ - if current_user.admin?
10
+ = link_to "admin", admin_root_path
11
+ &nbsp; | &nbsp;
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 "Building roles"
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
- inject_into_file habtm_roles, :after => "def self.down\n" do
14
- <<-RUBY
15
- drop_table :roles_users
16
- RUBY
17
- end
3
+ say 'Building roles..'
18
4
 
19
- inject_into_file 'app/models/user.rb', :after => "class User < ActiveRecord::Base\n" do
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
- has_and_belongs_to_many :roles
9
+ rolify
22
10
  RUBY
23
11
  end
24
12
 
25
- inject_into_file 'app/models/role.rb', :after => "class Role < ActiveRecord::Base\n" do
13
+ run 'rm app/models/role.rb'
14
+ create_file 'app/models/role.rb' do
26
15
  <<-RUBY
27
- has_and_belongs_to_many :users
16
+ class Role < ActiveRecord::Base
28
17
 
29
- def self.sanitize role
30
- role.to_s.humanize.split(' ').each{ |word| word.capitalize! }.join(' ')
31
- end
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
- if ENV['RAILSMAKER_ADMIN']
83
- inject_into_file 'app/views/admin/users/_form.html.haml', :after => "= f.password_field :password_confirmation\n" do
84
- <<-'RUBY'
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
- gsub_file 'app/controllers/admin/users_controller.rb', /# attr_accessor logic here/, '@user.accessible = [:role_ids] if current_user.role? :admin'
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