sun-sword 0.0.1 → 0.0.3

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 (30) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -10
  3. data/lib/generators/sun_sword/frontend_generator.rb +43 -19
  4. data/lib/generators/sun_sword/init_generator.rb +15 -0
  5. data/lib/generators/sun_sword/scaffold_generator.rb +160 -9
  6. data/lib/generators/sun_sword/templates_frontend/config/devise.rb +313 -0
  7. data/lib/generators/sun_sword/templates_frontend/controllers/application_controller.rb.tt +7 -3
  8. data/lib/generators/sun_sword/templates_frontend/controllers/site_controller.rb +1 -0
  9. data/lib/generators/sun_sword/templates_frontend/db/migrate/20241117042039_devise_create_auths.rb +47 -0
  10. data/lib/generators/sun_sword/templates_frontend/db/migrate/20241117043154_create_models_accounts.rb +11 -0
  11. data/lib/generators/sun_sword/templates_frontend/db/migrate/20241117044142_create_models_users.rb +16 -0
  12. data/lib/generators/sun_sword/templates_frontend/db/seeds.rb +3 -0
  13. data/lib/generators/sun_sword/templates_frontend/db/structures/example.yaml.tt +106 -0
  14. data/lib/generators/sun_sword/templates_frontend/frontend/pages/web.js +2 -8
  15. data/lib/generators/sun_sword/templates_frontend/models/models/account.rb +4 -0
  16. data/lib/generators/sun_sword/templates_frontend/models/models/auth.rb +8 -0
  17. data/lib/generators/sun_sword/templates_frontend/models/models/user.rb +5 -0
  18. data/lib/generators/sun_sword/templates_frontend/views/layouts/dashboard/_sidebar.html.erb.tt +11 -1
  19. data/lib/generators/sun_sword/templates_frontend/views/layouts/dashboard/application.html.erb.tt +6 -9
  20. data/lib/generators/sun_sword/templates_init/config/initializers/sun_sword.rb +3 -0
  21. data/lib/generators/sun_sword/templates_scaffold/controllers/controller.rb.tt +31 -20
  22. data/lib/generators/sun_sword/templates_scaffold/views/_form.html.erb.tt +3 -8
  23. data/lib/generators/sun_sword/templates_scaffold/views/components/menu/link.html.erb.tt +1 -1
  24. data/lib/generators/sun_sword/templates_scaffold/views/index.html.erb.tt +4 -4
  25. data/lib/generators/sun_sword/templates_scaffold/views/show.html.erb.tt +2 -2
  26. data/lib/sun-sword.rb +4 -2
  27. data/lib/sun_sword/configuration.rb +29 -0
  28. data/lib/sun_sword/version.rb +1 -1
  29. metadata +28 -3
  30. data/lib/generators/sun_sword/templates_frontend/controllers/dashboard/site_controller.rb +0 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c8b5bbb0e89debafcd22667bbce169c0fb847525e117a171f4de9132164ebad
4
- data.tar.gz: 8b6201d7d4b9fe8f7bebd6237d81b66ed8a83c2dab005174c1763296ffd95961
3
+ metadata.gz: e4c9ef673af8d51240f8c706d4c18ca8286721cf860d3effca58aafcb3042a38
4
+ data.tar.gz: 57afc60235b6dd6a2bdd15906d039ad865cee125447290786af3385ceed0551e
5
5
  SHA512:
6
- metadata.gz: a87faefb890fcd7c4d99796079f914e441616c1945078b302d12492321c2b9859556b13ca79364cd3cb05f92f3c80b493c241172377ac341f949632833daafaf
7
- data.tar.gz: 3876268e5930319b5204f7351c15ffaee81b7cfca94a3bcac893de864f1ed6a2cf96be7b30a1e902e44b29907e94d975ab720b7b2353d280456a7ef007f9ab5e
6
+ metadata.gz: ce22f06920d4dec14553da3a111e2d51040d4c8ac7a0c00a0faca90b2b80b03ef0926d5b74af43cca035eb955e74a390daf9cdfc5411870363161308828513d7
7
+ data.tar.gz: 7d026ad20c3fcf0e9b8c4d16ed226a2487efa837d0df7a52fe496b061b5094a3babcf79578dd5c1e288efb8019fb099f5c4751a2bd33a171d09ff49885ccf4ab
data/README.md CHANGED
@@ -10,19 +10,19 @@ Add this line to your application's Gemfile:
10
10
  ```ruby
11
11
  gem 'rider-kick'
12
12
  gem 'sun-sword'
13
-
14
13
  ```
15
14
 
16
- And then execute:
17
- $ rails new kotaro_minami -d=postgresql -T --skip-javascript --skip-asset-pipeline
18
- $ bundle install
19
- $ rails generate rider_kick:init
20
- $ rails generate rider_kick:clean_arch --setup
21
- $ rails db:drop db:create db:migrate db:seed
22
- $ rails generate sun_sword:frontend
23
-
24
- $ rubocop -a
25
15
 
16
+ And then execute:
17
+ ```bash
18
+ $ rails new kotaro_minami -d=postgresql -T --skip-javascript --skip-asset-pipeline
19
+ $ bundle install
20
+ $ rails generate rider_kick:init
21
+ $ rails generate rider_kick:clean_arch --setup
22
+ $ rails db:drop db:create db:migrate db:seed
23
+ $ rails generate sun_sword:frontend
24
+ $ rubocop -a
25
+ ```
26
26
  ## Usage
27
27
  ```bash
28
28
  Description:
@@ -13,12 +13,10 @@ module SunSword
13
13
  desc 'This generator installs Vite with Rails 7 configuration'
14
14
 
15
15
  def setup
16
+ validation!
16
17
  remove_assets_folder
17
18
  copy_assets_from_template
18
19
 
19
- # copy_database_config
20
- # copy_env_development
21
-
22
20
  add_vite_to_gemfile
23
21
  install_vite
24
22
  configure_vite
@@ -28,10 +26,50 @@ module SunSword
28
26
  generate_controllers_site
29
27
  generate_components
30
28
  modify_layout_for_vite
29
+
30
+ setup_migration
31
+ setup_models
32
+ gem_dependencies
31
33
  end
32
34
 
33
35
  private
34
36
 
37
+ def gem_dependencies
38
+ # auth
39
+ # rails generate devise:install
40
+ # config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
41
+ # devise_for :auths, class_name: "Models::Auth", controllers: {
42
+ # sessions: 'auths/sessions'
43
+ # }
44
+ gem 'devise', '~> 4.9'
45
+
46
+ say 'Menjalankan bundle install...'
47
+ run 'bundle install'
48
+ run 'rails generate devise:install'
49
+ inject_into_file 'config/routes.rb', " devise_for :auths, class_name: 'Models::Auth'\n", after: "Rails.application.routes.draw do\n"
50
+ end
51
+
52
+ def setup_migration
53
+ template 'db/migrate/20241117042039_devise_create_auths.rb', File.join('db/migrate/20241117042039_devise_create_auths.rb')
54
+ template 'db/migrate/20241117043154_create_models_accounts.rb', File.join('db/migrate/20241117043154_create_models_accounts.rb')
55
+ template 'db/migrate/20241117044142_create_models_users.rb', File.join('db/migrate/20241117044142_create_models_users.rb')
56
+
57
+ template 'db/seeds.rb', File.join('db/seeds.rb')
58
+ end
59
+
60
+ def setup_models
61
+ template 'models/models/account.rb', File.join('app/models/models/account.rb')
62
+ template 'models/models/auth.rb', File.join('app/models/models/auth.rb')
63
+ template 'models/models/user.rb', File.join('app/models/models/user.rb')
64
+ end
65
+
66
+ def validation!
67
+ unless File.exist?('config/initializers/sun_sword.rb')
68
+ say 'Error must create init configuration for sun_sword!'
69
+ raise Thor::Error, 'run: bin/rails generate sun_sword:init'
70
+ end
71
+ end
72
+
35
73
  def remove_assets_folder
36
74
  assets_path = "#{path_app}/assets"
37
75
  if Dir.exist?(assets_path)
@@ -47,18 +85,8 @@ module SunSword
47
85
  say "File '#{path_app}/assets' has been copied from template.", :green
48
86
  end
49
87
 
50
- def copy_database_config
51
- template 'config/database.yml', File.join('config/database.yml')
52
- end
53
-
54
- def copy_env_development
55
- template 'env.development', File.join('.env.development')
56
- template 'env.development', File.join('env.example')
57
- end
58
-
59
88
  def add_vite_to_gemfile
60
89
  gem_dependencies = <<~RUBY
61
-
62
90
  # --- SunSword Package frontend
63
91
  gem "turbo-rails"
64
92
  gem "stimulus-rails"
@@ -120,10 +148,6 @@ module SunSword
120
148
  get "site/jadi_a"
121
149
  get "site/jadi_b"
122
150
 
123
- namespace :dashboard do
124
- get "site" => "site#index", as: :dashboard_site_index
125
- end
126
-
127
151
  RUBY
128
152
  inject_into_file 'config/routes.rb', site_route, after: "Rails.application.routes.draw do\n"
129
153
 
@@ -139,9 +163,9 @@ module SunSword
139
163
  template 'views/site/_comment.html.erb.tt', 'app/views/site/_comment.html.erb'
140
164
  template 'views/layouts/application.html.erb.tt', 'app/views/layouts/application.html.erb'
141
165
 
142
- directory('views/layouts/dashboard', 'app/views/layouts/dashboard')
166
+ template 'views/layouts/dashboard/application.html.erb.tt', 'app/views/layouts/owner/application.html.erb'
167
+ template 'views/layouts/dashboard/_sidebar.html.erb.tt', 'app/views/components/layouts/_sidebar.html.erb'
143
168
  directory('helpers', 'app/helpers')
144
- template('controllers/dashboard/site_controller.rb', 'app/controllers/dashboard/site_controller.rb')
145
169
  say 'Updated application layout for Vite integration', :green
146
170
  end
147
171
  end
@@ -0,0 +1,15 @@
1
+ module SunSword
2
+ class InitGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('templates_init', __dir__)
4
+
5
+ def setup_configuration
6
+ copy_initializer('sun_sword')
7
+ end
8
+
9
+ private
10
+
11
+ def copy_initializer(file_name)
12
+ template "config/initializers/#{file_name}.rb", File.join("config/initializers/#{file_name}.rb")
13
+ end
14
+ end
15
+ end
@@ -3,6 +3,7 @@ module SunSword
3
3
  source_root File.expand_path('templates_scaffold', __dir__)
4
4
 
5
5
  argument :arg_structure, type: :string, default: '', banner: ''
6
+ argument :arg_scope, type: :hash, default: '', banner: 'scope:dashboard'
6
7
 
7
8
  def running
8
9
  validation!
@@ -16,9 +17,9 @@ module SunSword
16
17
  private
17
18
 
18
19
  def validation!
19
- unless File.exist?('config/initializers/rider_kick.rb')
20
- say 'Error must create init configuration for rider_kick!'
21
- raise Thor::Error, 'run: bin/rails generate rider_kick:init'
20
+ unless File.exist?('config/initializers/sun_sword.rb')
21
+ say 'Error must create init configuration for sun_sword!'
22
+ raise Thor::Error, 'run: bin/rails generate sun_sword:init'
22
23
  end
23
24
  end
24
25
 
@@ -43,8 +44,6 @@ module SunSword
43
44
  @contract_destroy = @services.action_destroy.use_case.contract || []
44
45
  @skipped_fields = entity.skipped_fields || []
45
46
  @custom_fields = entity.custom_fields || []
46
- @route_scope_path = @structure.controllers.route_scope.downcase rescue ''
47
- @route_scope_class = @route_scope_path.camelize rescue ''
48
47
 
49
48
  @variable_subject = model_name.split('::').last.underscore.downcase
50
49
  @scope_path = resource_name.pluralize.underscore.downcase
@@ -52,6 +51,127 @@ module SunSword
52
51
  @model_class = model_name.camelize.constantize
53
52
  @subject_class = resource_name.camelize
54
53
  @fields = contract_fields
54
+ @form_fields = @controllers.form_fields
55
+
56
+ @route_scope_path = arg_scope['scope'].to_s.downcase rescue ''
57
+ @route_scope_class = @route_scope_path.camelize rescue ''
58
+
59
+ @mapping_fields = {
60
+ string: :text_field,
61
+ text: :text_area,
62
+ integer: :number_field,
63
+ float: :number_field,
64
+ decimal: :number_field,
65
+ boolean: :check_box,
66
+ date: :date_select,
67
+ datetime: :datetime_select,
68
+ timestamp: :datetime_select,
69
+ time: :time_select,
70
+ enum: :select,
71
+ file: :file_field,
72
+ files: :file_fields
73
+ }
74
+ end
75
+
76
+ def generate_form_fields_html
77
+ form_fields_html = ''
78
+ @form_fields.each do |field|
79
+ field_name = field[:name].to_sym
80
+ field_type = field[:type].to_sym
81
+ form_helper = @mapping_fields[field_type] || :text_field
82
+ input_id = "#{@variable_subject}_#{field_name}"
83
+ label_input_id = case form_helper
84
+ when :date_select, :datetime_select then "#{input_id}_1i" # Year
85
+ when :time_select then "#{input_id}_4i" # Hour
86
+ else input_id
87
+ end
88
+ field_html = <<-HTML
89
+
90
+ <div class="sm:grid sm:grid-cols-3 sm:items-start sm:gap-4 sm:py-6">
91
+ <%= form.label :#{field_name}, for: '#{label_input_id}', class: "block text-sm font-medium text-gray-700 sm:pt-1.5" %>
92
+ <div class="mt-2 sm:col-span-2 sm:mt-0">
93
+ HTML
94
+
95
+ case form_helper
96
+ when :text_field, :number_field
97
+ field_html += <<-HTML
98
+ <%= form.#{form_helper} :#{field_name}, id: '#{input_id}', class: "block w-full rounded-md border-gray-300 py-1.5 text-gray-700 shadow-sm focus:ring-2 focus:ring-gray-300 focus:border-gray-300 sm:max-w-md sm:text-sm" %>
99
+ HTML
100
+ when :text_area
101
+ field_html += <<-HTML
102
+ <%= form.text_area :#{field_name}, id: '#{input_id}', rows: 3, class: "block w-full max-w-2xl rounded-md border-gray-300 py-1.5 text-gray-700 shadow-sm focus:ring-2 focus:ring-gray-300 focus:border-gray-300 sm:text-sm" %>
103
+ HTML
104
+ when :check_box
105
+ field_html += <<-HTML
106
+ <div class="relative flex items-start">
107
+ <div class="flex h-6 items-center">
108
+ <%= form.check_box :#{field_name}, id: '#{input_id}', class: "h-4 w-4 rounded border-gray-300 text-gray-600 focus:ring-gray-300 focus:border-gray-300" %>
109
+ </div>
110
+ <div class="ml-3 text-sm">
111
+ <%= form.label :#{field_name}, for: '#{input_id}', class: "font-medium text-gray-700" %>
112
+ </div>
113
+ </div>
114
+ HTML
115
+ when :select
116
+ field_html += <<-HTML
117
+ <%= form.select :#{field_name}, options_for_select([['Option 1', 1], ['Option 2', 2]]), {}, { id: '#{input_id}', class: "block w-full rounded-md border-gray-300 py-1.5 text-gray-700 shadow-sm focus:ring-2 focus:ring-gray-300 focus:border-gray-300 sm:max-w-xs sm:text-sm" } %>
118
+ HTML
119
+ when :datetime_select, :date_select, :time_select
120
+ field_html += <<-HTML
121
+ <%= form.#{form_helper} :#{field_name}, { discard_second: true, id_prefix: '#{input_id}' }, { class: "text-gray-700 shadow-sm focus:ring-2 focus:ring-gray-300 focus:border-gray-300 sm:text-sm" } %>
122
+ HTML
123
+ when :file_fields
124
+ field_html += <<-HTML
125
+ <div class="flex max-w-2xl justify-center rounded-lg border border-dashed border-gray-300 px-6 py-10">
126
+ <div class="text-center">
127
+ <!-- SVG Icon -->
128
+ <svg class="mx-auto size-12 text-gray-300" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true" data-slot="icon">
129
+ <path fill-rule="evenodd" d="M1.5 6a2.25 2.25 0 0 1 2.25-2.25h16.5A2.25 2.25 0 0 1 22.5 6v12a2.25 2.25 0 0 1-2.25 2.25H3.75A2.25 2.25 0 0 1 1.5 18V6ZM3 16.06V18c0 .414.336.75.75.75h16.5A.75.75 0 0 0 21 18v-1.94l-2.69-2.689a1.5 1.5 0 0 0-2.12 0l-.88.879.97.97a.75.75 0 1 1-1.06 1.06l-5.16-5.159a1.5 1.5 0 0 0-2.12 0L3 16.061Zm10.125-7.81a1.125 1.125 0 1 1 2.25 0 1.125 1.125 0 0 1-2.25 0Z" clip-rule="evenodd" />
130
+ </svg>
131
+ <div class="mt-4 flex text-sm text-gray-600">
132
+ <label for="<%= '#{input_id}' %>" class="relative cursor-pointer rounded-md bg-white font-semibold text-gray-600 hover:text-gray-500">
133
+ <span>Upload a file</span>
134
+ <%= form.file_field :#{field_name}, id: '#{input_id}', class: "sr-only", multiple: true %>
135
+ </label>
136
+ <p class="pl-1">or drag and drop</p>
137
+ </div>
138
+ <p class="text-xs text-gray-600">PNG, JPG, GIF, DOC etc.</p>
139
+ </div>
140
+ </div>
141
+ HTML
142
+ when :file_field
143
+ field_html += <<-HTML
144
+ <div class="flex max-w-2xl justify-center rounded-lg border border-dashed border-gray-300 px-6 py-10">
145
+ <div class="text-center">
146
+ <!-- SVG Icon -->
147
+ <svg class="mx-auto size-12 text-gray-300" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true" data-slot="icon">
148
+ <path fill-rule="evenodd" d="M1.5 6a2.25 2.25 0 0 1 2.25-2.25h16.5A2.25 2.25 0 0 1 22.5 6v12a2.25 2.25 0 0 1-2.25 2.25H3.75A2.25 2.25 0 0 1 1.5 18V6ZM3 16.06V18c0 .414.336.75.75.75h16.5A.75.75 0 0 0 21 18v-1.94l-2.69-2.689a1.5 1.5 0 0 0-2.12 0l-.88.879.97.97a.75.75 0 1 1-1.06 1.06l-5.16-5.159a1.5 1.5 0 0 0-2.12 0L3 16.061Zm10.125-7.81a1.125 1.125 0 1 1 2.25 0 1.125 1.125 0 0 1-2.25 0Z" clip-rule="evenodd" />
149
+ </svg>
150
+ <div class="mt-4 flex text-sm text-gray-600">
151
+ <label for="<%= '#{input_id}' %>" class="relative cursor-pointer rounded-md bg-white font-semibold text-gray-600 hover:text-gray-500">
152
+ <span>Upload a file</span>
153
+ <%= form.file_field :#{field_name}, id: '#{input_id}', class: "sr-only" %>
154
+ </label>
155
+ <p class="pl-1">or drag and drop</p>
156
+ </div>
157
+ <p class="text-xs text-gray-600">PNG, JPG, GIF, DOC etc.</p>
158
+ </div>
159
+ </div>
160
+ HTML
161
+ else
162
+ field_html += <<-HTML
163
+ <%= form.#{form_helper} :#{field_name}, id: '#{input_id}', class: "block w-full rounded-md border-gray-300 py-1.5 text-gray-700 shadow-sm focus:ring-2 focus:ring-gray-300 focus:border-gray-300 sm:max-w-md sm:text-sm" %>
164
+ HTML
165
+ end
166
+
167
+ field_html += <<-HTML
168
+ </div>
169
+ </div>
170
+ HTML
171
+
172
+ form_fields_html += field_html
173
+ end
174
+ form_fields_html
55
175
  end
56
176
 
57
177
  def build_usecase_filename(action, suffix = '')
@@ -67,6 +187,7 @@ module SunSword
67
187
  end
68
188
 
69
189
  def create_view_file
190
+ @form_fields_html = generate_form_fields_html
70
191
  template 'views/_form.html.erb.tt', File.join('app/views', @route_scope_path.to_s, @scope_path.to_s, '_form.html.erb')
71
192
  template 'views/edit.html.erb.tt', File.join('app/views', @route_scope_path.to_s, @scope_path.to_s, 'edit.html.erb')
72
193
  template 'views/index.html.erb.tt', File.join('app/views', @route_scope_path.to_s, @scope_path.to_s, 'index.html.erb')
@@ -74,23 +195,53 @@ module SunSword
74
195
  template 'views/show.html.erb.tt', File.join('app/views', @route_scope_path.to_s, @scope_path.to_s, 'show.html.erb')
75
196
  end
76
197
 
198
+ def namespace_exists?
199
+ routes_file = 'config/routes.rb'
200
+ scope_pattern = "namespace :#{@route_scope_path} do\n"
201
+ if File.exist?(routes_file)
202
+ file_content = File.read(routes_file)
203
+ file_content.include?(scope_pattern)
204
+ else
205
+ false
206
+ end
207
+ end
208
+
77
209
  def create_link_file
78
210
  template 'views/components/menu/link.html.erb.tt', File.join('app/views/components/menu', "_link_to_#{@scope_path}.html.erb")
79
211
  link_to = " <li><%= render 'components/menu/#{"link_to_#{@scope_path}"}' %></li>\n"
80
- inject_into_file 'app/views/layouts/dashboard/_sidebar.html.erb', link_to, before: " <%# generate_link %>\n"
212
+ inject_into_file 'app/views/components/layouts/_sidebar.html.erb', link_to, before: " <%# generate_link %>\n"
213
+ routes_file = 'config/routes.rb'
214
+ if !namespace_exists? && @route_scope_path.present?
215
+ scope_code = <<-RUBY
216
+ namespace :#{@route_scope_path} do
217
+ end
218
+ RUBY
219
+ insert_into_file routes_file, scope_code, after: "Rails.application.routes.draw do\n"
220
+ end
221
+ if @route_scope_path.present?
222
+ inject_into_file routes_file, " resources :#{@scope_path}\n", after: "namespace :#{@route_scope_path} do\n"
223
+ else
224
+ inject_into_file routes_file, " resources :#{@scope_path}\n", after: "Rails.application.routes.draw do\n"
225
+ end
81
226
  end
82
227
 
83
228
  def contract_fields
84
229
  skip_contract_fields = @skipped_fields.map(&:strip).uniq
85
- if RiderKick.scope_owner_column.present?
86
- skip_contract_fields << RiderKick.scope_owner_column.to_s
230
+ if SunSword.scope_owner_column.present?
231
+ skip_contract_fields << SunSword.scope_owner_column.to_s
87
232
  end
88
233
  @model_class.columns.reject { |column| skip_contract_fields.include?(column.name.to_s) }.map(&:name).map(&:to_s)
89
234
  end
90
235
 
91
236
  def strong_params
92
237
  results = ''
93
- @controllers.form_fields.map { |tc| results << ":#{tc}," }
238
+ @controllers.form_fields.each do |field|
239
+ if field.type.to_s.eql?('files')
240
+ results << "{ #{field.name}: [] }, "
241
+ else
242
+ results << ":#{field.name}, "
243
+ end
244
+ end
94
245
  results[0..-2]
95
246
  end
96
247
  end