svelte-on-rails 8.0.1 → 9.0.0

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 (54) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +17 -8
  3. data/lib/generators/showcase_generator.rb +128 -0
  4. data/lib/generators/svelte_on_rails/install/install_generator.rb +1 -111
  5. data/lib/svelte-on-rails.rb +1 -1
  6. data/lib/svelte_on_rails/active_record_extensions.rb +15 -4
  7. data/lib/svelte_on_rails/installer/hello_world.rb +7 -26
  8. data/lib/svelte_on_rails/installer/utils.rb +2 -14
  9. data/lib/svelte_on_rails/lib/development_utils.rb +0 -29
  10. data/lib/svelte_on_rails/lib/to_svelte.rb +37 -7
  11. data/lib/svelte_on_rails/lib/utils.rb +6 -0
  12. data/lib/svelte_on_rails/lib/view_helper_support.rb +2 -0
  13. data/lib/svelte_on_rails/renderer/render-backup.js +26 -0
  14. data/lib/svelte_on_rails/renderer/render.js +61 -13
  15. data/lib/svelte_on_rails/renderer/renderer.rb +7 -4
  16. data/lib/svelte_on_rails/renderer/utils.js +40 -40
  17. data/lib/svelte_on_rails/view_helpers.rb +3 -3
  18. data/lib/tasks/svelte_on_rails_tasks.rake +13 -51
  19. data/templates/config_base/config/svelte_on_rails.yml +1 -1
  20. data/templates/config_base/vite-ssr.config.ts +8 -3
  21. data/templates/showcase/app/channels/application_cable/channel.rb +4 -0
  22. data/templates/showcase/app/channels/application_cable/connection.rb +5 -0
  23. data/templates/showcase/app/controllers/svelte_on_rails_showcase_controller.rb +65 -0
  24. data/templates/showcase/app/frontend/javascript/svelte_on_rails_showcase/JavascriptImport.svelte +7 -0
  25. data/templates/showcase/app/frontend/javascript/svelte_on_rails_showcase/JpgImport.svelte +15 -0
  26. data/templates/{all_features_test/app/frontend/javascript/components → showcase/app/frontend/javascript/svelte_on_rails_showcase}/ParentWithChild.svelte +1 -1
  27. data/templates/showcase/app/frontend/javascript/svelte_on_rails_showcase/PngImport.svelte +16 -0
  28. data/templates/{all_features_test/app/frontend/javascript/components → showcase/app/frontend/javascript/svelte_on_rails_showcase}/ReceiveFromChannel.svelte +5 -3
  29. data/templates/{all_features_test/app/frontend/javascript/components → showcase/app/frontend/javascript/svelte_on_rails_showcase}/SvelteOnRailsHelloWorld.svelte +4 -4
  30. data/templates/showcase/app/frontend/javascript/svelte_on_rails_showcase/SvgRawImport.svelte +7 -0
  31. data/templates/showcase/app/views/svelte_on_rails_showcase/_nav.html.erb +13 -0
  32. data/templates/showcase/app/views/svelte_on_rails_showcase/backend_frontend_rendered.html.erb +37 -0
  33. data/templates/showcase/app/views/svelte_on_rails_showcase/index.html.erb +9 -0
  34. data/templates/{all_features_test/app/views/svelte_on_rails_hello_world → showcase/app/views/svelte_on_rails_showcase}/ssr_auto_rendered.html.erb +2 -2
  35. data/templates/{all_features_test/app/views/svelte_on_rails_hello_world → showcase/app/views/svelte_on_rails_showcase}/web_socket.html.erb +1 -1
  36. metadata +27 -24
  37. data/lib/svelte_on_rails/installer/vite.rb +0 -95
  38. data/templates/all_features_test/app/controllers/svelte_on_rails_hello_world_controller.rb +0 -62
  39. data/templates/all_features_test/app/frontend/javascript/components/JavascriptImport.svelte +0 -7
  40. data/templates/all_features_test/app/frontend/javascript/components/JpgImport.svelte +0 -7
  41. data/templates/all_features_test/app/frontend/javascript/components/PngImport.svelte +0 -7
  42. data/templates/all_features_test/app/frontend/javascript/components/SvgRawImport.svelte +0 -7
  43. data/templates/all_features_test/app/views/svelte_on_rails_hello_world/_nav.html.erb +0 -13
  44. data/templates/all_features_test/app/views/svelte_on_rails_hello_world/backend_frontend_rendered.html.erb +0 -37
  45. data/templates/all_features_test/app/views/svelte_on_rails_hello_world/index.html.erb +0 -9
  46. /data/templates/{all_features_test → showcase}/app/channels/svelte_on_rails_channel.rb +0 -0
  47. /data/templates/{all_features_test → showcase}/app/frontend/initializers/actionCable.js +0 -0
  48. /data/templates/{all_features_test/app/frontend/images/svelte-on-rails-hello-world-england.png → showcase/app/frontend/javascript/svelte_on_rails_showcase/england.png} +0 -0
  49. /data/templates/{all_features_test/app/frontend/images/svelte-on-rails-hello-world-face-smile-wink.svg → showcase/app/frontend/javascript/svelte_on_rails_showcase/face-smile-wink.svg} +0 -0
  50. /data/templates/{all_features_test/app/frontend/javascript → showcase/app/frontend/javascript/svelte_on_rails_showcase}/nestedJavascript.js +0 -0
  51. /data/templates/{all_features_test/app/frontend/javascript → showcase/app/frontend/javascript/svelte_on_rails_showcase}/nestedJavascriptToggled.js +0 -0
  52. /data/templates/{all_features_test/app/frontend/javascript/components → showcase/app/frontend/javascript/svelte_on_rails_showcase}/sub/NestedComponent.svelte +0 -0
  53. /data/templates/{all_features_test/app/frontend/images/svelte-on-rails-hello-world-switzerland.jpg → showcase/app/frontend/javascript/svelte_on_rails_showcase/switzerland.jpg} +0 -0
  54. /data/templates/{all_features_test/app/views/svelte_on_rails_hello_world → showcase/app/views/svelte_on_rails_showcase}/_styles.html.erb +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9fd78748cd0a7f9c896123e31dbbccab10dc7d42bc394a14fbc66efa360a166d
4
- data.tar.gz: ce14713af9654cdbeb0e2ddf5cb9e46174308d9018c668b97f1db47b083e0324
3
+ metadata.gz: c7247500d2d0c44d3c91cf1bfa5aec7861ec4306928fdfecbcc42e79fda5e63a
4
+ data.tar.gz: 5668e8ec34a3d12fb794641fc763162aa11b1ba2611f4b6fd4145762f743e11e
5
5
  SHA512:
6
- metadata.gz: 033d0d7cb3a2147119668913978f8a8fdba37d0467b7e7c756afaf60609352ed6bb2aa3805c1640c81a78dedb325eda7e298410c3df4485ed6f0fc7d796fa33c
7
- data.tar.gz: f1b1830ad7056f1dff09077ba6576c6d64db6a8f27bfbfb9cc2dad70f128d0078137b9fbb970179c6702c677f443316f69c2d4f7e32dec33cca7aa37d6a8c6f9
6
+ metadata.gz: 806a37910e007388a2d89e10d370ca144e46180c78138d1eb7ee05852edec90fc43436f452c64d5901881eb77c1567c416406cff7ecb1e9102a4906a9ba56918
7
+ data.tar.gz: d2f5c37db64f31f061cca10132a4c2b1e70773e3377cce4502a411bcf23afbafad7e98809e9e2f08a933375a721df48a96dabbd12fd3b869dc0fbb88f2b6e41c
data/README.md CHANGED
@@ -24,7 +24,7 @@ See: [Comparitions on the guide](https://svelte-on-rails.dev/about/why.html)
24
24
 
25
25
  see [issues](https://gitlab.com/sedl/svelte-on-rails/-/issues)
26
26
 
27
- # Description 👍
27
+ # Description
28
28
 
29
29
  Renders Svelte components server-side and, together with [@csedl/svelte-on-rails](https://www.npmjs.com/package/@csedl/svelte-on-rails),
30
30
  hydrates the component on the frontend.
@@ -37,13 +37,16 @@ If you have issues, please open one, and contributors are welcome!
37
37
  ## Requirements
38
38
 
39
39
  - tested on
40
- - ruby 3.2.2 and rails 7.1
41
- - ruby 2.7.5 and rails 6.1
42
- - vite@6 (v7 not supported, see issues)
40
+ - ruby 3.4.2 / Rails 8.1.2
41
+ - ruby 3.2.2 / rails 7.1
42
+ - ruby 2.7.5 / rails 6.1
43
+ - vite@6 (v7 not yet tested, see issues)
43
44
  - turbolinks and hotwired/turbo
44
45
  - vite_rails (the installer will install it by option --full or --vite)
45
46
  - svelte@5, @sveltejs/vite-plugin-svelte@5 (see: [how to install svelte on rails/vite](https://dev.to/chmich/setup-inertia-and-svelte-on-rails-7-3glk))
47
+ - since gem-version 9, svelte runes-mode is set to true for ssr, check svelte-5 migration guide
46
48
  - turbo (recommended / [how to install turbo on rails](https://github.com/hotwired/turbo-rails?tab=readme-ov-file#installation))
49
+ - this is because of using ssr for initial-load only but that feature is also available for turbo-links.
47
50
  - npm on latest versions
48
51
  - actual node installed.
49
52
  - if `nvm` is installed it gets the path to the node-binary from there.
@@ -55,7 +58,9 @@ If you have issues, please open one, and contributors are welcome!
55
58
  On ruby <3 Arguments are misinterpreted, see [issue-10](https://gitlab.com/sedl/svelte-on-rails/-/issues/10)
56
59
 
57
60
 
58
- ## Installation
61
+ ## Installation on a existing app
62
+
63
+ => To generate a two-minute, full-featured app, refer to the [guide/installer with options](https://svelte-on-rails.dev/installation/installer_options.html).
59
64
 
60
65
  Required: `vite_rails` must be installed, it wants a `app/frontend` folder.
61
66
 
@@ -67,7 +72,7 @@ Within the app, add the gem
67
72
  bundle add svelte-on-rails
68
73
  ```
69
74
 
70
- and run the minimal installer (you are free to add any options from above)
75
+ and run the installer without options
71
76
 
72
77
  ```bash
73
78
  rails g svelte_on_rails:install
@@ -89,13 +94,17 @@ Add it to the view
89
94
  <%= svelte_component('HelloWorld', {title: 'Hello World'}) %>
90
95
  ```
91
96
 
92
- And you will see "Svelte Hello World" on the browser! 👍 🤗
97
+ And you will see «Svelte Hello World» on the browser! 👍 🤗
93
98
 
94
99
  # Contributors welcome
95
100
 
96
101
  see [Guide / run your first test](https://svelte-on-rails.dev/first_test.html)
97
102
 
103
+ # Special Thanks
104
+
105
+ - [vite_ruby](https://github.com/ElMassimo/vite_ruby)
106
+ - [react_on_rails](https://github.com/shakacode/react_on_rails)
98
107
 
99
108
  ## Licence
100
109
 
101
- Copyright © 2025-2028 sedlmair.ch. Distributed by [MIT License](https://svelte-on-rails.dev/license.html)
110
+ Copyright © 2025-2027 sedlmair.ch. Distributed by [MIT License](https://svelte-on-rails.dev/license.html)
@@ -0,0 +1,128 @@
1
+ require "rails/generators"
2
+
3
+ module SvelteOnRails
4
+ module Generators
5
+ class ShowcaseGenerator < Rails::Generators::Base
6
+
7
+ class_option :full, type: :boolean, default: false, desc: "Run full installation with additional setup"
8
+ class_option :force, type: :boolean, default: false, desc: "Do not ask for overwriting files"
9
+ class_option :turbo, type: :boolean, default: false, desc: "Use @hotwired/turbo-rails"
10
+ class_option :turbo_streams, type: :boolean, default: false, desc: "Turbo::StreamsChannel"
11
+ class_option :hello_world, type: :boolean, default: false, desc: "Create Hello World component"
12
+
13
+ require 'svelte_on_rails/installer/utils'
14
+ require 'svelte_on_rails/installer/haml'
15
+ require 'svelte_on_rails/lib/development_utils'
16
+
17
+ NPM_PACKAGE_NAME = '@csedl/svelte-on-rails'
18
+
19
+ def initialize(*args)
20
+
21
+ super
22
+
23
+ puts
24
+ puts '-' * 80
25
+ puts '-' * 80
26
+ puts 'INSTALLING DEMO COMPONENTS'
27
+ puts '-' * 80
28
+
29
+ end
30
+
31
+
32
+
33
+ def turbo
34
+
35
+ puts '-' * 80
36
+ puts ' ▶︎▶︎▶︎ INSTALLING @hotwired/turbo-rails'
37
+ puts '-' * 80
38
+ npm_i = SvelteOnRails::Installer::Npm
39
+ tr_pkg = '@hotwired/turbo-rails'
40
+ npm_i.install_or_update_package(tr_pkg)
41
+ js_i = SvelteOnRails::Installer::Javascript
42
+ js_i.append_import_statement(application_js_path, tr_pkg, "import '#{tr_pkg}';")
43
+
44
+ puts '-' * 80
45
+ puts ' ▶︎▶︎▶︎ INSTALLING Turbo::StreamsChannel'
46
+ puts '-' * 80
47
+
48
+ gem_uts = SvelteOnRails::GemUtils
49
+ puts '• installing turbo-rails ...'
50
+ gem_uts.install_gem('turbo-rails')
51
+ puts '• installed turbo-rails!'
52
+ utils_i = SvelteOnRails::Installer::Utils
53
+ puts '• running turbo:install ...'
54
+ utils_i.run_command('bundle exec rails turbo:install', success_message: 'You must import')
55
+ puts '• turbo:install finished!'
56
+ end
57
+
58
+
59
+
60
+
61
+ def hello_world
62
+
63
+ puts '-' * 80
64
+ puts ' ▶︎▶︎▶︎︎ INSTALLING Hello World component'
65
+ puts '-' * 80
66
+
67
+ hw_i = SvelteOnRails::Installer::HelloWorld
68
+
69
+ utils_i = SvelteOnRails::Installer::Utils
70
+ utils_i.add_route(" get \"svelte_on_rails_showcase/web_socket\"", app_root: Rails.root)
71
+ utils_i.add_route(" get \"svelte_on_rails_showcase/web_socket_action\"", app_root: Rails.root)
72
+ npm_i = SvelteOnRails::Installer::Npm
73
+ npm_i.install_or_update_package('axios')
74
+ npm_i.install_or_update_package('@rails/actioncable')
75
+ js_i = SvelteOnRails::Installer::Javascript
76
+ init_stat = '../initializers/actionCable.js'
77
+ js_i.append_import_statement(application_js_path, init_stat, "import '#{init_stat}';")
78
+ @hello_world_path = hw_i.install_hello_world(['showcase'], app_root: nil, force: true, silent: true)
79
+ end
80
+
81
+
82
+
83
+
84
+ def finish
85
+ puts '-' * 80
86
+
87
+ puts "SvelteOnRails Showcase installed successfully!"
88
+ puts "Restart the server and check if it all works."
89
+ if @hello_world_path
90
+ puts "You can now access the Hello World component on: http://localhost:your-port-number/svelte_on_rails_showcase."
91
+ end
92
+ puts "Happy coding!"
93
+ end
94
+
95
+ private
96
+
97
+ def toggle_hello_world_files
98
+ %w[
99
+ app/frontend/images/svg.svg
100
+ app/frontend/images/atom.svg
101
+ app/frontend/javascript/nestedJavascript.js
102
+ app/frontend/javascript/nestedJavascriptToggled.js
103
+ ]
104
+ end
105
+
106
+ # def validate_raw_options!(args)
107
+ # # Get option names from class_options, excluding inherited Thor/Rails options
108
+ # valid_options = self.class.class_options.keys.map { |opt| "--#{opt.to_s.tr('_', '-')}" }
109
+ # rails_internal_options = %w[--skip-namespace --skip-collision-check --pretend --quiet --force]
110
+ # options_array = args.find { |arg| arg.is_a?(Array) && arg.any? { |a| a.start_with?('--') } } || []
111
+ # passed_options = options_array.select { |arg| arg.start_with?('--') }
112
+ # unknown_options = passed_options - valid_options - rails_internal_options
113
+ # unless unknown_options.empty?
114
+ # valid_display = valid_options.map { |opt| opt.gsub(/^--/, '') }.join(', ')
115
+ # raise Thor::Error, "Unknown options: #{unknown_options.join(', ')}. Valid options are: #{valid_display}\nNothing done."
116
+ # end
117
+ #
118
+ # if options[:turbo_streams] && !(options[:full] || options[:turbo])
119
+ # raise Thor::Error, "--turbo-streams option cannot be used without --turbo"
120
+ # end
121
+ # end
122
+
123
+ def application_js_path
124
+ Rails.root.join("app", "frontend", "entrypoints", "application.js")
125
+ end
126
+ end
127
+ end
128
+ end
@@ -6,12 +6,8 @@ module SvelteOnRails
6
6
 
7
7
  class_option :full, type: :boolean, default: false, desc: "Run full installation with additional setup"
8
8
  class_option :force, type: :boolean, default: false, desc: "Do not ask for overwriting files"
9
- class_option :vite, type: :boolean, default: false, desc: "Use Vite"
10
- class_option :haml, type: :boolean, default: false, desc: "Use Haml"
11
9
  class_option :turbo, type: :boolean, default: false, desc: "Use @hotwired/turbo-rails"
12
10
  class_option :turbo_streams, type: :boolean, default: false, desc: "Turbo::StreamsChannel"
13
- class_option :svelte, type: :boolean, default: false, desc: "Install Svelte"
14
- class_option :pug, type: :boolean, default: false, desc: "Install Pug"
15
11
  class_option :hello_world, type: :boolean, default: false, desc: "Create Hello World component"
16
12
 
17
13
  require 'svelte_on_rails/installer/utils'
@@ -24,29 +20,16 @@ module SvelteOnRails
24
20
 
25
21
  super
26
22
 
27
- @local_npm_package_url = SvelteOnRails::DevelopmentUtils.local_npm_package_url
28
-
29
23
  puts
30
24
  validate_raw_options!(args)
31
25
  puts '-' * 80
32
26
  puts '-' * 80
33
27
  puts 'STARTING SVELTE-ON-RAILS INSTALLATION'
34
28
  puts " • FORCED (option --force was given)" if options[:force]
35
- if @local_npm_package_url
36
- puts " • Local Npm Package used: #{NPM_PACKAGE_NAME} used: #{@local_npm_package_url}"
37
- end
38
29
  puts '-' * 80
39
30
 
40
31
  end
41
32
 
42
- def vite
43
- return unless options[:full] || options[:vite]
44
- puts '-' * 80
45
- puts '• INSTALLING VITE-RAILS'
46
- vite_i = SvelteOnRails::Installer::Vite
47
- vite_i.install_vite(version_specifier: '6')
48
- end
49
-
50
33
  def svelte_on_rails
51
34
 
52
35
  puts '-' * 80
@@ -61,14 +44,7 @@ module SvelteOnRails
61
44
  pkg_js['scripts'] ||= { 'build:ssr': "vite build --config vite-ssr.config.ts" }
62
45
  File.write('package.json', JSON.pretty_generate(pkg_js))
63
46
 
64
- if @local_npm_package_url
65
- npm_i.link_local_package(NPM_PACKAGE_NAME, @local_npm_package_url)
66
-
67
- # npm does not resolve peer dependencies on local package url
68
- npm_i.install_or_update_package('@hotwired/stimulus')
69
- else
70
- npm_i.install_or_update_package(NPM_PACKAGE_NAME)
71
- end
47
+ npm_i.install_or_update_package(NPM_PACKAGE_NAME)
72
48
  npm_i.install_or_update_package('typescript')
73
49
  npm_i.install_or_update_package('@types/node')
74
50
 
@@ -77,92 +53,6 @@ module SvelteOnRails
77
53
  js_i.append_import_statement(application_js_path, init_stat, "import '#{init_stat}';")
78
54
  end
79
55
 
80
- def turbo
81
- return unless options[:full] || options[:turbo]
82
-
83
- puts '-' * 80
84
- puts ' ▶︎▶︎▶︎ INSTALLING @hotwired/turbo-rails'
85
- puts '-' * 80
86
- npm_i = SvelteOnRails::Installer::Npm
87
- tr_pkg = '@hotwired/turbo-rails'
88
- npm_i.install_or_update_package(tr_pkg)
89
- js_i = SvelteOnRails::Installer::Javascript
90
- js_i.append_import_statement(application_js_path, tr_pkg, "import '#{tr_pkg}';")
91
-
92
- return unless options[:turbo_streams] || options[:full]
93
-
94
- puts '-' * 80
95
- puts ' ▶︎▶︎▶︎ INSTALLING Turbo::StreamsChannel'
96
- puts '-' * 80
97
-
98
- gem_uts = SvelteOnRails::GemUtils
99
- puts '• installing turbo-rails ...'
100
- gem_uts.install_gem('turbo-rails')
101
- puts '• installed turbo-rails!'
102
- utils_i = SvelteOnRails::Installer::Utils
103
- puts '• running turbo:install ...'
104
- utils_i.run_command('bundle exec rails turbo:install', success_message: 'You must import')
105
- puts '• turbo:install finished!'
106
- end
107
-
108
- def svelte
109
- return unless options[:svelte] || options[:full]
110
-
111
- puts '-' * 80
112
- puts ' ▶︎▶︎▶︎ INSTALLING svelte (npm package)'
113
-
114
- puts '-' * 80
115
- svelte_i = SvelteOnRails::Installer::Svelte
116
- svelte_i.install_svelte(
117
- svelte_version_specifier: '5',
118
- vite_plugin_svelte_version_specifier: '5'
119
- )
120
- end
121
-
122
- def hello_world
123
- return unless options[:hello_world] || options[:full]
124
-
125
- puts '-' * 80
126
- puts ' ▶︎▶︎▶︎︎ INSTALLING Hello World component'
127
- puts '-' * 80
128
-
129
- hw_i = SvelteOnRails::Installer::HelloWorld
130
-
131
- utils_i = SvelteOnRails::Installer::Utils
132
- utils_i.add_route(" get \"svelte_on_rails_hello_world/web_socket\"", app_root: Rails.root)
133
- utils_i.add_route(" get \"svelte_on_rails_hello_world/web_socket_action\"", app_root: Rails.root)
134
- npm_i = SvelteOnRails::Installer::Npm
135
- npm_i.install_or_update_package('axios')
136
- npm_i.install_or_update_package('@rails/actioncable')
137
- js_i = SvelteOnRails::Installer::Javascript
138
- init_stat = '../initializers/actionCable.js'
139
- js_i.append_import_statement(application_js_path, init_stat, "import '#{init_stat}';")
140
- @hello_world_path = hw_i.install_hello_world(['all_features_test'], app_root: nil, force: true, silent: true)
141
- end
142
-
143
- def haml_and_convert
144
- return unless options[:haml]
145
-
146
- puts '-' * 80
147
- puts ' ▶︎▶︎▶︎ INSTALLING Haml and converting existing .erb files to .haml'
148
-
149
- haml_i = SvelteOnRails::Installer::Haml
150
- haml_i.install_haml_and_convert(force: options[:force])
151
- end
152
-
153
- def finish
154
- puts '-' * 80
155
- puts ' 👍 FINISHED SVELTE-ON-RAILS INSTALLATION 👍'
156
- puts '-' * 80
157
-
158
- puts "SvelteOnRails installed successfully!"
159
- puts "Restart the server and check if it all works."
160
- if @hello_world_path
161
- puts "You can now access the Hello World component on: http://localhost:your-port-number#{@hello_world_path}."
162
- end
163
- puts "Happy coding!"
164
- end
165
-
166
56
  private
167
57
 
168
58
  def toggle_hello_world_files
@@ -16,13 +16,13 @@ require "svelte_on_rails/lib/to_svelte"
16
16
  require 'svelte_on_rails/installer/utils'
17
17
  require 'svelte_on_rails/installer/haml'
18
18
  require 'svelte_on_rails/installer/gem_utils'
19
- require 'svelte_on_rails/installer/vite'
20
19
  require 'svelte_on_rails/installer/svelte'
21
20
  require 'svelte_on_rails/installer/npm'
22
21
  require 'svelte_on_rails/installer/javascript'
23
22
  require 'svelte_on_rails/installer/hello_world'
24
23
 
25
24
  require 'generators/svelte_on_rails/install/install_generator'
25
+ require 'generators/showcase_generator'
26
26
 
27
27
  module SvelteOnRails
28
28
  class << self
@@ -9,8 +9,12 @@ module SvelteOnRails
9
9
  @to_svelte ||= begin
10
10
 
11
11
  cl = SvelteOnRails::Lib::SvelteAttributes
12
- r = cl.new.calculate_instance(self, attributes, associations)
13
- r.first.merge({ self.class.to_s.underscore => r[1] })
12
+ lab, enums, val = cl.new.calculate_instance(self, attributes, associations)
13
+ k = self.class.to_s.underscore
14
+ res = lab
15
+ res["#{k}_enums"] = enums if enums
16
+ res[k] = val
17
+ res
14
18
 
15
19
  end
16
20
  end
@@ -25,7 +29,10 @@ module SvelteOnRails
25
29
 
26
30
  @to_svelte ||= begin
27
31
  cl = SvelteOnRails::Lib::SvelteAttributes
28
- cl.new.calculate_class(self, attributes, associations)
32
+ lab, enums = cl.new.calculate_class(self, attributes, associations)
33
+ lab["#{self.to_s.underscore}_enums"] = enums if enums
34
+ lab
35
+
29
36
  end
30
37
  end
31
38
 
@@ -38,7 +45,11 @@ module SvelteOnRails
38
45
  def to_svelte(*attributes, **associations)
39
46
  @to_svelte ||= begin
40
47
  cl = SvelteOnRails::Lib::SvelteAttributes
41
- cl.new.calculate_relation(self, attributes, associations)
48
+ lab, enums, val = cl.new.calculate_relation(self, attributes, associations)
49
+ k = self.klass.to_s.underscore
50
+ lab[k.pluralize] = val
51
+ lab["#{k}_enums"] = enums if enums
52
+ lab
42
53
  end
43
54
  end
44
55
 
@@ -10,35 +10,16 @@ module SvelteOnRails
10
10
 
11
11
  utils_i.write_templates(templates, ask_for_overwrite: !force, app_root: app_root, silent: silent)
12
12
 
13
- # route
14
-
15
- route = 'svelte_on_rails_hello_world#index'
16
-
17
- utils_i.add_route(" get \"#{route.sub('#', '/')}\"", app_root: app_root)
18
- utils_i.add_route(" get \"svelte_on_rails_hello_world/backend_frontend_rendered\"", app_root: app_root)
19
- utils_i.add_route(" get \"svelte_on_rails_hello_world/ssr_auto_rendered\"", app_root: app_root)
20
-
21
- rr = utils_i.which_root_route(app_root)
22
- root_url = "/"
23
- url = root_url + route.sub('#', '/')
24
-
25
- if rr && rr == route
26
- puts "Root route «#{route}» already exists, skipping."
27
- root_url
28
- elsif rr && utils_i.route_exists?(route.sub('#', '/'))
29
- puts "route «#{route}» already exists, skipping."
30
- url
31
- elsif rr
32
- url
33
- else
34
- utils_i.add_route(' root "svelte_on_rails_hello_world#index"', app_root: app_root)
35
- root_url
36
- end
13
+ # routes
14
+
15
+ utils_i.add_route(" get \"svelte_on_rails_showcase/backend_frontend_rendered\"", app_root: app_root)
16
+ utils_i.add_route(" get \"svelte_on_rails_showcase/ssr_auto_rendered\"", app_root: app_root)
17
+ utils_i.add_route(" get \"svelte_on_rails_showcase\", to: \"svelte_on_rails_showcase#index\"", app_root: app_root)
37
18
 
38
19
  end
39
20
 
40
21
 
41
- def self.remove_hello_world(templates = ['all_features_test'], app_root: nil, ask: true)
22
+ def self.remove_hello_world(templates = ['showcase'], app_root: nil, ask: true)
42
23
 
43
24
  utils = SvelteOnRails::Installer::Utils
44
25
 
@@ -58,7 +39,7 @@ module SvelteOnRails
58
39
 
59
40
  utils.remove_line_from_file(
60
41
  utils.app_root_path(app_root).join('config/routes.rb'),
61
- "svelte_on_rails_hello_world",
42
+ "svelte_on_rails_showcase",
62
43
  force: !ask,
63
44
  )
64
45
 
@@ -5,12 +5,11 @@ module SvelteOnRails
5
5
  module Utils
6
6
  def self.install_npm_package
7
7
  package_name = "@csedl/svelte-on-rails@latest"
8
- puts "Installing #{package_name} via npm..."
9
8
 
10
9
  if system("npm install #{package_name}")
11
- puts "#{package_name} successfully installed."
10
+ puts "Installed successfully: «#{package_name}»"
12
11
  else
13
- abort "Failed to install #{package_name}. Please ensure npm is installed and try running 'npm install #{package_name}' manually."
12
+ abort "Failed to install «#{package_name}» by npm. You must manually."
14
13
  end
15
14
  end
16
15
 
@@ -243,17 +242,6 @@ module SvelteOnRails
243
242
  continue == 'y'
244
243
  end
245
244
 
246
- def self.which_root_route(app_root = nil)
247
-
248
- # Check if the root route is active (uncommented) or commented out
249
-
250
- routes = File.read(app_root_path(app_root).join('config', 'routes.rb'))
251
- m = routes.match(/^\s*root\s+['"]([^'"]+)['"]/m)
252
- if m
253
- m.to_s.match(/^\s*root\s*['"]([^'"]*)['"]/)[1]
254
- end
255
- end
256
-
257
245
  def self.add_route(route, app_root: nil)
258
246
 
259
247
  file_path = app_root_path(app_root).join('config/routes.rb')
@@ -1,38 +1,9 @@
1
1
  module SvelteOnRails
2
2
  class DevelopmentUtils
3
3
 
4
- def self.local_npm_package_url
5
- str = contributor_config('local_npm_package_path', required: false)
6
- if !str.present?
7
- nil
8
- elsif !Dir.exist?(str)
9
- raise "Invalid path given on local_npm_package_path: «#{str}»"
10
- elsif !File.exist?(Pathname.new(str).join('package.json'))
11
- raise "Given local_npm_package_path does not seem to be a valid npm package as it does not contain a package.json"
12
- else
13
- return Pathname.new(str)
14
- end
15
- end
16
-
17
4
  def self.gem_folder
18
5
  Pathname.new(File.expand_path('../../..', __dir__))
19
6
  end
20
7
 
21
- def self.contributor_config(key, required: true)
22
- config_file = File.expand_path('../svelte_on_rails_contributor_configs.yml', gem_folder)
23
- if !File.exist?(config_file)
24
- if required
25
- raise "Missing configuration file, searched at: «#{config_file}»"
26
- end
27
- else
28
- yml = YAML.load_file(config_file)
29
- value = yml[key.to_s]
30
- if required && !value.present?
31
- raise "Missing value «#{key}» in configuration file: «#{config_file}»"
32
- end
33
- value
34
- end
35
- end
36
-
37
8
  end
38
9
  end
@@ -26,8 +26,6 @@ module SvelteOnRails
26
26
  record
27
27
  end
28
28
 
29
- # set_labels(record.first, attributes)
30
-
31
29
  values = recs2.map do |rec|
32
30
  calculate_instance(rec, attributes, associations, call_stack: next_stack)
33
31
  end
@@ -46,6 +44,8 @@ module SvelteOnRails
46
44
  _key = attr.to_s
47
45
 
48
46
  values[_key] = record.send(_key)
47
+
48
+ fetch_enum(record.class, attr)
49
49
  end
50
50
 
51
51
  associations.each do |key, val|
@@ -71,7 +71,7 @@ module SvelteOnRails
71
71
  if recs.respond_to?(:each)
72
72
  values[_key] = calculate_instance(
73
73
  recs,
74
- val.reject{|v|v.is_a?(Hash)},
74
+ val.reject { |v| v.is_a?(Hash) },
75
75
  {},
76
76
  call_stack: next_stack,
77
77
  offset: _offset,
@@ -95,6 +95,7 @@ module SvelteOnRails
95
95
  else
96
96
  [
97
97
  @labels,
98
+ @enums,
98
99
  values
99
100
  ]
100
101
  end
@@ -106,6 +107,9 @@ module SvelteOnRails
106
107
  next_stack = call_stack + 1
107
108
 
108
109
  set_labels(model, attributes, associations)
110
+ attributes.each do |attr|
111
+ fetch_enum(model, attr)
112
+ end
109
113
 
110
114
  associations.each do |key, value|
111
115
  reflect = model.reflect_on_association(key.to_s)
@@ -120,19 +124,28 @@ module SvelteOnRails
120
124
  end
121
125
 
122
126
  if call_stack == 0
123
- @labels
127
+ [
128
+ @labels,
129
+ @enums
130
+ ]
124
131
  end
132
+
125
133
  end
126
134
 
127
135
  def calculate_relation(relation, attributes, associations)
128
136
  set_labels(relation.klass, attributes)
137
+ attributes.each do |attr|
138
+ fetch_enum(relation.klass, attr)
139
+ end
129
140
  r = relation.map do |rec|
130
141
  calculate_instance(rec, attributes, associations, call_stack: 1)
131
142
  end
132
143
 
133
- @labels.merge({
134
- relation.klass.to_s.underscore.pluralize => r
135
- })
144
+ [
145
+ @labels,
146
+ @enums,
147
+ r
148
+ ]
136
149
 
137
150
  end
138
151
 
@@ -188,6 +201,23 @@ module SvelteOnRails
188
201
  end
189
202
  end
190
203
 
204
+ def fetch_enum(model, attribute)
205
+ return unless model.respond_to?(:defined_enums)
206
+ raise ArgumentError, "attribute_name must be a symbol" unless attribute.is_a?(Symbol)
207
+
208
+ enums = model.defined_enums
209
+ return nil unless enums.key?(attribute.to_s)
210
+
211
+ @enums ||= {}
212
+ @enums[attribute.to_s] = enums[attribute.to_s].map do |k,v|
213
+ translation_key = "activerecord.attributes.#{model.model_name.i18n_key}.#{attribute}/#{k}"
214
+ [
215
+ k,
216
+ I18n.t(translation_key, default: k.humanize)
217
+ ]
218
+ end
219
+ end
220
+
191
221
  end
192
222
  end
193
223
  end
@@ -69,6 +69,7 @@ module SvelteOnRails
69
69
  errors_matcher = Regexp.new('(Could not resolve|failed to resolve)')
70
70
  error_lines = warnings.select { |e| e.match(errors_matcher) }
71
71
  have_error = error_lines.present? || status.to_s.match(/exit 1/)
72
+ copy_render_js
72
73
 
73
74
  # error handling
74
75
 
@@ -118,6 +119,11 @@ module SvelteOnRails
118
119
 
119
120
  end
120
121
 
122
+ def self.copy_render_js
123
+ config = SvelteOnRails::Configuration.instance
124
+ FileUtils.cp(gem_app_dir + 'renderer/render.js', config.ssr_dist_folder.join('render.js'))
125
+ end
126
+
121
127
  # Defining methods to log errors and warnings using Rails logger
122
128
  def self.puts_error(text)
123
129
  # Using Rails logger to log error with custom formatting
@@ -9,6 +9,8 @@ module SvelteOnRails
9
9
 
10
10
  def initialize(file, props, html_options, options, request, caching = false)
11
11
 
12
+ require 'zlib'
13
+
12
14
  @start_time = Time.now
13
15
  @conf = SvelteOnRails::Configuration.instance
14
16
  utils = SvelteOnRails::Lib::Utils