rails_app_generator 0.2.22 → 0.2.25

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +24 -0
  3. data/after_templates/addons/devise/_.rb +43 -0
  4. data/after_templates/addons/devise/app/controllers/home_controller.rb +4 -0
  5. data/after_templates/addons/devise/app/controllers/posts_controller.rb +81 -0
  6. data/after_templates/addons/devise/app/models/post.rb +11 -0
  7. data/after_templates/addons/devise/app/models/user.rb +17 -0
  8. data/after_templates/addons/devise/app/views/home/index.html.erb +40 -0
  9. data/after_templates/addons/devise/app/views/layouts/_footer.html.erb +1 -0
  10. data/after_templates/addons/devise/app/views/layouts/_navbar.html.erb +10 -0
  11. data/after_templates/addons/devise/app/views/layouts/application.html.erb +38 -0
  12. data/after_templates/addons/devise/app/views/posts/_form.html.erb +27 -0
  13. data/after_templates/addons/devise/app/views/posts/_post.html.erb +14 -0
  14. data/after_templates/addons/devise/app/views/posts/index.html.erb +12 -0
  15. data/after_templates/addons/devise/app/views/posts/show.html.erb +8 -0
  16. data/after_templates/addons/devise/db/seeds.rb +11 -0
  17. data/after_templates/addons/devise_masquerade/_.rb +36 -0
  18. data/after_templates/addons/devise_masquerade/app/controllers/application_controller.rb +3 -0
  19. data/after_templates/addons/devise_masquerade/app/controllers/home_controller.rb +4 -0
  20. data/after_templates/addons/devise_masquerade/app/controllers/users/masquerades_controller.rb +10 -0
  21. data/after_templates/addons/devise_masquerade/app/models/user.rb +17 -0
  22. data/after_templates/addons/devise_masquerade/app/views/home/index.html.erb +66 -0
  23. data/after_templates/addons/devise_masquerade/app/views/layouts/_footer.html.erb +1 -0
  24. data/after_templates/addons/devise_masquerade/app/views/layouts/_navbar.html.erb +6 -0
  25. data/after_templates/addons/devise_masquerade/app/views/layouts/application.html.erb +57 -0
  26. data/after_templates/addons/devise_masquerade/db/seeds.rb +3 -0
  27. data/after_templates/addons/minimal_css/_.rb +62 -0
  28. data/after_templates/addons/minimal_css/app/controllers/home_controller.rb +4 -0
  29. data/after_templates/addons/minimal_css/app/views/home/index.html.erb +140 -0
  30. data/after_templates/addons/minimal_css/app/views/layouts/_footer.html.erb +1 -0
  31. data/after_templates/addons/minimal_css/app/views/layouts/_navbar.html.erb +3 -0
  32. data/after_templates/addons/minimal_css/app/views/layouts/application.html.erb +29 -0
  33. data/after_templates/addons/minimal_css/db/seeds.rb +5 -0
  34. data/after_templates/rag/devise/_.rb +14 -17
  35. data/after_templates/rag/devise/app/controllers/home_controller.rb +4 -0
  36. data/after_templates/rag/devise/{post → app/controllers}/posts_controller.rb +0 -0
  37. data/after_templates/rag/devise/{post → app/models}/post.rb +0 -0
  38. data/after_templates/rag/devise/app/views/home/index.html.erb +3 -0
  39. data/after_templates/rag/devise/app/views/layouts/_footer.html.erb +3 -0
  40. data/after_templates/rag/devise/app/views/layouts/_navbar.html.erb +8 -0
  41. data/after_templates/rag/devise/app/views/layouts/application.html.erb +24 -0
  42. data/after_templates/rag/devise/{post → app/views/post}/_post.html.erb +0 -0
  43. data/after_templates/rag/test/_.rb +64 -0
  44. data/after_templates/rag/test/app/controllers/home_controller.rb +4 -0
  45. data/after_templates/rag/test/app/views/home/index.html.erb +3 -0
  46. data/after_templates/rag/test/app/views/layouts/_footer.html.erb +1 -0
  47. data/after_templates/rag/test/app/views/layouts/_navbar.html.erb +5 -0
  48. data/after_templates/rag/test/app/views/layouts/application.html.erb +29 -0
  49. data/after_templates/rag/test/db/seeds.rb +7 -0
  50. data/after_templates/rag/testy/_.rb +64 -0
  51. data/after_templates/rag/testy/app/controllers/home_controller.rb +4 -0
  52. data/after_templates/rag/testy/app/views/home/index.html.erb +3 -0
  53. data/after_templates/rag/testy/app/views/layouts/_footer.html.erb +1 -0
  54. data/after_templates/rag/testy/app/views/layouts/_navbar.html.erb +5 -0
  55. data/after_templates/rag/testy/app/views/layouts/application.html.erb +29 -0
  56. data/after_templates/rag/testy/db/seeds.rb +7 -0
  57. data/docs/last_run/app_generator_class.json +25 -1
  58. data/docs/last_run/app_generator_data.json +10 -7
  59. data/docs/last_run/rails_options_class.json +25 -1
  60. data/docs/last_run/rails_options_data.json +12 -8
  61. data/lib/rails_app_generator/addon.rb +30 -2
  62. data/lib/rails_app_generator/addons/devise.rb +119 -0
  63. data/lib/rails_app_generator/addons/devise_masquerade.rb +23 -0
  64. data/lib/rails_app_generator/addons/minimal_css.rb +70 -0
  65. data/lib/rails_app_generator/app_generator.rb +11 -1
  66. data/lib/rails_app_generator/gem_query.rb +34 -0
  67. data/lib/rails_app_generator/rag_initializer.rb +4 -0
  68. data/lib/rails_app_generator/version.rb +1 -1
  69. data/lib/rails_app_generator.rb +1 -0
  70. data/package-lock.json +2 -2
  71. data/package.json +1 -1
  72. data/profiles/addons/devise.json +16 -0
  73. data/profiles/addons/devise_masquerade.json +17 -0
  74. data/profiles/addons/minimal_css.json +15 -0
  75. data/profiles/rag/testy.json +12 -0
  76. data/tasks/addon.thor +7 -3
  77. data/tasks/profile.thor +3 -3
  78. data/templates/Gemfile.erb +1 -0
  79. data/{after_templates/rag/devise/turbo_controller.rb → templates/addons/devise/app/controllers/turbo_devise_controller.rb} +4 -10
  80. data/templates/addons/devise/app/controllers/users/registrations_controller.rb +62 -0
  81. data/templates/addons/devise/app/views/devise/registrations/edit.html.erb +40 -54
  82. data/templates/addons/devise/app/views/devise/registrations/new.html.erb +32 -43
  83. data/templates/addons/devise/app/views/layouts/_alerts.html.erb +2 -0
  84. data/{after_templates/rag → templates/addons}/devise/config/initializers/devise_turbo.rb +1 -1
  85. data/templates/thor_task/profile/after_template.rb +1 -1
  86. data/templates/thor_task/profile/app/views/layouts/_footer.html.erb.tt +1 -3
  87. data/templates/thor_task/profile/app/views/layouts/_navbar.html.erb +5 -8
  88. data/templates/thor_task/profile/app/views/layouts/application.html.erb.tt +8 -2
  89. data/templates/thor_task/profile/db/seeds.rb +2 -11
  90. metadata +67 -18
  91. data/after_templates/rag/devise/application.html.erb +0 -20
  92. data/lib/rails_app_generator/addons/devise_old.rb +0 -61
  93. data/tasks/gem_info.rb +0 -47
  94. data/templates/addons/devise/app/views/devise/confirmations/new.html.erb +0 -24
  95. data/templates/addons/devise/app/views/devise/passwords/edit.html.erb +0 -34
  96. data/templates/addons/devise/app/views/devise/passwords/new.html.erb +0 -23
  97. data/templates/addons/devise/app/views/devise/sessions/new.html.erb +0 -33
  98. data/templates/addons/devise/app/views/devise/shared/_error_messages.html.erb +0 -15
  99. data/templates/addons/devise/app/views/devise/shared/_form_wrap.html.erb +0 -5
  100. data/templates/addons/devise/app/views/devise/shared/_links.html.erb +0 -25
  101. data/templates/addons/devise/app/views/devise/unlocks/new.html.erb +0 -22
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3e643271882d2a6f9c9d1e5e417196dbefb08f94c8e7cefe866a7d8493edfaa1
4
- data.tar.gz: 07b93158af7720c52a2384473c3af1605fb7c0f47c031726bc6c3d95888a2c99
3
+ metadata.gz: f540e53519b46581a9bc18d4db37766d6ac38a27a7493732058aa83254caf62e
4
+ data.tar.gz: da9ecf9716c196663b3d8be4d18e070fadf5f8f3ef4c6c501fbc213f2130a9db
5
5
  SHA512:
6
- metadata.gz: 37df302b97eb73a61e0dbc69006c4cc442e3f22a1f37b9e5af94f87fcab9791dccd803f1b28d0919da839fd0f78deeb40b58bc5261e82b34d1494b55b2c4d024
7
- data.tar.gz: 044e8a0f8da4e0915b10fd894c28af8b062d5907bf216fd3afc56f285d32906a2aba767a09baeaece33e41bed0222d39498b337de4669fb2a009695db6902a8f
6
+ metadata.gz: 7b16806642e285948098aa32af0454f86288a466db8decd5fda79ceb9a2cdbf4d63fa439631d42fc4cf5d45b7b8bc23fc05c2472c781870dd5306cb65940e3dd
7
+ data.tar.gz: dd0a91e5039ae9f3b450f62624f40651a3fc7e036c3a52a8d9a4d2b582958f95b32fc6d51f57742bf4e9e156714bd5ecb5784fa1030a1de4f5e8c6d16199ff7b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,27 @@
1
+ ## [0.2.24](https://github.com/klueless-io/rails_app_generator/compare/v0.2.23...v0.2.24) (2022-08-16)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * add minimal_css profile ([5770975](https://github.com/klueless-io/rails_app_generator/commit/577097594ee3c82218628525b9bc6dd8299fb13e))
7
+
8
+ ## [0.2.23](https://github.com/klueless-io/rails_app_generator/compare/v0.2.22...v0.2.23) (2022-08-16)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * add devise profile ([a665767](https://github.com/klueless-io/rails_app_generator/commit/a6657670b1fce4eb802a56ace36368505ede3c2e))
14
+ * fix invalid gem ([377e547](https://github.com/klueless-io/rails_app_generator/commit/377e5479c4debbd0df9e1362694d1847cf7d58e0))
15
+ * update addon to work with non gems ([f9b0b14](https://github.com/klueless-io/rails_app_generator/commit/f9b0b147859760abbb921a236cd1e7f6a97eb3f0))
16
+
17
+ ## [0.2.22](https://github.com/klueless-io/rails_app_generator/compare/v0.2.21...v0.2.22) (2022-08-14)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * update avo profile ([79a3f36](https://github.com/klueless-io/rails_app_generator/commit/79a3f366f9f95e29a5f003a815c1fc963d796179))
23
+ * update avo profile ([2830ddf](https://github.com/klueless-io/rails_app_generator/commit/2830ddff3fd327d2c162ef1e0128dc525b3a493c))
24
+
1
25
  ## [0.2.21](https://github.com/klueless-io/rails_app_generator/compare/v0.2.20...v0.2.21) (2022-08-12)
2
26
 
3
27
 
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Based on guidance from:
4
+ # Rails 7 - Intro to Devise
5
+ # https://www.youtube.com/watch?v=m3uhldUGVes
6
+ #
7
+ # exe/rag addons/devise
8
+
9
+ self.local_template_path = File.dirname(__FILE__)
10
+
11
+ gac 'base rails 7 image created'
12
+
13
+ prepare_environment
14
+
15
+ after_bundle do
16
+ scaffolds
17
+ setup_customizations
18
+ setup_db
19
+ end
20
+
21
+ def scaffolds
22
+ add_scaffold('post', 'title', 'body:text', 'views:integer', 'user:references')
23
+ end
24
+
25
+ def setup_db
26
+ template 'db/seeds.rb' , 'db/seeds.rb'
27
+
28
+ db_migrate
29
+ db_seed
30
+ end
31
+
32
+ def setup_customizations
33
+ route("root 'home#index'")
34
+
35
+ force_copy
36
+
37
+ add_controller('home', 'index')
38
+
39
+ directory "app/controllers"
40
+ directory "app/models"
41
+ directory "app/views"
42
+ template 'app/views/layouts/application.html.erb' , 'app/views/layouts/application.html.erb'
43
+ end
@@ -0,0 +1,4 @@
1
+ class HomeController < ApplicationController
2
+ def index
3
+ end
4
+ end
@@ -0,0 +1,81 @@
1
+ class PostsController < ApplicationController
2
+ before_action :authenticate_user!, except: %i[show index]
3
+ before_action :set_post, only: %i[show edit update destroy]
4
+ before_action :require_permission, only: %i[edit destroy]
5
+
6
+ def index
7
+ @posts = Post.all.order(updated_at: :desc)
8
+ end
9
+
10
+ def show
11
+ @post.update(views: @post.views + 1)
12
+ end
13
+
14
+ def new
15
+ @post = Post.new
16
+ end
17
+
18
+ def edit
19
+ end
20
+
21
+ def create
22
+ @post = Post.new(post_params)
23
+ @post.user = current_user
24
+
25
+ respond_to do |format|
26
+ if @post.save
27
+ format.html { redirect_to post_url(@post), notice: "Post was successfully created." }
28
+ format.json { render :show, status: :created, location: @post }
29
+ else
30
+ format.html { render :new, status: :unprocessable_entity }
31
+ format.json { render json: @post.errors, status: :unprocessable_entity }
32
+ end
33
+ end
34
+ end
35
+
36
+ def update
37
+ respond_to do |format|
38
+ if @post.update(post_params)
39
+ format.html { redirect_to post_url(@post), notice: "Post was successfully updated." }
40
+ format.json { render :show, status: :ok, location: @post }
41
+ else
42
+ format.html { render :edit, status: :unprocessable_entity }
43
+ format.json { render json: @post.errors, status: :unprocessable_entity }
44
+ end
45
+ end
46
+ end
47
+
48
+ def destroy
49
+ @post.destroy
50
+
51
+ respond_to do |format|
52
+ format.html { redirect_to posts_url, notice: "Post was successfully destroyed." }
53
+ format.json { head :no_content }
54
+ end
55
+ end
56
+
57
+ private
58
+ def set_post
59
+ @post = Post.find(params[:id])
60
+ end
61
+
62
+ def post_params
63
+ params.require(:post).permit(:title, :body, :views, :user_id)
64
+ end
65
+
66
+ def require_permission
67
+ current_user_name = current_user.name.capitalize
68
+ post_user_name = @post.user.name.capitalize
69
+ post_user_name_plural = "#{post_user_name}#{post_user_name.ends_with?('s') ? "'" : "s"}"
70
+
71
+ flash.notice = "#{current_user_name} is trying to #{action_name} #{post_user_name_plural} post."
72
+
73
+ return if current_user.admin? # Administrators can do anything
74
+ return if current_user.moderator? && action_name == "edit" # Moderators can edit any post (but not delete)
75
+ return if current_user.user? && @post.user == current_user # Users can edit or delete their own posts
76
+
77
+ flash.alert = "You do not have permission to #{action_name} #{post_user_name_plural} post."
78
+
79
+ redirect_back(fallback_location: root_path)
80
+ end
81
+ end
@@ -0,0 +1,11 @@
1
+ class Post < ApplicationRecord
2
+ after_initialize :set_views, if: :new_record?
3
+
4
+ belongs_to :user
5
+
6
+ private
7
+
8
+ def set_views
9
+ self.views ||= 0
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ class User < ApplicationRecord
2
+ after_initialize :set_default_role, if: :new_record?
3
+
4
+ # Include default devise modules. Others available are:
5
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
6
+ devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable
7
+
8
+ has_many :posts
9
+
10
+ enum role: { user: 0, moderator: 1, admin: 9 }
11
+
12
+ private
13
+
14
+ def set_default_role
15
+ self.role ||= :user
16
+ end
17
+ end
@@ -0,0 +1,40 @@
1
+ <h1>Devise</h1>
2
+
3
+ <h4>Using a simple Enum based Role for permissions</h4>
4
+
5
+ <ul>
6
+ <li>Anyone can view posts</li>
7
+ <li>Only signed in users can create, update or delete a post</li>
8
+ <li>If role <b>user</b>, you can update and delete your own posts only</li>
9
+ <li>If role <b>moderator</b>, you can update any post, but only delete your own posts</li>
10
+ <li>If role <b>admin</b>, you can update or delete any post</li>
11
+ </ul>
12
+
13
+ <h3>Sample users and passwords</h3>
14
+
15
+ <style>
16
+ table {
17
+ border-collapse: collapse;
18
+ }
19
+ th, td {
20
+ border: 1px solid #ccc;
21
+ padding: 5px;
22
+ }
23
+ </style>
24
+
25
+ <table style='width: 600px;'>
26
+ <tr>
27
+ <th>Name</th>
28
+ <th>Email</th>
29
+ <th>Role</th>
30
+ <th>Password</th>
31
+ </tr>
32
+ <% User.all.each do |user| %>
33
+ <tr>
34
+ <td><%= user.name %></td>
35
+ <td><%= user.email %></td>
36
+ <td><%= user.role %></td>
37
+ <td>password</td>
38
+ </tr>
39
+ <% end %>
40
+ </table>
@@ -0,0 +1,10 @@
1
+ <%= link_to 'Home', root_path %>
2
+ | <%= link_to 'Posts', posts_path %>
3
+ <% if current_user %>
4
+ <%= current_user.name || current_user.email %>
5
+ | <%= link_to 'Edit', edit_user_registration_path %>
6
+ | <%= link_to 'Sign Out', destroy_user_session_path, data: { turbo_method: :delete } %>
7
+ <% else %>
8
+ | <%= link_to 'Sign Up', new_user_registration_path %>
9
+ | <%= link_to 'Sign In', new_user_session_path %>
10
+ <% end%>
@@ -0,0 +1,38 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title><%= camelized %></title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <%%= csrf_meta_tags %>
7
+ <%%= csp_meta_tag %>
8
+
9
+ <%- if options[:skip_hotwire] || options[:skip_javascript] -%>
10
+ <%%= stylesheet_link_tag "application" %>
11
+ <%- else -%>
12
+ <%%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
13
+ <%- end -%>
14
+ <%%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>
15
+ <style>
16
+ .notice {
17
+ color: green;
18
+ }
19
+ .alert {
20
+ color: red;
21
+ }
22
+ </style>
23
+ </head>
24
+
25
+ <body>
26
+ <header>
27
+ <%%= render 'layouts/navbar' %>
28
+ <hr />
29
+ <%%= render 'layouts/alerts' %>
30
+ </header>
31
+ <main>
32
+ <%%= yield %>
33
+ </main>
34
+ <footer>
35
+ <%%= render 'layouts/footer' %>
36
+ </footer>
37
+ </body>
38
+ </html>
@@ -0,0 +1,27 @@
1
+ <%= form_with(model: post) do |form| %>
2
+ <% if post.errors.any? %>
3
+ <div style="color: red">
4
+ <h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
5
+
6
+ <ul>
7
+ <% post.errors.each do |error| %>
8
+ <li><%= error.full_message %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <div>
15
+ <%= form.label :title, style: "display: block" %>
16
+ <%= form.text_field :title %>
17
+ </div>
18
+
19
+ <div>
20
+ <%= form.label :body, style: "display: block" %>
21
+ <%= form.text_area :body %>
22
+ </div>
23
+
24
+ <div>
25
+ <%= form.submit %>
26
+ </div>
27
+ <% end %>
@@ -0,0 +1,14 @@
1
+ <div id="<%= dom_id post %>">
2
+ <h3>posted by <%= post.user.name || post.user.email %></h4>
3
+ <h4><%= pluralize(post.views, 'view') %></h4>
4
+
5
+ <p>
6
+ <strong>Title:</strong>
7
+ <%= post.title %>
8
+ </p>
9
+
10
+ <p>
11
+ <strong>Body:</strong>
12
+ <%= post.body %>
13
+ </p>
14
+ </div>
@@ -0,0 +1,12 @@
1
+ <h1>Posts</h1>
2
+
3
+ <div id="posts">
4
+ <% @posts.each do |post| %>
5
+ <%= render post %>
6
+ <p>
7
+ <%= link_to "Show this post", post %>
8
+ </p>
9
+ <% end %>
10
+ </div>
11
+
12
+ <%= link_to "New post", new_post_path %>
@@ -0,0 +1,8 @@
1
+ <%= render @post %>
2
+
3
+ <div>
4
+ <%= link_to "Edit this post", edit_post_path(@post) %> |
5
+ <%= link_to "Back to posts", posts_path %>
6
+
7
+ <%= button_to "Destroy this post", @post, method: :delete %>
8
+ </div>
@@ -0,0 +1,11 @@
1
+ User.create(email: 'david@site.com', name: 'david', password: 'password', password_confirmation: 'password', role: :admin)
2
+ User.create(email: 'james@site.com', name: 'james', password: 'password', password_confirmation: 'password')
3
+ User.create(email: 'sally@site.com', name: 'sally', password: 'password', password_confirmation: 'password', role: :moderator)
4
+
5
+ 10.times do
6
+ Post.create(
7
+ title: "Post #{Faker::Lorem.words(number: rand(1..3)).join(' ')}",
8
+ body: "#{Faker::Lorem.sentences(number: 8).join("<br />")}",
9
+ views: 0,
10
+ user: User.all.sample)
11
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Description goes here
4
+ #
5
+ # exe/rag addons/devise_masquerade
6
+
7
+ self.local_template_path = File.dirname(__FILE__)
8
+
9
+ gac 'base rails 7 image created'
10
+
11
+ prepare_environment
12
+
13
+ after_bundle do
14
+ setup_customizations
15
+ setup_db
16
+ end
17
+
18
+ def setup_db
19
+ template 'db/seeds.rb' , 'db/seeds.rb'
20
+
21
+ db_migrate
22
+ db_seed
23
+ end
24
+
25
+ def setup_customizations
26
+ route("root 'home#index'")
27
+
28
+ force_copy
29
+
30
+ add_controller('home', 'index')
31
+
32
+ directory "app/controllers"
33
+ directory "app/models"
34
+ directory "app/views"
35
+ template 'app/views/layouts/application.html.erb' , 'app/views/layouts/application.html.erb'
36
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ before_action :masquerade_user!
3
+ end
@@ -0,0 +1,4 @@
1
+ class HomeController < ApplicationController
2
+ def index
3
+ end
4
+ end
@@ -0,0 +1,10 @@
1
+ class Users::MasqueradesController < Devise::MasqueradesController
2
+ # This location matches the location for other Devise controllers
3
+
4
+ protected
5
+
6
+ # Custom url redirect after masquerade
7
+ def after_masquerade_path_for(_resource)
8
+ root_path
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ class User < ApplicationRecord
2
+ after_initialize :set_default_role, if: :new_record?
3
+
4
+ # Include default devise modules. Others available are:
5
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
6
+ devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :masqueradable
7
+
8
+ has_many :posts
9
+
10
+ enum role: { user: 0, moderator: 1, admin: 9 }
11
+
12
+ private
13
+
14
+ def set_default_role
15
+ self.role ||= :user
16
+ end
17
+ end
@@ -0,0 +1,66 @@
1
+ <h1>Devise Masquerade</h1>
2
+
3
+ <h4>Devise extension that enables login as another user functionality</h4>
4
+
5
+ <p><b class='alert'>Warning!</b> If <code>masquerade?</code> is never returning <code>true</code> then ensure you have turned on caching in development using <code>rails dev:cache</code> </p>
6
+
7
+ <pre><code>user_masquerade? => <%= user_masquerade? %></code></pre>
8
+
9
+ <p>Sign in with an <b>Admin</b> user and then impersonate other users</p>
10
+
11
+ <h3>Sample users and passwords</h3>
12
+
13
+ <% if current_user %>
14
+ <% if current_user.admin? %>
15
+ <h1 class='admin'><%= current_user.name.capitalize %> - Admininstrator</h1>
16
+ <% elsif current_user.user? %>
17
+ <h1 class='user'><%= current_user.name.capitalize %> - User</h1>
18
+ <% end %>
19
+ <% else %>
20
+ <h1 class='not-signed-in'>Not currently signed in</h1>
21
+ <% end %>
22
+
23
+ <table style='width: 600px;'>
24
+ <tr>
25
+ <th>Name</th>
26
+ <th>Email</th>
27
+ <th>Role</th>
28
+ <% if current_user %>
29
+ <th>Masquerade As</th>
30
+ <% else %>
31
+ <th>Password</th>
32
+ <% end %>
33
+ </tr>
34
+ <% User.all.each do |user| %>
35
+ <tr>
36
+ <td><%= user.name %></td>
37
+ <td><%= user.email %></td>
38
+ <td><%= user.role %></td>
39
+ <td>
40
+ <% if current_user %>
41
+ <% if user_masquerade? %>
42
+ <% if user.admin? %>
43
+ <%= link_to "Back to #{user.name.capitalize}", back_masquerade_path(User.new) %>
44
+ <% end %>
45
+ <% else %>
46
+ <% if current_user.user? %>
47
+ Cannot masquerade
48
+ <% elsif user.user? %>
49
+ <%= link_to user.name.capitalize, masquerade_path(user) %>
50
+ <% else %>
51
+
52
+ <% end %>
53
+ <% end %>
54
+ <% else %>
55
+ password
56
+ <% end %>
57
+
58
+ </td>
59
+ </td>
60
+ </tr>
61
+ <% end %>
62
+ </table>
63
+
64
+ <% if current_user&.user? && !user_masquerade? %>
65
+ <p class='alert'>Sign in as an admin user</p>
66
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <%= link_to 'Home', root_path %>
2
+ <% if current_user %>
3
+ | <%= link_to 'Sign Out', destroy_user_session_path, data: { turbo_method: :delete } %>
4
+ <% else %>
5
+ | <%= link_to 'Sign In', new_user_session_path %>
6
+ <% end%>
@@ -0,0 +1,57 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title><%= camelized %></title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <%%= csrf_meta_tags %>
7
+ <%%= csp_meta_tag %>
8
+
9
+ <%- if options[:skip_hotwire] || options[:skip_javascript] -%>
10
+ <%%= stylesheet_link_tag "application" %>
11
+ <%- else -%>
12
+ <%%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
13
+ <%- end -%>
14
+ <%%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>
15
+ <style>
16
+ .notice {
17
+ color: green;
18
+ }
19
+ .alert {
20
+ color: red;
21
+ }
22
+ table {
23
+ border-collapse: collapse;
24
+ }
25
+ th, td {
26
+ border: 1px solid #ccc;
27
+ padding: 5px;
28
+ }
29
+ .admin {
30
+ background-color: pink;
31
+ }
32
+ .user {
33
+ background-color: lightgreen;
34
+ }
35
+ .not-signed-in {
36
+ background-color: lightgrey;
37
+ }
38
+ .admin, .user, .not-signed-in {
39
+ text-align: center;
40
+ }
41
+ </style>
42
+ </head>
43
+
44
+ <body>
45
+ <header>
46
+ <%%= render 'layouts/navbar' %>
47
+ <hr />
48
+ <%%= render 'layouts/alerts' %>
49
+ </header>
50
+ <main>
51
+ <%%= yield %>
52
+ </main>
53
+ <footer>
54
+ <%%= render 'layouts/footer' %>
55
+ </footer>
56
+ </body>
57
+ </html>
@@ -0,0 +1,3 @@
1
+ User.create(email: 'david@site.com', name: 'david', password: 'password', password_confirmation: 'password', role: :admin)
2
+ User.create(email: 'james@site.com', name: 'james', password: 'password', password_confirmation: 'password')
3
+ User.create(email: 'sally@site.com', name: 'sally', password: 'password', password_confirmation: 'password')
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Description goes here
4
+ #
5
+ # exe/rag addons/minimal_css
6
+
7
+ self.local_template_path = File.dirname(__FILE__)
8
+
9
+ gac 'base rails 7 image created'
10
+
11
+ prepare_environment
12
+
13
+ add_controller('home', 'index')
14
+
15
+ route("root 'home#index'")
16
+
17
+ force_copy
18
+
19
+ directory "app/controllers"
20
+ directory "app/views/home"
21
+ directory "app/views/layouts"
22
+ template 'app/views/layouts/application.html.erb' , 'app/views/layouts/application.html.erb'
23
+
24
+ template 'db/seeds.rb' , 'db/seeds.rb'
25
+
26
+ after_bundle do
27
+ setup_db
28
+ end
29
+
30
+ def setup_db
31
+ add_scaffold('person', 'first_name', 'last_name', 'age:integer', 'address:text')
32
+
33
+ db_migrate
34
+ db_seed
35
+ end
36
+
37
+ # Other template command examples
38
+ # prepare_environment
39
+ # bundle_install
40
+ # css_install('tailwind')
41
+ # rails_command('db:migrate')
42
+ # rails_command('db:migrate')
43
+ # bundle_add('hotwire-rails')
44
+ # rails_command('hotwire:install')
45
+ # run('bin/importmap pin sortablejs')
46
+ # run('npm install daisyui')
47
+ # rubocop
48
+ #
49
+ # directory 'app/assets/images'
50
+ # create_file 'app/assets/stylesheets/custom-bootstrap-import.scss' , read_template('custom-bootstrap-import.scss')
51
+ # append_to_file 'app/assets/config/manifest.js' , read_template('manifest.js')
52
+ # insert_into_file 'app/views/layouts/application.html.erb', read_template('application.html.erb'),
53
+ # before: %( <%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>)
54
+ # gsub_file 'app/views/layouts/application.html.erb', %(container mx-auto mt-28 px-5 flex), 'container mx-auto px-5'
55
+ # template 'home.css', 'app/assets/stylesheets/home.css'
56
+ #
57
+ # add_controller('page', 'benefits', 'faq', 'terms', 'privacy', '--skip-routes')
58
+ # route(<<-'RUBY')
59
+ # PageController.action_methods.each do |action|
60
+ # get "/#{action}", to: "page##{action}", as: "page_#{action}"
61
+ # end
62
+ # RUBY
@@ -0,0 +1,4 @@
1
+ class HomeController < ApplicationController
2
+ def index
3
+ end
4
+ end