b4um_generators 0.4.2 → 0.5.1

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.
@@ -0,0 +1,217 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+ require "rails/generators/named_base"
5
+
6
+ module B4um
7
+ module Generators
8
+ class TodoGenerator < Rails::Generators::NamedBase
9
+ include Rails::Generators::Migration
10
+
11
+ source_root File.expand_path("templates", __dir__)
12
+
13
+ class_option :owner,
14
+ type: :string,
15
+ default: nil,
16
+ desc: "Associates tasks with the specified owner model."
17
+
18
+ class_option :login_path,
19
+ type: :string,
20
+ default: "login_path",
21
+ desc: "Route helper for unauthenticated users (default: login_path)."
22
+
23
+ class << self
24
+ def desc(_description = nil)
25
+ "Creates a complete To-Do list with Turbo and Stimulus."
26
+ end
27
+ end
28
+
29
+ def validate_owner
30
+ return if options[:owner].nil?
31
+
32
+ owner_name = options[:owner].to_s
33
+
34
+ return if owner_name.match?(/\A[A-Z][A-Za-z0-9]*\z/)
35
+
36
+ raise Thor::Error,
37
+ "Invalid owner model: #{owner_name}"
38
+ end
39
+
40
+ def validate_login_path
41
+ return unless owner?
42
+
43
+ login_route = options[:login_path].to_s
44
+
45
+ return if login_route.match?(/\A[a-z_][a-zA-Z0-9_]*_path\z/)
46
+
47
+ raise Thor::Error,
48
+ "Invalid login path: #{login_route}. Expected a Rails route helper ending in _path."
49
+ end
50
+
51
+ def create_todo_migration
52
+ return if todo_migration_exists?
53
+
54
+ migration_template(
55
+ "create_tasks.rb.tt",
56
+ "db/migrate/create_#{plural_table_name}.rb"
57
+ )
58
+ end
59
+
60
+ def self.next_migration_number(_dirname)
61
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
62
+ end
63
+
64
+ def create_todo_model
65
+ model_path = "app/models/#{file_path}.rb"
66
+
67
+ return if File.exist?(
68
+ File.join(destination_root, model_path)
69
+ )
70
+
71
+ template(
72
+ "task.rb.tt",
73
+ model_path
74
+ )
75
+ end
76
+
77
+ def update_owner_model
78
+ return unless owner?
79
+
80
+ owner_path = "app/models/#{owner_file_name}.rb"
81
+
82
+ unless File.exist?(
83
+ File.join(destination_root, owner_path)
84
+ )
85
+ raise Thor::Error,
86
+ "Owner model not found: #{owner_path}"
87
+ end
88
+
89
+ association = "has_many :#{plural_table_name}, dependent: :destroy"
90
+
91
+ return if File.read(
92
+ File.join(destination_root, owner_path)
93
+ ).include?(association)
94
+
95
+ inject_into_class(
96
+ owner_path,
97
+ owner_class_name,
98
+ " #{association}\n"
99
+ )
100
+ end
101
+
102
+ def create_todo_controller
103
+ controller_path = "app/controllers/#{plural_table_name}_controller.rb"
104
+
105
+ return if File.exist?(
106
+ File.join(destination_root, controller_path)
107
+ )
108
+
109
+ template(
110
+ "tasks_controller.rb.tt",
111
+ controller_path
112
+ )
113
+ end
114
+
115
+ def add_todo_routes
116
+ routes_path = "config/routes.rb"
117
+
118
+ full_path = File.join(destination_root, routes_path)
119
+
120
+ raise Thor::Error, "Routes file not found: #{routes_path}" unless File.exist?(full_path)
121
+
122
+ routes_content = File.read(full_path)
123
+
124
+ resource_name = plural_table_name
125
+
126
+ return if routes_content.match?(
127
+ /^\s*resources\s+:#{Regexp.escape(resource_name)}\b/
128
+ )
129
+
130
+ route(
131
+ "resources :#{resource_name}, only: %i[index create update destroy]"
132
+ )
133
+ end
134
+
135
+ def create_todo_views
136
+ views_path = "app/views/#{plural_table_name}"
137
+
138
+ {
139
+ "index.html.erb.tt" => "index.html.erb",
140
+ "_task.html.erb.tt" => "_task.html.erb",
141
+ "_progress.html.erb.tt" => "_progress.html.erb"
142
+ }.each do |template_name, output_name|
143
+ destination = "#{views_path}/#{output_name}"
144
+
145
+ next if File.exist?(
146
+ File.join(destination_root, destination)
147
+ )
148
+
149
+ template(template_name, destination)
150
+ end
151
+ end
152
+
153
+ def create_todo_stylesheet
154
+ stylesheet_path = "app/assets/stylesheets/b4um_todo.css"
155
+
156
+ return if File.exist?(
157
+ File.join(destination_root, stylesheet_path)
158
+ )
159
+
160
+ copy_file(
161
+ "todo.css",
162
+ stylesheet_path
163
+ )
164
+ end
165
+
166
+ def create_todo_stimulus_controller
167
+ controller_path = "app/javascript/controllers/b4um_todo_controller.js"
168
+
169
+ return if File.exist?(
170
+ File.join(destination_root, controller_path)
171
+ )
172
+
173
+ copy_file(
174
+ "todo_controller.js",
175
+ controller_path
176
+ )
177
+ end
178
+
179
+ private
180
+
181
+ def current_owner_method
182
+ "current_#{owner_file_name}"
183
+ end
184
+
185
+ def migration_version
186
+ version = Gem.loaded_specs.fetch("railties").version
187
+
188
+ "#{version.segments[0]}.#{version.segments[1]}"
189
+ end
190
+
191
+ def todo_migration_exists?
192
+ Dir.glob(
193
+ File.join(
194
+ destination_root,
195
+ "db/migrate/*_create_#{plural_table_name}.rb"
196
+ )
197
+ ).any?
198
+ end
199
+
200
+ def owner?
201
+ options[:owner].present?
202
+ end
203
+
204
+ def owner_class_name
205
+ options[:owner].to_s.camelize
206
+ end
207
+
208
+ def owner_file_name
209
+ owner_class_name.underscore
210
+ end
211
+
212
+ def owner_reference_name
213
+ owner_file_name
214
+ end
215
+ end
216
+ end
217
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: b4um_generators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Baum
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 1980-01-02 00:00:00.000000000 Z
10
+ date: 2026-10-14 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: railties
@@ -168,6 +168,15 @@ files:
168
168
  - lib/generators/b4um/sortable/templates/sortable_controller.js
169
169
  - lib/generators/b4um/table/table_generator.rb
170
170
  - lib/generators/b4um/table/templates/_table.html.erb.tt
171
+ - lib/generators/b4um/todo/templates/_progress.html.erb.tt
172
+ - lib/generators/b4um/todo/templates/_task.html.erb.tt
173
+ - lib/generators/b4um/todo/templates/create_tasks.rb.tt
174
+ - lib/generators/b4um/todo/templates/index.html.erb.tt
175
+ - lib/generators/b4um/todo/templates/task.rb.tt
176
+ - lib/generators/b4um/todo/templates/tasks_controller.rb.tt
177
+ - lib/generators/b4um/todo/templates/todo.css
178
+ - lib/generators/b4um/todo/templates/todo_controller.js
179
+ - lib/generators/b4um/todo/todo_generator.rb
171
180
  - lib/generators/b4um/trix/templates/b4um_rich_text_helper.rb
172
181
  - lib/generators/b4um/trix/templates/b4um_trix.js
173
182
  - lib/generators/b4um/trix/trix_generator.rb
@@ -192,7 +201,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
201
  - !ruby/object:Gem::Version
193
202
  version: '0'
194
203
  requirements: []
195
- rubygems_version: 3.6.9
204
+ rubygems_version: 3.6.2
196
205
  specification_version: 4
197
206
  summary: Rails generators for b4um applications
198
207
  test_files: []