mologue 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,112 @@
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
12
+
13
+ inject_into_file habtm_roles, :after => "def self.down\n" do
14
+ <<-RUBY
15
+ drop_table :roles_users
16
+ RUBY
17
+ end
18
+
19
+ inject_into_file 'app/models/user.rb', :after => "class User < ActiveRecord::Base\n" do
20
+ <<-RUBY
21
+ has_and_belongs_to_many :roles
22
+ RUBY
23
+ end
24
+
25
+ inject_into_file 'app/models/role.rb', :after => "class Role < ActiveRecord::Base\n" do
26
+ <<-RUBY
27
+ has_and_belongs_to_many :users
28
+
29
+ def self.sanitize role
30
+ role.to_s.humanize.split(' ').each{ |word| word.capitalize! }.join(' ')
31
+ end
32
+ RUBY
33
+ end
34
+
35
+ create_file 'app/models/ability.rb' do
36
+ <<-RUBY
37
+ class Ability
38
+ include CanCan::Ability
39
+
40
+ def initialize(user)
41
+ user ||= User.new # guest user
42
+
43
+ if user.role? :admin
44
+ can :manage, :all
45
+ # elsif user.role? :writter
46
+ # can :manage, [Post, Asset]
47
+ # elsif user.role? :memeber
48
+ # can :read, [MemberPost, Asset]
49
+ # # manage posts, assets user owns
50
+ # can :manage, Post do |p|
51
+ # p.try(:owner) == user
52
+ # end
53
+ # can :manage, Asset do |a|
54
+ # a.try(:owner) == user
55
+ # end
56
+ end
57
+ end
58
+ end
59
+ RUBY
60
+ end
61
+
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
+ inject_into_file 'app/controllers/application_controller.rb', :before => "end\n" do
73
+ <<-RUBY
74
+
75
+ rescue_from CanCan::AccessDenied do |exception|
76
+ flash[:error] = "Access Denied"
77
+ redirect_to root_url
78
+ end
79
+ RUBY
80
+ end
81
+
82
+ if ENV['MOLOGUE_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
93
+
94
+ gsub_file 'app/controllers/admin/users_controller.rb', /# attr_accessor logic here/, '@user.accessible = [:role_ids] if current_user.role? :admin'
95
+ end
96
+
97
+ append_file 'db/seeds.rb' do
98
+ <<-FILE
99
+ Role.create! :name => 'Admin'
100
+ Role.create! :name => 'Member'
101
+
102
+ user1 = User.find_by_email('#{ENV['MOLOGUE_USER_EMAIL']}')
103
+ user1.role_ids = [1,2]
104
+ user1.save
105
+ FILE
106
+ end
107
+
108
+
109
+
110
+
111
+
112
+
@@ -0,0 +1,8 @@
1
+ gsub_file 'config/routes.rb', /get \"home\/index\"/, ''
2
+ gsub_file 'config/routes.rb', /get \"users\/index\"/, ''
3
+ gsub_file 'config/routes.rb', /get \"users\/new\"/, ''
4
+ gsub_file 'config/routes.rb', /get \"users\/create\"/, ''
5
+ gsub_file 'config/routes.rb', /get \"users\/edit\"/, ''
6
+ gsub_file 'config/routes.rb', /get \"users\/update\"/, ''
7
+ gsub_file 'config/routes.rb', /get \"users\/destroy\"/, ''
8
+ gsub_file 'config/routes.rb', /get \"dashboard\/index\"/, ''
@@ -0,0 +1,2 @@
1
+ run 'rake db:migrate'
2
+ run 'rake db:test:prepare'
@@ -0,0 +1,5 @@
1
+ if ENV['MOLOGUE_MONGOID']
2
+ run 'rake db:reseed'
3
+ else
4
+ run 'rake db:seed'
5
+ end
@@ -0,0 +1,215 @@
1
+ say "Building authentication"
2
+ gsub_file 'config/application.rb', /:password/, ':password, :password_confirmation'
3
+
4
+ run 'rails generate devise:install'
5
+
6
+ gsub_file 'config/environments/development.rb', /# Don't care if the mailer can't send/, '### ActionMailer Config'
7
+
8
+ gsub_file 'config/environments/development.rb', /config.action_mailer.raise_delivery_errors = false/ do
9
+ <<-RUBY
10
+ config.action_mailer.default_url_options = { :host => '0.0.0.0:3000' }
11
+ # A dummy setup for development - no deliveries, but logged
12
+ config.action_mailer.delivery_method = :smtp
13
+ config.action_mailer.perform_deliveries = false
14
+ config.action_mailer.raise_delivery_errors = true
15
+ config.action_mailer.default :charset => "utf-8"
16
+ RUBY
17
+ end
18
+
19
+ inject_into_file 'config/environments/test.rb', :after => "config.action_controller.allow_forgery_protection = false\n" do
20
+ <<-RUBY
21
+ config.action_mailer.default_url_options = { :host => '0.0.0.0:3000' }
22
+ RUBY
23
+ end
24
+
25
+ gsub_file 'config/environments/production.rb', /config.i18n.fallbacks = true/ do
26
+ <<-RUBY
27
+ config.i18n.fallbacks = true
28
+ config.action_mailer.default_url_options = { :host => 'yourhost.com' }
29
+ ### ActionMailer Config
30
+ # Setup for production - deliveries, no errors raised
31
+ config.action_mailer.delivery_method = :smtp
32
+ config.action_mailer.perform_deliveries = true
33
+ config.action_mailer.raise_delivery_errors = false
34
+ config.action_mailer.default :charset => "utf-8"
35
+ RUBY
36
+ end
37
+
38
+ run 'rails generate devise User'
39
+
40
+ run 'rm app/models/user.rb'
41
+ if ENV['MOLOGUE_MONGOID']
42
+ create_file 'app/models/user.rb' do
43
+ <<-RUBY
44
+ class User
45
+ include Mongoid::Document
46
+ # Include default devise modules. Others available are:
47
+ # :lockable and :timeoutable
48
+ devise :database_authenticatable, :registerable, :token_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :confirmable
49
+
50
+ field :username
51
+ field :admin, :type => Boolean
52
+
53
+ validates_presence_of :username
54
+ validates_uniqueness_of :username, :email, :case_sensitive => false
55
+ attr_accessible :username, :admin, :email, :password, :password_confirmation, :remember_me
56
+ end
57
+ RUBY
58
+ end
59
+ else
60
+ create_file 'app/models/user.rb' do
61
+ <<-RUBY
62
+ class User < ActiveRecord::Base
63
+ # Include default devise modules. Others available are:
64
+ # :lockable and :timeoutable
65
+ devise :database_authenticatable, :registerable, :token_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :confirmable
66
+
67
+ validates_presence_of :username
68
+ validates_uniqueness_of :username, :email, :case_sensitive => false, :scope => :deleted_at
69
+ attr_accessible :username, :admin, :email, :password, :password_confirmation, :remember_me
70
+
71
+ default_scope :conditions => { :deleted_at => nil }
72
+ has_friendly_id :username, :use_slug => true, :strip_non_ascii => true
73
+
74
+ def destroy
75
+ self.update_attribute(:deleted_at, Time.now.utc)
76
+ end
77
+
78
+ def self.find_with_destroyed *args
79
+ self.with_exclusive_scope { find(*args) }
80
+ end
81
+
82
+ def self.find_only_destroyed
83
+ self.with_exclusive_scope :find => { :conditions => "deleted_at IS NOT NULL" } do
84
+ all
85
+ end
86
+ end
87
+
88
+ end
89
+ RUBY
90
+ end
91
+ generate(:migration, "AddNameToUsers username:string")
92
+ generate(:migration, "AddAdminToUsers admin:boolean")
93
+ generate(:migration, "AddCachedSlugToUsers cached_slug:string")
94
+ generate(:migration, "AddDeletedAtToUsers deleted_at:datetime")
95
+
96
+ devise_migration = Dir['db/migrate/*_devise_create_users.rb'].first
97
+
98
+ gsub_file devise_migration, /# t.confirmable/, 't.confirmable'
99
+ gsub_file devise_migration, /# t.token_authenticatable/, 't.token_authenticatable'
100
+ gsub_file devise_migration, /# add_index :users, :confirmation_token, :unique => true/, 'add_index :users, :confirmation_token, :unique => true'
101
+
102
+ end
103
+
104
+ run 'rails generate devise:views'
105
+
106
+ run 'rm app/views/devise/registrations/edit.html.haml'
107
+ create_file 'app/views/devise/registrations/edit.html.haml' do
108
+ <<-FILE
109
+ %h2
110
+ Edit \#{resource_name.to_s.humanize}
111
+ = form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f|
112
+ = devise_error_messages!
113
+ %p
114
+ = f.label :username
115
+ %br/
116
+ = f.text_field :username
117
+ %p
118
+ = f.label :email
119
+ %br/
120
+ = f.text_field :email
121
+ %p
122
+ = f.label :password
123
+ %i (leave blank if you don't want to change it)
124
+ %br/
125
+ = f.password_field :password
126
+ %p
127
+ = f.label :password_confirmation
128
+ %br/
129
+ = f.password_field :password_confirmation
130
+ %p
131
+ = f.label :current_password
132
+ %i (we need your current password to confirm your changes)
133
+ %br/
134
+ = f.password_field :current_password
135
+ %p= f.submit "Update"
136
+ %h3 Cancel my account
137
+ %p
138
+ Unhappy? \#{link_to "Cancel my account", registration_path(resource_name), :confirm => "Are you sure?", :method => :delete}.
139
+ = link_to "Back", :back
140
+ FILE
141
+ end
142
+
143
+ run 'rm app/views/devise/registrations/new.html.haml'
144
+ create_file 'app/views/devise/registrations/new.html.haml' do
145
+ <<-'FILE'
146
+ %h2 Sign up
147
+ = form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
148
+ = devise_error_messages!
149
+ %p
150
+ = f.label :username
151
+ %br/
152
+ = f.text_field :username
153
+ %p
154
+ = f.label :email
155
+ %br/
156
+ = f.text_field :email
157
+ %p
158
+ = f.label :password
159
+ %br/
160
+ = f.password_field :password
161
+ %p
162
+ = f.label :password_confirmation
163
+ %br/
164
+ = f.password_field :password_confirmation
165
+ %p= f.submit "Sign up"
166
+ = render :partial => "devise/shared/links"
167
+ FILE
168
+ end
169
+
170
+ create_file "app/views/devise/menu/_registration_items.html.haml" do <<-'FILE'
171
+ - if user_signed_in?
172
+ %li
173
+ = link_to('Edit account', edit_user_registration_path)
174
+ - else
175
+ %li
176
+ = link_to('Sign up', new_user_registration_path)
177
+ FILE
178
+ end
179
+
180
+
181
+ create_file 'app/views/devise/menu/_login_items.html.haml' do
182
+ <<-'FILE'
183
+ - if user_signed_in?
184
+ %li
185
+ = link_to('Logout', destroy_user_session_path)
186
+ %li
187
+ = current_user.username
188
+ - else
189
+ %li
190
+ = link_to('Login', new_user_session_path)
191
+ FILE
192
+ end
193
+
194
+ append_file 'app/views/shared/_header.html.haml' do
195
+ <<-'FILE'
196
+ %ul#user_nav
197
+ = render 'devise/menu/registration_items'
198
+ = render 'devise/menu/login_items'
199
+ FILE
200
+ end
201
+
202
+ ENV['MOLOGUE_USERNAME'] = ask("What would you like the username of the admin user to be? (example: 'admin')")
203
+ ENV['MOLOGUE_USER_EMAIL'] = ask("What would you like the administrator's email address to be? (example: 'me@me.com')")
204
+ ENV['MOLOGUE_USER_PASSWORD'] = ask("What would you like the administrator's password to be? (example: 'xyz123')")
205
+ append_file 'db/seeds.rb' do
206
+ <<-FILE
207
+ # Setup initial user so we can get in
208
+ user = User.create! :username => "#{ENV['MOLOGUE_USERNAME']}", :email => "#{ENV['MOLOGUE_USER_EMAIL']}", :password => "#{ENV['MOLOGUE_USER_PASSWORD']}", :password_confirmation => "#{ENV['MOLOGUE_USER_PASSWORD']}"
209
+ user.confirmed_at = user.confirmation_sent_at
210
+ user.save
211
+ FILE
212
+ end
213
+
214
+ # make cukes and websteps for devise
215
+ apply File.expand_path("../devise/cucumber.rb", __FILE__)
@@ -0,0 +1,111 @@
1
+
2
+ create_file 'spec/factories/user.rb' do
3
+ <<-'FILE'
4
+ Factory.define :user do |u|
5
+ u.sequence(:username) { |n| "Quick #{n}" }
6
+ u.sequence(:email) { |n| "user.#{n}@quickleft.com" }
7
+ u.password "password"
8
+ u.confirmed_at Time.new.to_s
9
+ u.confirmation_sent_at Time.new.to_s
10
+ u.password_confirmation { |u| u.password }
11
+ end
12
+
13
+ Factory.define :admin, :parent => :user do |admin|
14
+ admin.username "admin"
15
+ admin.email "admin@quickleft.com"
16
+ admin.password "password"
17
+ end
18
+
19
+ Factory.define :member, :parent => :user do |member|
20
+ member.username "member"
21
+ member.email "member@quickleft.com"
22
+ member.password "password"
23
+ end
24
+
25
+ FILE
26
+ end
27
+
28
+ create_file 'features/step_definitions/devise_steps.rb' do
29
+ <<-'FILE'
30
+ When /^I log in as "([^\"]*)" with password "([^\"]*)"$/ do |email, password|
31
+ visit(new_user_session_path)
32
+ fill_in("user[email]", :with => email)
33
+ fill_in("user[password]", :with => password)
34
+ click_button("Sign in")
35
+ end
36
+
37
+ Given /^a logged in (\w+)$/ do |usertype|
38
+ Factory.create(usertype.to_sym)
39
+ visit(new_user_session_path)
40
+ fill_in("user[email]", :with => "#{usertype}@quickleft.com")
41
+ fill_in("user[password]", :with => "password")
42
+ click_button("Sign in")
43
+ end
44
+
45
+ When /^I log out$/ do
46
+ visit(destroy_user_session_path)
47
+ end
48
+
49
+ Given /^an? (\w+) "([^\"]*)"$/ do |usertype, email|
50
+ Factory.create(usertype.to_sym, :email => email)
51
+ end
52
+ FILE
53
+ end
54
+
55
+ inject_into_file 'features/support/paths.rb', :after => "case page_name\n" do
56
+ <<-'FILE'
57
+
58
+ when /logout/
59
+ '/users/sign_out'
60
+
61
+ when /login/
62
+ '/users/sign_in'
63
+ FILE
64
+ end
65
+
66
+ create_file 'features/forgot_password.feature' do
67
+ <<-'FILE'
68
+ Feature: Forgot password
69
+ As an user
70
+ I want to secure way to reset my password
71
+ So that I can still login if I forget it
72
+
73
+ Scenario: Follow forget your password link
74
+ Given the following user records
75
+ | email |
76
+ | forgot@quickleft.com |
77
+ When I am on the login page
78
+ And I follow "Forgot your password?"
79
+ And I fill in "Email" with "forgot@quickleft.com"
80
+ And I press "Send me reset password instructions"
81
+ Then I should see "You will receive an email with instructions about how to reset your password in a few minutes."
82
+ And 1 emails should be delivered to forgot@quickleft.com
83
+ When I click the first link in the email
84
+ And I fill in "Password" with "myNewP@ssword"
85
+ And I fill in "Password confirmation" with "myNewP@ssword"
86
+ And I press "Change my password"
87
+ Then I should see "Your password was changed successfully. You are now signed in."
88
+ FILE
89
+ end
90
+
91
+ create_file 'features/resend_verification_email.feature' do
92
+ <<-'FILE'
93
+ Feature: Resend Verification
94
+ As an user
95
+ I want to resend my verification email
96
+ So that I can activate my account if I lost the original email
97
+
98
+ Scenario: Follow resend verification email
99
+ Given the following user records
100
+ | email | confirmed_at |
101
+ | resend@quickleft.com | nil |
102
+ When I am on the login page
103
+ And I follow "Didn't receive confirmation instructions?"
104
+ And I fill in "Email" with "resend@quickleft.com"
105
+ And I press "Resend confirmation instructions"
106
+ Then I should see "You will receive an email with instructions about how to confirm your account in a few minutes."
107
+ And 2 emails should be delivered to resend@quickleft.com
108
+ When I click the first link in the email
109
+ Then I should see "Your account was successfully confirmed. You are now signed in."
110
+ FILE
111
+ end