crosstie 0.0.1 → 0.0.2

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.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/lib/crosstie/base.rb +46 -0
  3. data/lib/crosstie/cli.rb +1 -1
  4. data/lib/crosstie/templates/add_gitignore.rb +11 -0
  5. data/lib/crosstie/templates/authentication_token.rb +53 -0
  6. data/lib/crosstie/templates/authorization/application_controller.rb +39 -0
  7. data/lib/crosstie/templates/authorization/authorization.rb +209 -0
  8. data/lib/crosstie/templates/authorization/index.html.slim +17 -0
  9. data/lib/crosstie/templates/authorization/users.rb +19 -0
  10. data/lib/crosstie/templates/authorization/users_controller.rb +25 -0
  11. data/lib/crosstie/templates/authorization/users_controller_spec.rb +67 -0
  12. data/lib/crosstie/templates/authorization.rb +47 -0
  13. data/lib/crosstie/templates/bundle_install.rb +2 -0
  14. data/lib/crosstie/{template.rb → templates/change_source.rb} +0 -4
  15. data/lib/crosstie/templates/change_timezone.rb +3 -0
  16. data/lib/crosstie/templates/config_scaffold.rb +12 -0
  17. data/lib/crosstie/templates/config_timezone.rb +3 -0
  18. data/lib/crosstie/templates/controller_helpers.rb +26 -0
  19. data/lib/crosstie/templates/database_example.rb +2 -0
  20. data/lib/crosstie/templates/devise.rb +82 -0
  21. data/lib/crosstie/templates/figaro.rb +3 -0
  22. data/lib/crosstie/templates/git_init.rb +3 -0
  23. data/lib/crosstie/templates/guard.rb +3 -0
  24. data/lib/crosstie/templates/install_gems.rb +40 -0
  25. data/lib/crosstie/templates/ldap.rb +42 -0
  26. data/lib/crosstie/templates/rails_layout.rb +2 -0
  27. data/lib/crosstie/templates/resources.rb +28 -0
  28. data/lib/crosstie/templates/rolify.rb +3 -0
  29. data/lib/crosstie/templates/rspec.rb +3 -0
  30. data/lib/crosstie/templates/run_test.rb +3 -0
  31. data/lib/crosstie/templates/seeds.rb +8 -0
  32. data/lib/crosstie/templates/serve_static.rb +3 -0
  33. data/lib/crosstie/templates/sidekiq.rb +47 -0
  34. data/lib/crosstie/templates/simple_form.rb +32 -0
  35. data/lib/crosstie/templates/skeleton.rb +29 -0
  36. data/lib/crosstie/templates/static_pages.rb +26 -0
  37. data/lib/crosstie/templates/stop_robots.rb +3 -0
  38. data/lib/crosstie/templates/user.rb +27 -0
  39. data/lib/crosstie/version.rb +1 -1
  40. metadata +38 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b331dbf642e5ff40082edb8ccfd2713d271d517
4
- data.tar.gz: 30adf14822e23c89a4dcd34a68667e9768d6627f
3
+ metadata.gz: 18ea44f8a3105b1eda7150116dd12605ebb4e086
4
+ data.tar.gz: a9269c5892f2b1609ca5b3489d85edcdeda353d5
5
5
  SHA512:
6
- metadata.gz: 9109a0165e72d87aea63a3acf7f9b6f45ce1fcd47a48589ef4b9a7cee46a299cd367e746afbe0512ce24d80d42c7e274a240b725c8a1146560538bbe41c22a38
7
- data.tar.gz: 402c7edce633fd09acd8c0bf36b257eb4654e664cdff1fc067084818cd5601e599b026e9d15a60f40ddd37dadb9137816047f98114380c5b8ee01518f402bf96
6
+ metadata.gz: b34d2017c6945b36305cbb56318159a44aec1075be1f7f3340ea26fd1df9df286097f8d9e315f493ce3823c29775bdae7b2d4f35387c325ca3ce5adfa1754248
7
+ data.tar.gz: 38e429278ac99c11bf2f3f2cc489c23f82c6aaed5d844a3dae844b3d12b874536664ad690bd122a5c0cd6ccf610d4e70da8a51633d6431ee8d05efbc6b141f59
@@ -0,0 +1,46 @@
1
+ def git_commit message
2
+ git add: '-A'
3
+ git commit: "-a -m '#{message}'"
4
+ end
5
+
6
+ def root
7
+ @root ||= File.expand_path File.dirname __FILE__
8
+ end
9
+
10
+ def read_template *path
11
+ File.read File.join root, "templates", *path
12
+ end
13
+
14
+ def perform task
15
+ eval File.read File.join root, "templates", "#{task}.rb"
16
+ end
17
+
18
+ perform :git_init
19
+ perform :change_source
20
+ perform :install_gems
21
+ perform :bundle_install
22
+ perform :add_gitignore
23
+ # perform :stop_robots # stop google
24
+ perform :config_timezone
25
+ perform :config_scaffold
26
+ perform :serve_static
27
+ perform :database_example
28
+ perform :figaro
29
+ perform :sidekiq
30
+ perform :simple_form
31
+ perform :rails_layout
32
+ perform :rspec
33
+ perform :guard
34
+ perform :static_pages
35
+ perform :devise
36
+ perform :skeleton
37
+ perform :user
38
+ # perform :ldap # who needs this
39
+ perform :controller_helpers
40
+ perform :authentication_token
41
+ perform :rolify
42
+ perform :authorization
43
+ perform :seeds
44
+ perform :resources
45
+ git_commit "project created"
46
+ perform :run_test
data/lib/crosstie/cli.rb CHANGED
@@ -29,7 +29,7 @@ module Crosstie
29
29
  private
30
30
 
31
31
  def template_path
32
- File.join root, "template.rb"
32
+ File.join root, "base.rb"
33
33
  end
34
34
 
35
35
  def root
@@ -0,0 +1,11 @@
1
+ # .gitignore
2
+ append_file ".gitignore", <<-EOF
3
+ dump.rdb
4
+ /config/database.yml
5
+ /config/secrets.yml
6
+ /config/ldap.yml
7
+ /config/sidekiq.yml
8
+ *.swp
9
+ /coverage
10
+ .DS_Store
11
+ EOF
@@ -0,0 +1,53 @@
1
+ # authentication token
2
+
3
+ generate "migration", "add_authentication_token_to_users", "authentication_token:string:index"
4
+ rake "db:migrate"
5
+
6
+ inject_into_file "app/models/user.rb", after: "# authentication_token\n" do
7
+ <<-EOF
8
+ before_save :ensure_authentication_token
9
+
10
+ def ensure_authentication_token
11
+ if authentication_token.blank?
12
+ self.authentication_token = generate_authentication_token
13
+ end
14
+ end
15
+
16
+ def generate_authentication_token
17
+ loop do
18
+ token = Devise.friendly_token
19
+ break token unless User.where(authentication_token: token).first
20
+ end
21
+ end
22
+ EOF
23
+ end
24
+
25
+ inject_into_file "app/controllers/application_controller.rb", after: "# authentication_token\n" do
26
+ <<-EOF
27
+ before_action :authenticate_user_from_token!
28
+
29
+ def authenticate_user_from_token!
30
+ auth_token = params[:auth_token].presence
31
+ user = auth_token && User.find_by_authentication_token(auth_token.to_s)
32
+
33
+ if user
34
+ # Notice we are passing store false, so the user is not
35
+ # actually stored in the session and a token is needed
36
+ # for every request. If you want the token to work as a
37
+ # sign in token, you can simply remove store: false.
38
+ sign_in user, store: false
39
+ end
40
+ end
41
+ EOF
42
+ end
43
+
44
+ inject_into_file "app/views/devise/registrations/edit.html.erb", before: " <%= f.submit 'Update', :class => 'button right' %>" do
45
+ <<-EOF
46
+ <fieldset>
47
+ <div class="form-group">
48
+ <%= f.label :authentication_token %>
49
+ <%= f.text_field :authentication_token, class: 'form-control', disabled: true %>
50
+ </div>
51
+ </fieldset>
52
+ EOF
53
+ end
@@ -0,0 +1,39 @@
1
+
2
+ before_action :authenticate_normal!
3
+
4
+ class AuthenticationError < SecurityError; end
5
+ class AuthorizationError < SecurityError; end
6
+
7
+ rescue_from AuthenticationError do |exception|
8
+ flash[:error] = exception.to_s
9
+ redirect_to :root
10
+ end
11
+
12
+ rescue_from AuthorizationError do |exception|
13
+ flash[:error] = exception.to_s
14
+ redirect_to :root
15
+ end
16
+
17
+ def authenticate_current_user! user
18
+ raise AuthorizationError unless current_user == user or current_user.system?
19
+ end
20
+
21
+ def authenticate_role! role, resource = nil
22
+ return unless user_signed_in?
23
+ unless current_user.has_role? role
24
+ raise AuthenticationError, "#{current_user.name} not authenticated as a #{role} user"
25
+ end
26
+ end
27
+
28
+ def authenticate_any_role! *roles
29
+ return unless user_signed_in?
30
+ unless current_user.has_any_role? *roles
31
+ raise AuthenticationError, "#{current_user.name} not authenticated as any of #{roles.join(", ")}"
32
+ end
33
+ end
34
+
35
+ Role::USER_ROLES.each do |role|
36
+ define_method "authenticate_#{role.to_s}!" do
37
+ authenticate_role! role
38
+ end
39
+ end
@@ -0,0 +1,209 @@
1
+ inject_into_file "app/models/user.rb", after: "# authorization\n" do
2
+ <<-EOF
3
+
4
+ def managing_roles
5
+ roles = []
6
+ roles += [:system, :admin] if has_role? :system
7
+ roles += [:normal] if has_role? :admin
8
+ roles.uniq
9
+ end
10
+ EOF
11
+ end
12
+
13
+ inject_into_file "app/controllers/application_controller.rb", after: "# authorization\n" do
14
+ <<-EOF
15
+
16
+ before_action :authenticate_normal!
17
+
18
+ class AuthenticationError < SecurityError; end
19
+ class AuthorizationError < SecurityError; end
20
+
21
+ rescue_from AuthenticationError do |exception|
22
+ flash[:error] = exception.to_s
23
+ redirect_to :root
24
+ end
25
+
26
+ rescue_from AuthorizationError do |exception|
27
+ flash[:error] = exception.to_s
28
+ redirect_to :root
29
+ end
30
+
31
+ def authenticate_current_user! user
32
+ raise AuthorizationError unless current_user == user or current_user.system?
33
+ end
34
+
35
+ def authenticate_role! role, resource = nil
36
+ return unless user_signed_in?
37
+ unless current_user.has_role? role
38
+ raise AuthenticationError, "\#{current_user.name} not authenticated as a \#{role} user"
39
+ end
40
+ end
41
+
42
+ def authenticate_any_role! *roles
43
+ return unless user_signed_in?
44
+ unless current_user.has_any_role? *roles
45
+ raise AuthenticationError, "\#{current_user.name} not authenticated as any of \#{roles.join(", ")}"
46
+ end
47
+ end
48
+
49
+ Role::USER_ROLES.each do |role|
50
+ define_method "authenticate_\#{role.to_s}!" do
51
+ authenticate_role! role
52
+ end
53
+ end
54
+ EOF
55
+ end
56
+
57
+ inject_into_file "app/models/role.rb", after: "scopify\n" do
58
+ <<-EOF
59
+ OPERATIONS = [:grant, :revoke]
60
+ USER_ROLES = [:system, :admin, :normal]
61
+ EOF
62
+ end
63
+ gsub_file "spec/support/devise.rb", "role = :user", "role = :system", force: true
64
+ inject_into_file "config/routes.rb", after: "Rails.application.routes.draw do\n" do
65
+ <<-EOF
66
+ resources :users, only: [:index] do
67
+ member do
68
+ put :role
69
+ end
70
+ end
71
+ EOF
72
+ end
73
+ inject_into_file "spec/factories/users.rb", after: "factory :user do\n" do
74
+ <<-EOF
75
+ factory :normal do
76
+ after(:create) do |user|
77
+ user.grant :normal
78
+ end
79
+ end
80
+
81
+ factory :admin do
82
+ after(:create) do |user|
83
+ user.grant :normal
84
+ user.grant :admin
85
+ end
86
+ end
87
+
88
+ factory :system do
89
+ after(:create) do |user|
90
+ user.grant :normal
91
+ user.grant :system
92
+ end
93
+ end
94
+ EOF
95
+ end
96
+ create_file "app/controllers/users_controller.rb", <<-EOF
97
+ class UsersController < ApplicationController
98
+
99
+ skip_before_action :authenticate_admin!, only: [:index, :role]
100
+
101
+ def index
102
+ authenticate_any_role! :system, :admin
103
+ @users = User.all
104
+ @roles = current_user.managing_roles
105
+ end
106
+
107
+ def role
108
+ authenticate_any_role! :system, :admin
109
+ @user = User.find params[:id]
110
+ operation, role = params[:operation].to_sym, params[:role].to_sym
111
+
112
+ raise "role operation \#{operation} undefined" unless operation.to_sym.in? Role::OPERATIONS
113
+ raise "user role \#{role} undefined" unless role.to_sym.in? Role::USER_ROLES
114
+ raise "current user not in charge of \#{role}" unless role.to_sym.in? current_user.managing_roles
115
+ @user.send operation, role
116
+ redirect_back :root, notice: "User \#{@user.name} was \#{operation}ed role \#{role}"
117
+ rescue => exc
118
+ redirect_back :root, notice: exc.to_s
119
+ end
120
+ end
121
+ EOF
122
+ create_file "spec/controllers/users_controller_spec.rb", <<-EOF
123
+ require 'rails_helper'
124
+
125
+ RSpec.describe UsersController, :type => :controller do
126
+
127
+ let(:valid_session) { { } }
128
+
129
+ describe "GET index" do
130
+ it "redirect normal users" do
131
+ @user = sign_in_user :normal
132
+ get :index, {}, valid_session
133
+ expect(response).to redirect_to :root
134
+ end
135
+
136
+ it "assigns all users as @users" do
137
+ @user = sign_in_user :admin
138
+ get :index, {}, valid_session
139
+ expect(assigns(:users)).to eq [@user]
140
+ expect(assigns(:roles)).to eq [:normal]
141
+ end
142
+
143
+ it "assigns all users as @users" do
144
+ @user = sign_in_user :system
145
+ get :index, {}, valid_session
146
+ expect(assigns(:users)).to eq [@user]
147
+ expect(assigns(:roles)).to eq [:system, :admin]
148
+ end
149
+ end
150
+
151
+ describe "PUT role" do
152
+
153
+ describe "normal users" do
154
+
155
+ it "redirect normal users" do
156
+ sign_in_user :normal
157
+ user = FactoryGirl.create :normal
158
+ operation = :grant
159
+ role = :normal
160
+ put :role, {:id => user.to_param, :operation => operation, :role => role}, valid_session
161
+ expect(response).to redirect_to(:root)
162
+ end
163
+ end
164
+
165
+ describe "system users" do
166
+
167
+ describe "global roles" do
168
+ it "grant role to user" do
169
+ sign_in_user :system
170
+ user = FactoryGirl.create :system
171
+
172
+ expect(user).to_not be_has_role :admin
173
+ put :role, {:id => user.to_param, :operation => :grant, :role => :admin}, valid_session
174
+ expect(assigns(:user)).to be_has_role :admin
175
+ end
176
+
177
+ it "revoke role from user" do
178
+ sign_in_user :system
179
+ user = FactoryGirl.create :user
180
+ user.grant 'admin'
181
+
182
+ expect(user).to be_has_role :admin
183
+ put :role, {:id => user.to_param, :operation => :revoke, :role => :admin}, valid_session
184
+ expect(assigns(:user)).to_not be_has_role :admin
185
+ end
186
+ end
187
+ end
188
+ end
189
+ end
190
+ EOF
191
+ create_file "app/views/users/index.html.slim", <<-EOF
192
+ table.table
193
+ tr
194
+ th Username
195
+ - @roles.each do |role|
196
+ th = role.to_s.titleize
197
+ end
198
+ - @users.each do |user|
199
+ tr
200
+ td = user.username
201
+ - @roles.each do |role|
202
+ td
203
+ = form_for user, url: role_user_path(user), method: :put do |f|
204
+ - operation, activation, btn_class = user.has_role?(role) ? \
205
+ %w(revoke active btn-success) : %w(grant inactive btn-danger)
206
+ = hidden_field_tag :operation, operation
207
+ = hidden_field_tag :role, role
208
+ = f.submit activation, class: "btn \#{btn_class}"
209
+ EOF
@@ -0,0 +1,17 @@
1
+ table.table
2
+ tr
3
+ th Username
4
+ - @roles.each do |role|
5
+ th = role.to_s.titleize
6
+ end
7
+ - @users.each do |user|
8
+ tr
9
+ td = user.username
10
+ - @roles.each do |role|
11
+ td
12
+ = form_for user, url: role_user_path(user), method: :put do |f|
13
+ - operation, activation, btn_class = user.has_role?(role) ? \
14
+ %w(revoke active btn-success) : %w(grant inactive btn-danger)
15
+ = hidden_field_tag :operation, operation
16
+ = hidden_field_tag :role, role
17
+ = f.submit activation, class: "btn \#{btn_class}"
@@ -0,0 +1,19 @@
1
+ factory :normal do
2
+ after(:create) do |user|
3
+ user.grant :normal
4
+ end
5
+ end
6
+
7
+ factory :admin do
8
+ after(:create) do |user|
9
+ user.grant :normal
10
+ user.grant :admin
11
+ end
12
+ end
13
+
14
+ factory :system do
15
+ after(:create) do |user|
16
+ user.grant :normal
17
+ user.grant :system
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+ class UsersController < ApplicationController
2
+
3
+ skip_before_action :authenticate_normal!, only: [:index, :role]
4
+ skip_before_action :authenticate_admin!, only: [:index, :role]
5
+
6
+ def index
7
+ authenticate_any_role! :system, :admin
8
+ @users = User.all
9
+ @roles = current_user.managing_roles
10
+ end
11
+
12
+ def role
13
+ authenticate_any_role! :system, :admin
14
+ @user = User.find params[:id]
15
+ operation, role = params[:operation].to_sym, params[:role].to_sym
16
+
17
+ raise "role operation #{operation} undefined" unless operation.to_sym.in? Role::OPERATIONS
18
+ raise "user role #{role} undefined" unless role.to_sym.in? Role::USER_ROLES
19
+ raise "current user not in charge of #{role}" unless role.to_sym.in? current_user.managing_roles
20
+ @user.send operation, role
21
+ redirect_back :root, notice: "User #{@user.name} was #{operation}ed role #{role}"
22
+ rescue => exc
23
+ redirect_back :root, notice: exc.to_s
24
+ end
25
+ end
@@ -0,0 +1,67 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe UsersController, :type => :controller do
4
+
5
+ let(:valid_session) { { } }
6
+
7
+ describe "GET index" do
8
+ it "redirect normal users" do
9
+ @user = sign_in_user :normal
10
+ get :index, {}, valid_session
11
+ expect(response).to redirect_to :root
12
+ end
13
+
14
+ it "assigns all users as @users" do
15
+ @user = sign_in_user :admin
16
+ get :index, {}, valid_session
17
+ expect(assigns(:users)).to eq [@user]
18
+ expect(assigns(:roles)).to eq [:normal]
19
+ end
20
+
21
+ it "assigns all users as @users" do
22
+ @user = sign_in_user :system
23
+ get :index, {}, valid_session
24
+ expect(assigns(:users)).to eq [@user]
25
+ expect(assigns(:roles)).to eq [:system, :admin]
26
+ end
27
+ end
28
+
29
+ describe "PUT role" do
30
+
31
+ describe "normal users" do
32
+
33
+ it "redirect normal users" do
34
+ sign_in_user :normal
35
+ user = FactoryGirl.create :normal
36
+ operation = :grant
37
+ role = :normal
38
+ put :role, {:id => user.to_param, :operation => operation, :role => role}, valid_session
39
+ expect(response).to redirect_to(:root)
40
+ end
41
+ end
42
+
43
+ describe "system users" do
44
+
45
+ describe "global roles" do
46
+ it "grant role to user" do
47
+ sign_in_user :system
48
+ user = FactoryGirl.create :system
49
+
50
+ expect(user).to_not be_has_role :admin
51
+ put :role, {:id => user.to_param, :operation => :grant, :role => :admin}, valid_session
52
+ expect(assigns(:user)).to be_has_role :admin
53
+ end
54
+
55
+ it "revoke role from user" do
56
+ sign_in_user :system
57
+ user = FactoryGirl.create :user
58
+ user.grant 'admin'
59
+
60
+ expect(user).to be_has_role :admin
61
+ put :role, {:id => user.to_param, :operation => :revoke, :role => :admin}, valid_session
62
+ expect(assigns(:user)).to_not be_has_role :admin
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,47 @@
1
+ inject_into_file "app/models/user.rb", after: "# authorization\n" do
2
+ <<-EOF
3
+
4
+ def managing_roles
5
+ roles = []
6
+ roles += [:system, :admin] if has_role? :system
7
+ roles += [:normal] if has_role? :admin
8
+ roles.uniq
9
+ end
10
+ EOF
11
+ end
12
+
13
+ inject_into_file "app/controllers/application_controller.rb", after: "# authorization\n" do
14
+ read_template "authorization/application_controller.rb"
15
+ end
16
+
17
+ inject_into_file "app/models/role.rb", after: "scopify\n" do
18
+ <<-EOF
19
+ OPERATIONS = [:grant, :revoke]
20
+ USER_ROLES = [:system, :admin, :normal]
21
+ EOF
22
+ end
23
+
24
+ gsub_file "spec/support/devise.rb", "role = :user", "role = :system", force: true
25
+
26
+ inject_into_file "config/routes.rb", after: "Rails.application.routes.draw do\n" do
27
+ <<-EOF
28
+ resources :users, only: [:index] do
29
+ member do
30
+ put :role
31
+ end
32
+ end
33
+ EOF
34
+ end
35
+
36
+ inject_into_file "spec/factories/users.rb", after: "factory :user do\n" do
37
+ read_template "authorization/users.rb"
38
+ end
39
+
40
+ create_file "app/controllers/users_controller.rb",
41
+ read_template("authorization/users_controller.rb")
42
+
43
+ create_file "spec/controllers/users_controller_spec.rb",
44
+ read_template("authorization/users_controller_spec.rb")
45
+
46
+ create_file "app/views/users/index.html.slim",
47
+ read_template("authorization/index.html.slim")
@@ -0,0 +1,2 @@
1
+ run "bundle install -V"
2
+ # run "bundle install --local" # for development
@@ -1,5 +1 @@
1
- git :init
2
- git add: '.'
3
- git commit: "-a -m 'rails new #{app_path}'"
4
-
5
1
  gsub_file "Gemfile", "https://rubygems.org", "https://ruby.taobao.org"
@@ -0,0 +1,3 @@
1
+ # application.rb
2
+ # change timezone to beijing
3
+ gsub_file "config/application.rb", "# config.time_zone = 'Central Time (US & Canada)'", "config.time_zone = 'Beijing'"
@@ -0,0 +1,12 @@
1
+ # application.rb
2
+ # stop scaffold from generating css, js, helper, test for helper, test for views
3
+ inject_into_file "config/application.rb", after: "# config.i18n.default_locale = :de\n" do
4
+ <<-EOS
5
+ config.generators do |g|
6
+ g.stylesheets false
7
+ g.javascripts false
8
+ g.helper false
9
+ g.test_framework :rspec, view_specs: false, request_specs: false
10
+ end
11
+ EOS
12
+ end
@@ -0,0 +1,3 @@
1
+ # application.rb
2
+ # change timezone to beijing
3
+ gsub_file "config/application.rb", "# config.time_zone = 'Central Time (US & Canada)'", "config.time_zone = 'Beijing'"
@@ -0,0 +1,26 @@
1
+ inject_into_file "app/controllers/application_controller.rb", after: "# controller_helpers\n" do
2
+ <<-EOF
3
+ skip_before_action :verify_authenticity_token, if: :skip_authenticity?
4
+ before_action :authenticate_user!
5
+ before_action :configure_permitted_parameters, if: :devise_controller?
6
+ after_action :log_current_user
7
+
8
+ def log_current_user
9
+ logger.info "Current user: \#{current_user.email}" if current_user
10
+ end
11
+
12
+ def redirect_back default_path = :root, options = {}
13
+ redirect_to :back, options
14
+ rescue ActionController::RedirectBackError
15
+ redirect_to default_path, options
16
+ end
17
+
18
+ def configure_permitted_parameters
19
+ devise_parameter_sanitizer.for(:sign_up) << :email
20
+ end
21
+
22
+ def skip_authenticity?
23
+ request.format.json? or params[:skip_authenticity]
24
+ end
25
+ EOF
26
+ end
@@ -0,0 +1,2 @@
1
+ # database config example
2
+ run "cp config/database.yml config/database.yml.example"
@@ -0,0 +1,82 @@
1
+ # devise
2
+ generate "devise:install"
3
+
4
+ # improve password strength
5
+ gsub_file "config/initializers/devise.rb", "config.password_length = 8..128", "config.password_length = 4..128"
6
+
7
+ # generate "devise:views" # taken over by rails_layout
8
+ generate "devise", "user"
9
+ rake "db:migrate"
10
+
11
+ prepend_file "spec/rails_helper.rb", <<-EOF
12
+ require 'simplecov'
13
+ SimpleCov.start
14
+ EOF
15
+
16
+ inject_into_file "spec/rails_helper.rb", after: "# Dir[Rails.root.join(\"spec/support/**/*.rb\")].each { |f| require f }\n" do
17
+ <<-EOF
18
+ Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } # since rspec 3.1
19
+ EOF
20
+ end
21
+
22
+ inject_into_file "spec/rails_helper.rb", after: "RSpec.configure do |config|\n" do
23
+ <<-EOF
24
+ config.before(:suite) do
25
+ DatabaseCleaner.strategy = :transaction
26
+ DatabaseCleaner.clean_with(:truncation)
27
+ end
28
+
29
+ config.around(:each) do |example|
30
+ DatabaseCleaner.cleaning do
31
+ example.run
32
+ end
33
+ end
34
+ EOF
35
+ end
36
+
37
+ create_file "spec/support/devise.rb", <<-EOF
38
+ module ValidUserControllerHelper
39
+ def sign_in_user role = :user
40
+ @user ||= FactoryGirl.create role
41
+ sign_in :user, @user
42
+ @user
43
+ end
44
+ end
45
+
46
+ RSpec.configure do |config|
47
+ config.include Devise::TestHelpers, :type => :controller
48
+ config.include Devise::TestHelpers, :type => :view
49
+ config.include ValidUserControllerHelper, :type => :controller
50
+ config.include ValidUserControllerHelper, :type => :view
51
+ end
52
+
53
+ # This support package contains modules for authenticaiting
54
+ # devise users for request specs.
55
+
56
+ # This module authenticates users for request specs.#
57
+ module ValidUserRequestHelper
58
+ # Define a method which signs in as a valid user.
59
+ def sign_in_user role = :user
60
+ # ASk factory girl to generate a valid user for us.
61
+ @user ||= FactoryGirl.create role
62
+
63
+ # We action the login request using the parameters before we begin.
64
+ # The login requests will match these to the user we just created in the factory, and authenticate us.
65
+ post_via_redirect user_session_path, 'user[username]' => @user.username, 'user[password]' => @user.password
66
+ end
67
+ end
68
+
69
+ # Configure these to modules as helpers in the appropriate tests.
70
+ RSpec.configure do |config|
71
+ # Include the help for the request specs.
72
+ config.include ValidUserRequestHelper, :type => :request
73
+ end
74
+ EOF
75
+ inject_into_file "spec/factories/users.rb", after: "factory :user do\n" do
76
+ <<-EOF
77
+ sequence(:username) { |n| "test\#{n}" }
78
+ sequence(:email) { |n| "test\#{n}@exampl.com" }
79
+ password "password"
80
+ password_confirmation "password"
81
+ EOF
82
+ end
@@ -0,0 +1,3 @@
1
+ # figaro
2
+ run "bundle exec figaro install"
3
+ run "cp config/application.yml config/application.yml.example"
@@ -0,0 +1,3 @@
1
+ git :init
2
+ git add: '.'
3
+ git commit: "-a -m 'rails new #{app_path}'"
@@ -0,0 +1,3 @@
1
+ # guard
2
+ run "bundle exec guard init rspec"
3
+ inject_into_file "Guardfile", ", cmd: 'spring rspec'", after: ":rspec"
@@ -0,0 +1,40 @@
1
+ gem 'slim-rails'
2
+ gem 'therubyracer'
3
+ gem 'figaro'
4
+ gem 'bootstrap-sass'
5
+ gem 'simple_form'
6
+ gem 'quiet_assets'
7
+ gem 'kaminari'
8
+ gem 'rest-client'
9
+ gem 'puma'
10
+ gem 'mysql2'
11
+
12
+ gem 'devise'
13
+ gem 'devise_ldap_authenticatable'
14
+ gem 'rolify'
15
+
16
+ gem 'sidekiq'
17
+ gem 'sinatra'
18
+
19
+ gem_group :development do
20
+ gem 'better_errors'
21
+ gem 'binding_of_caller'
22
+ gem 'rack-mini-profiler'
23
+ gem 'rails_layout'
24
+ gem 'annotate'
25
+ end
26
+
27
+ gem_group :development, :test do
28
+ gem 'spring-commands-rspec'
29
+ gem 'rspec-rails'
30
+ gem 'guard-rspec'
31
+ gem 'factory_girl_rails'
32
+ end
33
+
34
+ gem_group :test do
35
+ gem 'shoulda'
36
+ gem 'database_cleaner'
37
+
38
+ gem 'simplecov', require: false
39
+ gem 'test_after_commit'
40
+ end
@@ -0,0 +1,42 @@
1
+ # devise_ldap_authenticatable"
2
+
3
+ generate "devise_ldap_authenticatable:install"
4
+ run "cp config/ldap.yml config/ldap.yml.bak"
5
+
6
+ gsub_file "app/models/user.rb", " devise :ldap_authenticatable, :registerable,", ""
7
+ gsub_file "app/models/user.rb", " :recoverable, :rememberable, :trackable, :validatable", ""
8
+
9
+ inject_into_file "app/models/user.rb", after: "# ldap\n" do
10
+ <<-EOF
11
+
12
+ unless Rails.env.production?
13
+ devise :database_authenticatable, :rememberable, :trackable, :registerable# , :recoverable, :validatable
14
+ else
15
+ devise :ldap_authenticatable, :rememberable, :trackable, :registerable# , :recoverable, :validatable
16
+
17
+ before_validation :get_ldap_email, :get_ldap_id
18
+
19
+ def get_ldap_email
20
+ self.email = Devise::LDAP::Adapter.get_ldap_param(self.username,"mail").first
21
+ end
22
+
23
+ def get_ldap_id
24
+ self.id = Devise::LDAP::Adapter.get_ldap_param(self.username,"uidnumber").first
25
+ end
26
+
27
+ # hack for remember_token
28
+ def authenticatable_salt
29
+ Digest::SHA1.hexdigest(email)[0,29]
30
+ end
31
+ end
32
+ EOF
33
+ end
34
+
35
+ inject_into_file "config/initializers/devise.rb", after: "# ==> LDAP Configuration \n" do
36
+ <<-EOF
37
+ config.ldap_logger = true
38
+ config.ldap_create_user = true
39
+ config.ldap_update_password = true
40
+ config.ldap_use_admin_to_bind = true
41
+ EOF
42
+ end
@@ -0,0 +1,2 @@
1
+ generate 'layout:install', 'bootstrap3', '--force'
2
+ generate 'layout:devise', 'bootstrap3'
@@ -0,0 +1,28 @@
1
+ # scaffold resources
2
+ {
3
+ "article" => [
4
+ "name:string",
5
+ "content:text",
6
+ "published:boolean",
7
+ ],
8
+ "comment" => [
9
+ "article:references",
10
+ "content:text",
11
+ "kind:string",
12
+ ],
13
+ }.each do |resource, fields|
14
+ generate "scaffold", resource, *fields
15
+ rake "db:migrate"
16
+ inject_into_file "spec/controllers/#{resource.tableize}_controller_spec.rb", after: "RSpec.describe #{resource.pluralize.camelize}Controller, :type => :controller do\n" do
17
+ <<-EOF
18
+
19
+ before { sign_in_user }
20
+ EOF
21
+ end
22
+
23
+ gsub_file "spec/controllers/#{resource.tableize}_controller_spec.rb",
24
+ 'skip("Add a hash of attributes valid for your model")',
25
+ "FactoryGirl.build(:#{resource}).attributes"
26
+ end
27
+
28
+ run "bundle exec annotate"
@@ -0,0 +1,3 @@
1
+ # rolify
2
+ generate "rolify", "Role", "User"
3
+ rake "db:migrate"
@@ -0,0 +1,3 @@
1
+ # rspect
2
+ generate "rspec:install"
3
+ gsub_file ".rspec", "--warnings\n", ""
@@ -0,0 +1,3 @@
1
+ # run tests
2
+
3
+ run 'bundle exec rspec'
@@ -0,0 +1,8 @@
1
+ append_file "db/seeds.rb", <<-EOF
2
+ user = User.create! username: "admin", email: "admin@example.com", password: "password"
3
+ Role::USER_ROLES.each do |role|
4
+ user.grant role
5
+ end
6
+ puts "sign in with:\n\tusername: admin\n\tpassword: password"
7
+ EOF
8
+ rake "db:seed"
@@ -0,0 +1,3 @@
1
+ # production.rb
2
+ # serve static files
3
+ gsub_file "config/environments/production.rb", "config.serve_static_assets = false", "config.serve_static_assets = true"
@@ -0,0 +1,47 @@
1
+ # sidekiq
2
+ append_file "config/application.yml", <<-EOF
3
+ REDIS_HOST: localhost
4
+ REDIS_PORT: "6379"
5
+ EOF
6
+ run "cp config/application.yml config/application.yml.example"
7
+ create_file "app/workers/hello_worker.rb", <<-EOF
8
+ class HelloWorker
9
+ include Sidekiq::Worker
10
+ sidekiq_options queue: :default
11
+
12
+ def perform msg
13
+ puts msg
14
+ end
15
+ end
16
+ EOF
17
+ create_file "config/initializers/sidekiq.rb", <<-EOF
18
+ Sidekiq.configure_server do |config|
19
+ config.redis = { :url => "redis://\#{ENV['REDIS_HOST']}:\#{ENV['REDIS_PORT']}/0", :namespace => '#{app_path}' }
20
+ end
21
+
22
+ Sidekiq.configure_client do |config|
23
+ config.redis = { :url => "redis://\#{ENV['REDIS_HOST']}:\#{ENV['REDIS_PORT']}/0", :namespace => '#{app_path}' }
24
+ end
25
+ EOF
26
+ create_file "tmp/pids/.keep", ""
27
+ create_file "config/sidekiq.yml", <<-EOF
28
+ ---
29
+ :verbose: true
30
+ :pidfile: ./tmp/pids/sidekiq.pid
31
+ :logfile: ./log/sidekiq.log
32
+ :queues:
33
+ - default
34
+ development:
35
+ :concurrency: 1
36
+ production:
37
+ :concurrency: 2
38
+ EOF
39
+ inject_into_file "config/routes.rb", after: "Rails.application.routes.draw do\n" do
40
+ <<-EOF
41
+ require 'sidekiq/web'
42
+ authenticate :user do
43
+ mount Sidekiq::Web => '/sidekiq'
44
+ end
45
+ EOF
46
+ end
47
+ run "cp config/sidekiq.yml config/sidekiq.yml.example"
@@ -0,0 +1,32 @@
1
+ # simple_form
2
+ generate 'simple_form:install --bootstrap'
3
+
4
+ # default horizontal form
5
+ {
6
+ 'config.default_wrapper = :vertical_form' =>
7
+ 'config.default_wrapper = :horizontal_form',
8
+ 'check_boxes: :vertical_radio_and_checkboxes,' =>
9
+ 'check_boxes: :horizontal_radio_and_checkboxes,',
10
+ 'radio_buttons: :vertical_radio_and_checkboxes,' =>
11
+ 'radio_buttons: :horizontal_radio_and_checkboxes,',
12
+ 'file: :vertical_file_input,' =>
13
+ 'file: :horizontal_file_input,',
14
+ 'boolean: :vertical_boolean,' =>
15
+ 'boolean: :horizontal_boolean,',
16
+ }.each do |from, to|
17
+ gsub_file "lib/templates/slim/scaffold/_form.html.slim", from, to
18
+ end
19
+
20
+ #remove_file "lib/templates/slim/scaffold/_form.html.slim"
21
+ #create_file "lib/templates/slim/scaffold/_form.html.slim", <<-EOF
22
+ #= simple_form_for(@<%= singular_table_name %>, html: { class: 'form-horizontal' }, wrapper: :horizontal_form, wrapper_mappings: { check_boxes: :horizontal_radio_and_checkboxes, radio_buttons: :horizontal_radio_and_checkboxes, file: :horizontal_file_input, boolean: :horizontal_boolean }) do |f|
23
+ # = f.error_notification
24
+ #
25
+ # .form-inputs
26
+ #<%- attributes.each do |attribute| -%>
27
+ # = f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %>
28
+ #<%- end -%>
29
+ #
30
+ # .form-actions
31
+ # = f.button :submit
32
+ #EOF
@@ -0,0 +1,29 @@
1
+ # user.rb
2
+ inject_into_file "app/models/user.rb", before: 'end' do
3
+ <<-EOF
4
+
5
+ # crosstie skeleton
6
+
7
+ # ldap
8
+
9
+ # username
10
+
11
+ # authentication_token
12
+
13
+ # authorization
14
+
15
+ EOF
16
+ end
17
+
18
+ # application_controller.rb
19
+ inject_into_file "app/controllers/application_controller.rb", before: 'end' do
20
+ <<-EOF
21
+
22
+ # controller_helpers
23
+
24
+ # authentication_token
25
+
26
+ # authorization
27
+
28
+ EOF
29
+ end
@@ -0,0 +1,26 @@
1
+ # static pages
2
+ generate "controller", "static_pages", "home", "status"
3
+ inject_into_file "app/controllers/static_pages_controller.rb", after: "class StaticPagesController < ApplicationController\n" do
4
+ <<-EOF
5
+ skip_before_action :authenticate_user!, only: [:home, :status]
6
+ skip_before_action :authenticate_normal!, only: [:home, :status]
7
+ EOF
8
+ end
9
+ inject_into_file "app/controllers/static_pages_controller.rb", after: "def status\n" do
10
+ <<-EOF
11
+ render json: {
12
+ status: "ok",
13
+ hostname: Socket.gethostname,
14
+ service: "#{app_path}",
15
+ commit: @@comment ||= `git log -1 --oneline`
16
+ }
17
+ EOF
18
+ end
19
+ gsub_file "config/routes.rb", "get 'static_pages/home'", "root to: 'static_pages#home'"
20
+ gsub_file "config/routes.rb", "get 'static_pages/status'", "get '/status' => 'static_pages#status'"
21
+ inject_into_file "spec/controllers/static_pages_controller_spec.rb", after: "RSpec.describe StaticPagesController, :type => :controller do\n" do
22
+ <<-EOF
23
+
24
+ before { sign_in_user }
25
+ EOF
26
+ end
@@ -0,0 +1,3 @@
1
+ # robotes.txt
2
+ gsub_file "public/robots.txt", "# User-agent: *", "User-agent: *"
3
+ gsub_file "public/robots.txt", "# Disallow: /", "Disallow: /"
@@ -0,0 +1,27 @@
1
+ # add username to users
2
+ inject_into_file "app/models/user.rb", after: "# username\n" do
3
+ <<-EOF
4
+ alias_attribute :name, :username
5
+ EOF
6
+ end
7
+
8
+ generate "migration", "add_username_to_users", "username:string:index"
9
+ rake "db:migrate"
10
+
11
+ gsub_file "app/views/devise/sessions/new.html.erb", ":email", ":username"
12
+
13
+ [
14
+ "app/views/devise/registrations/new.html.erb",
15
+ "app/views/devise/registrations/edit.html.erb",
16
+ ].each do |file|
17
+ inject_into_file file, after: "<%= devise_error_messages! %>\n" do
18
+ <<-EOF
19
+ <div class="form-group">
20
+ <%= f.label :username %>
21
+ <%= f.text_field :username, class: 'form-control' %>
22
+ </div>
23
+ EOF
24
+ end
25
+ end
26
+
27
+ gsub_file "config/initializers/devise.rb", "# config.authentication_keys = [ :email ]", "config.authentication_keys = [ :username ]"
@@ -1,3 +1,3 @@
1
1
  module Crosstie
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crosstie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dong Qingshan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-09 00:00:00.000000000 Z
11
+ date: 2014-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -68,8 +68,43 @@ files:
68
68
  - bin/crosstie
69
69
  - crosstie.gemspec
70
70
  - lib/crosstie.rb
71
+ - lib/crosstie/base.rb
71
72
  - lib/crosstie/cli.rb
72
- - lib/crosstie/template.rb
73
+ - lib/crosstie/templates/add_gitignore.rb
74
+ - lib/crosstie/templates/authentication_token.rb
75
+ - lib/crosstie/templates/authorization.rb
76
+ - lib/crosstie/templates/authorization/application_controller.rb
77
+ - lib/crosstie/templates/authorization/authorization.rb
78
+ - lib/crosstie/templates/authorization/index.html.slim
79
+ - lib/crosstie/templates/authorization/users.rb
80
+ - lib/crosstie/templates/authorization/users_controller.rb
81
+ - lib/crosstie/templates/authorization/users_controller_spec.rb
82
+ - lib/crosstie/templates/bundle_install.rb
83
+ - lib/crosstie/templates/change_source.rb
84
+ - lib/crosstie/templates/change_timezone.rb
85
+ - lib/crosstie/templates/config_scaffold.rb
86
+ - lib/crosstie/templates/config_timezone.rb
87
+ - lib/crosstie/templates/controller_helpers.rb
88
+ - lib/crosstie/templates/database_example.rb
89
+ - lib/crosstie/templates/devise.rb
90
+ - lib/crosstie/templates/figaro.rb
91
+ - lib/crosstie/templates/git_init.rb
92
+ - lib/crosstie/templates/guard.rb
93
+ - lib/crosstie/templates/install_gems.rb
94
+ - lib/crosstie/templates/ldap.rb
95
+ - lib/crosstie/templates/rails_layout.rb
96
+ - lib/crosstie/templates/resources.rb
97
+ - lib/crosstie/templates/rolify.rb
98
+ - lib/crosstie/templates/rspec.rb
99
+ - lib/crosstie/templates/run_test.rb
100
+ - lib/crosstie/templates/seeds.rb
101
+ - lib/crosstie/templates/serve_static.rb
102
+ - lib/crosstie/templates/sidekiq.rb
103
+ - lib/crosstie/templates/simple_form.rb
104
+ - lib/crosstie/templates/skeleton.rb
105
+ - lib/crosstie/templates/static_pages.rb
106
+ - lib/crosstie/templates/stop_robots.rb
107
+ - lib/crosstie/templates/user.rb
73
108
  - lib/crosstie/version.rb
74
109
  homepage: https://github.com/dongqs/crosstie
75
110
  licenses: