Browse Source

load_definitions

main
pvincent 4 months ago
parent
commit
3d02f81a5a
  1. 2
      .env.sample
  2. 1
      .vscode/settings.json
  3. 7
      app/controllers/hot_controller.rb
  4. 17
      app/controllers/strong_controller.rb
  5. 4
      app/views/hot/index.html.erb
  6. 2
      app/views/layouts/_navbar.html.erb
  7. 2
      app/views/layouts/application.html.erb
  8. 34
      app/views/strong/index.html.erb
  9. 4
      config/application.rb
  10. 8
      config/initializers/hot_changes.rb
  11. 2
      config/initializers/rails_live_reload.rb
  12. 3
      config/initializers/strong_parameters.rb
  13. 6
      config/routes.rb
  14. 11
      lib/hot/constants.rb
  15. 8
      lib/hot/live.rb
  16. 2
      lib/monkey_patches/rails_live_reload/watcher.rb

2
.env.sample

@ -14,7 +14,7 @@ LOG_ACTION_VIEW=false
# with default value, type gets inferred from # with default value, type gets inferred from
INTEGER=a INTEGER=a
STRING=456
STRING=457
BOOLEAN=false BOOLEAN=false
# with no default value (evaluated to nil), type must be defined expressively otherwise it reverts to a string type # with no default value (evaluated to nil), type must be defined expressively otherwise it reverts to a string type

1
.vscode/settings.json

@ -41,5 +41,6 @@
"terminal.background": "#1f1f1f" "terminal.background": "#1f1f1f"
}, },
"workbench.colorTheme": "Default Dark Modern", "workbench.colorTheme": "Default Dark Modern",
"inlineFold.unfoldOnLineSelect": true,
// --------------------------------------------------------- // ---------------------------------------------------------
} }

7
app/controllers/hot_controller.rb

@ -0,0 +1,7 @@
class HotController < ApplicationController
def index
logger.info('callee')
logger.info(__callee__)
logger.info(__method__)
end
end

17
app/controllers/strong_controller.rb

@ -0,0 +1,17 @@
class StrongController < ApplicationController
def index
logger.info(Hot::Constants)
end
def update
logger.info 'parameters', strong_params
redirect_to strong_path, notice: 'form is ok'
end
private
def strong_params
# params.require(:strong).permit [:name, :surname, { birth: {} }] # implicit: any fields inside bith
params.require(:strong).permit [:name, :surname, { birth: %i[date location] }] # explicit
end
end

4
app/views/hot/index.html.erb

@ -0,0 +1,4 @@
<div>
<h1 class="font-bold text-4xl">Hot#index</h1>
<%= Hot::Live %>
</div>

2
app/views/layouts/_navbar.html.erb

@ -5,6 +5,8 @@
<% end %> <% end %>
<div> <div>
<%= link_to 'Strong', strong_path, class: :menu %>
<%= link_to 'Hot', hot_path, class: :menu %>
<%= link_to 'Flash', flash_path, class: :menu %> <%= link_to 'Flash', flash_path, class: :menu %>
</div> </div>
</nav> </nav>

2
app/views/layouts/application.html.erb

@ -14,7 +14,7 @@
<%= tag :meta, name: :viewport, content: 'width=device-width,initial-scale=1' %> <%= tag :meta, name: :viewport, content: 'width=device-width,initial-scale=1' %>
<% if Rails.env.development? %> <% if Rails.env.development? %>
<%= tag :meta, name: 'turbo-prefetch', content: false %> <%= tag :meta, name: 'turbo-prefetch', content: false %>
<%= tag :meta, name: 'stimulus-debug', content: HotConstants.stimulus_debug %>
<%= tag :meta, name: 'stimulus-debug', content: Hot::Constants.stimulus_debug %>
<% end%> <% end%>
<!-- LINK--> <!-- LINK-->

34
app/views/strong/index.html.erb

@ -0,0 +1,34 @@
<div>
<h1 class="font-bold text-4xl">Form with strong parameters</h1>
<%= form_with url: update_strong_path, class:"bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4" do |form| %>
<div class="mb-4">
<%= form.label :name, "Name:", class:"block text-gray-700 text-sm font-bold mb-2" %>
</div>
<div class="mb-6">
<%= form.text_field 'strong[name]', class:"shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" %>
</div>
<div class="mb-4">
<%= form.label :surname, "Surname:", class:"block text-gray-700 text-sm font-bold mb-2" %>
</div>
<div class="mb-6">
<%= form.text_field 'strong[surname]', class:"shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" %>
</div>
<div class="flex flex-wrap -mx-3 mb-6">
<div class="w-full md:w-1/2 px-3 mb-6 md:mb-0">
<%= form.label :birth_date, "Birth Date:", class:"block text-gray-700 text-sm font-bold mb-2" %>
<%= form.text_field 'strong[birth][date]', class:"shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" %>
</div>
<div class="w-full md:w-1/2 px-3">
<%= form.label :birth_location, "Birth Location:", class:"block text-gray-700 text-sm font-bold mb-2" %>
<%= form.text_field 'strong[birth][location]', class:"shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" %>
</div>
</div>
<div class="flex items-center justify-between">
<%= form.submit class:"bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" %>
</div>
<% end %>
</div>

4
config/application.rb

@ -15,7 +15,9 @@ module EasyGoingRails
# Please, add to the `ignore` list any other `lib` subdirectories that do # Please, add to the `ignore` list any other `lib` subdirectories that do
# not contain `.rb` files, or that should not be reloaded or eager loaded. # not contain `.rb` files, or that should not be reloaded or eager loaded.
# Common ones are `templates`, `generators`, or `middleware`, for example. # Common ones are `templates`, `generators`, or `middleware`, for example.
config.autoload_lib(ignore: %w[assets tasks formatters hot_constants monkey_patches])
# config.autoload_lib(ignore: %w[assets tasks formatters hot_constants monkey_patches])
# config.autoload_lib(ignore: %w[assets tasks formatters monkey_patches])
config.autoload_lib(ignore: %w[assets tasks])
# main application title defined from current module name, see module above # main application title defined from current module name, see module above
config.application_title = module_parent.to_s.titleize config.application_title = module_parent.to_s.titleize

8
config/initializers/hot_changes.rb

@ -1,6 +1,6 @@
return unless defined?(Rails::Server) return unless defined?(Rails::Server)
require Rails.root.join('lib', 'hot_constants', 'hot_constants')
HotConstants.on_change(:log_active_record) { |bool| ActiveRecord::Base.logger.level = bool ? :debug : :fatal }
HotConstants.on_change(:log_action_view) { |bool| ActionView::Base.logger.level = bool ? :debug : :fatal }
Rails.application.config.after_initialize do
Hot::Constants.on_change(:log_active_record) { |bool| ActiveRecord::Base.logger.level = bool ? :debug : :fatal }
Hot::Constants.on_change(:log_action_view) { |bool| ActionView::Base.logger.level = bool ? :debug : :fatal }
end

2
config/initializers/rails_live_reload.rb

@ -5,7 +5,7 @@ if defined?(RailsLiveReload) && Rails.env.development?
# HOT Constants # HOT Constants
config.watch(%r{/\.env$}, reload: :always) config.watch(%r{/\.env$}, reload: :always)
config.watch(%r{/\.env\.sample$}) config.watch(%r{/\.env\.sample$})
config.watch(%r{/config/initializers/hot_changes.rb$})
# config.watch(%r{/config/initializers/hot_changes.rb$})
# USEFUL for tailwind changes!!!! # USEFUL for tailwind changes!!!!
config.watch %r{app/assets/builds/tailwind.css}, reload: :always config.watch %r{app/assets/builds/tailwind.css}, reload: :always

3
config/initializers/strong_parameters.rb

@ -0,0 +1,3 @@
Rails.application.configure do
config.action_controller.action_on_unpermitted_parameters = :raise
end

6
config/routes.rb

@ -1,4 +1,6 @@
Rails.application.routes.draw do Rails.application.routes.draw do
get 'strong/index'
get 'hot/index'
resources :scores resources :scores
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
@ -15,4 +17,8 @@ Rails.application.routes.draw do
get 'flash/redirect_with_both' => 'flash#redirect_with_both', as: :flash_redirect_with_both get 'flash/redirect_with_both' => 'flash#redirect_with_both', as: :flash_redirect_with_both
get 'flash/from_render' => 'flash#from_render', as: :flash_from_render get 'flash/from_render' => 'flash#from_render', as: :flash_from_render
post 'flash/from_turbo' => 'flash#from_turbo', as: :flash_from_turbo post 'flash/from_turbo' => 'flash#from_turbo', as: :flash_from_turbo
get 'hot' => 'hot#index', as: :hot
get 'strong' => 'strong#index', as: :strong
post 'strong' => 'strong#update', as: :update_strong
end end

11
lib/hot_constants/hot_constants.rb → lib/hot/constants.rb

@ -1,22 +1,23 @@
require 'dotenv' require 'dotenv'
# Hot Live Constants # Hot Live Constants
module HotConstants
module Hot::Constants
HOTENV = {} # rubocop:disable Style/MutableConstant HOTENV = {} # rubocop:disable Style/MutableConstant
LISTENERS = {} # rubocop:disable Style/MutableConstant LISTENERS = {} # rubocop:disable Style/MutableConstant
LOGGER = SemanticLogger[self] LOGGER = SemanticLogger[self]
TOTO = 'titi'
class << self class << self
def initialize def initialize
@old_definitions = {} @old_definitions = {}
@hot_definitions = {} @hot_definitions = {}
set_definitions
load_definitions
load_values load_values
end end
def set_definitions
LOGGER.info('--set_definitions')
def load_definitions
LOGGER.debug('--load_definitions3')
new_definitions = Dotenv.parse('.env.sample') new_definitions = Dotenv.parse('.env.sample')
@ -40,7 +41,7 @@ module HotConstants
end end
def load_values def load_values
LOGGER.info('--load_values')
LOGGER.debug('--load_values')
new_env = Dotenv.parse new_env = Dotenv.parse
constants_to_delete = HOTENV.except(*new_env.keys) constants_to_delete = HOTENV.except(*new_env.keys)

8
lib/hot/live.rb

@ -0,0 +1,8 @@
module Hot
# Hot Live constants
class Live
def initialize
puts 'initialized'
end
end
end

2
lib/monkey_patches/rails_live_reload/watcher.rb

@ -15,7 +15,7 @@ module RailsLiveReload
private private
def before_reload(files) def before_reload(files)
perform_when_change(files, ENV_SAMPLE_FILE) { HotConstants.set_definitions }
perform_when_change(files, ENV_SAMPLE_FILE) { HotConstants.load_definitions }
perform_when_change(files, ENV_FILE) { HotConstants.load_values } perform_when_change(files, ENV_FILE) { HotConstants.load_values }
perform_when_change(files, INITIALIZER) { load Rails.root.join('config', 'initializers', 'hot_changes.rb') } perform_when_change(files, INITIALIZER) { load Rails.root.join('config', 'initializers', 'hot_changes.rb') }
end end

Loading…
Cancel
Save