Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Apr 17, 2024
1 parent 3ed1086 commit 60d7f6e
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% @contact_group.each do |setting| %>
<% if setting.format == 'array' and setting.code == 'code_of_technical_contact' %>
<%= component 'settings/tech_contact_field', form: form, setting: setting %>
<% else %>
<fieldset class="space-y-5">
<%= component 'form/checkbox_with_description', form: form, attribute: "[settings][#{setting.id}", label_name: setting.code, description: setting.description, value: setting.value == 'true' %>
</fieldset>
<% end %>
<% end %>
14 changes: 14 additions & 0 deletions app/components/admin/settings/contact_group/component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Admin
module Settings
module ContactGroup
class Component < ApplicationViewComponent
attr_reader :contact_group, :form

def initialize(contact_group:, form:)
@contact_group = contact_group
@form = form
end
end
end
end
end
15 changes: 15 additions & 0 deletions app/components/admin/settings/invoice_group/component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<% @invoice_group.each do |setting| %>
<div class='flex items-center space-y-2'>

<div class='w-52'>
<%= form.label setting.code, class: 'block text-sm font-bold text-gray-700' %>
</div>
<div class="relative mt-1 rounded-md shadow-md border border-gray-300 w-96">
<div class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
<%= heroicon 'cog', variant: :solid, options: { class: 'h-5 w-5 text-gray-400' } %>
</div>
<%= form.text_field "[settings][#{setting.id}", placeholder: t(".#{setting.code}"), value: setting.value, class: 'block w-full rounded-md border-gray-300 pl-10 focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm py-2' %>
</div>
<%= form.hidden_field :id, value: "[settings][#{setting.id}" %>
</div>
<% end %>
14 changes: 14 additions & 0 deletions app/components/admin/settings/invoice_group/component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Admin
module Settings
module InvoiceGroup
class Component < ApplicationViewComponent
attr_reader :invoice_group, :form

def initialize(invoice_group:, form:)
@invoice_group = invoice_group
@form = form
end
end
end
end
end
32 changes: 32 additions & 0 deletions app/components/admin/settings/registrar_group/component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<% @registrar_group.each do |setting| %>

<div class='flex items-center space-y-2'>
<div class='w-52'>
<%= form.label setting.code, class: 'block text-sm font-bold text-gray-700' %>
</div>
<% if setting.format == 'array' and setting.code == 'default_nameserver_records' %>
<%= component 'settings/array_field', form: form, setting: setting %>
<% elsif setting.format == 'hash' %>
<%= component 'settings/hash_field', form: form, setting: setting %>
<% elsif setting.format == 'boolean' %>

<fieldset class="space-y-5">
<%= component 'form/checkbox_with_description', form: form, attribute: "[settings][#{setting.id}", label_name: setting.code, description: setting.description, value: setting.value == 'true' %>
</fieldset>

<% else %>

<div class="relative mt-1 rounded-md shadow-md border border-gray-300 w-96">
<div class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
<%= heroicon 'cog', variant: :solid, options: { class: 'h-5 w-5 text-gray-400' } %>
</div>
<%= form.text_field "[settings][#{setting.id}", placeholder: t(".#{setting.code}"), value: setting.value, class: 'block w-full rounded-md border-gray-300 pl-10 focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm py-2' %>
</div>

<% end %>
<%= form.hidden_field :id, value: "[settings][#{setting.id}" %>
</div>
<% end %>
14 changes: 14 additions & 0 deletions app/components/admin/settings/registrar_group/component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Admin
module Settings
module RegistrarGroup
class Component < ApplicationViewComponent
attr_reader :registrar_group, :form

def initialize(registrar_group:, form:)
@registrar_group = registrar_group
@form = form
end
end
end
end
end
54 changes: 1 addition & 53 deletions app/controllers/admin/settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,7 @@ def show
end

def update
updated = true
errors = []

casted_settings.each do |id, setting_params|
setting = Setting.find_by(id: id)

if setting.update!(setting_params)
next
else
updated = false
errors += setting.errors.full_messages
end
end
updated, errors = Admin::SettingUpdaterService.new(params).update

if updated
redirect_to root_path, status: :see_other, notice: t('.success')
Expand All @@ -30,45 +18,5 @@ def update
render :show, status: :unprocessable_entity
end
end

private

def casted_settings
settings_values = {}

params.require(:settings).permit!

params[:settings].each do |k, v|
setting = Setting.find_by(id: k)

next unless setting

formatted_value = format_setting_value(v, setting.format)
settings_values[setting.id] = { value: formatted_value }
end

settings_values
end

def format_setting_value(value, format_type)
case format_type
when 'boolean'
(value.to_s == 'true').to_s
when 'integer'
value.to_i
when 'float'
value.to_f
when 'hash'
value.is_a?(ActionController::Parameters) ? value.to_unsafe_h.to_json : value
when 'array'
if value.is_a?(Array) && value.first.is_a?(ActionController::Parameters)
value.map(&:to_unsafe_h).to_json
else
value.to_json
end
else
value
end
end
end
end
67 changes: 67 additions & 0 deletions app/services/admin/setting_updater_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module Admin
class SettingUpdaterService
attr_reader :params

def initialize(params)
@params = params
end

def update
updated = true
errors = []

casted_settings.each do |id, setting_params|
setting = Setting.find_by(id: id)

if setting.update!(setting_params)
next
else
updated = false
errors += setting.errors.full_messages
end
end

return updated, errors
end

private

def casted_settings
settings_values = {}

params.require(:settings).permit!

params[:settings].each do |k, v|
setting = Setting.find_by(id: k)

next unless setting

formatted_value = format_setting_value(v, setting.format)
settings_values[setting.id] = { value: formatted_value }
end

settings_values
end

def format_setting_value(value, format_type)
case format_type
when 'boolean'
(value.to_s == 'true').to_s
when 'integer'
value.to_i
when 'float'
value.to_f
when 'hash'
value.is_a?(ActionController::Parameters) ? value.to_unsafe_h.to_json : value
when 'array'
if value.is_a?(Array) && value.first.is_a?(ActionController::Parameters)
value.map(&:to_unsafe_h).to_json
else
value.to_json
end
else
value
end
end
end
end
67 changes: 6 additions & 61 deletions app/views/admin/settings/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,13 @@
<div class="-mx-4 mt-8 overflow-hidden shadow ring-1 ring-black ring-opacity-5 sm:-mx-6 md:mx-0 md:rounded-lg p-8">
<%= form_with url: admin_settings_path, method: :patch, class: 'w-full mt-8 space-y-4' do |form| %>
<% @invoice_group.each do |setting| %>
<div class='flex items-center space-y-2'>
<%= component 'admin/settings/invoice_group', form: form, invoice_group: @invoice_group %>
<%= component 'admin/settings/registrar_group', form: form, registrar_group: @registrar_group %>
<%= component 'admin/settings/contact_group', form: form, contact_group: @contact_group %>

<div class='w-52'>
<%= form.label setting.code, class: 'block text-sm font-bold text-gray-700' %>
</div>
<div class="relative mt-1 rounded-md shadow-md border border-gray-300 w-96">
<div class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
<%= heroicon 'cog', variant: :solid, options: { class: 'h-5 w-5 text-gray-400' } %>
</div>
<%= form.text_field "[settings][#{setting.id}", placeholder: t(".#{setting.code}"), value: setting.value, class: 'block w-full rounded-md border-gray-300 pl-10 focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm py-2' %>
</div>
<%= form.hidden_field :id, value: "[settings][#{setting.id}" %>
</div>
<div class='w-full flex justify-end'>
<%= form.submit t('.submit'), class: "text-gray-600 inline-flex w-72 justify-center rounded-md border border-[#DDA77B] bg-white py-2 px-4 text-sm font-medium text-[#DDA77B] shadow-sm hover:bg-[#DDA77B] hover:text-white mt-2 cursor-pointer" %>
</div>
<% end %>
<% @registrar_group.each do |setting| %>

<div class='flex items-center space-y-2'>
<div class='w-52'>
<%= form.label setting.code, class: 'block text-sm font-bold text-gray-700' %>
</div>
<% if setting.format == 'array' and setting.code == 'default_nameserver_records' %>
<%= component 'settings/array_field', form: form, setting: setting %>
<% elsif setting.format == 'hash' %>
<%= component 'settings/hash_field', form: form, setting: setting %>
<% elsif setting.format == 'boolean' %>

<fieldset class="space-y-5">
<%= component 'form/checkbox_with_description', form: form, attribute: "[settings][#{setting.id}", label_name: setting.code, description: setting.description, value: setting.value == 'true' %>
</fieldset>

<% else %>

<div class="relative mt-1 rounded-md shadow-md border border-gray-300 w-96">
<div class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
<%= heroicon 'cog', variant: :solid, options: { class: 'h-5 w-5 text-gray-400' } %>
</div>
<%= form.text_field "[settings][#{setting.id}", placeholder: t(".#{setting.code}"), value: setting.value, class: 'block w-full rounded-md border-gray-300 pl-10 focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm py-2' %>
</div>

<% end %>
<%= form.hidden_field :id, value: "[settings][#{setting.id}" %>
</div>
<% end %>
<% @contact_group.each do |setting| %>
<% if setting.format == 'array' and setting.code == 'code_of_technical_contact' %>
<%= component 'settings/tech_contact_field', form: form, setting: setting %>
<% else %>
<fieldset class="space-y-5">
<%= component 'form/checkbox_with_description', form: form, attribute: "[settings][#{setting.id}", label_name: setting.code, description: setting.description, value: setting.value == 'true' %>
</fieldset>
<% end %>
<% end %>

<div class='w-full flex justify-end'>
<%= form.submit t('.submit'), class: "text-gray-600 inline-flex w-72 justify-center rounded-md border border-[#DDA77B] bg-white py-2 px-4 text-sm font-medium text-[#DDA77B] shadow-sm hover:bg-[#DDA77B] hover:text-white mt-2 cursor-pointer" %>
</div>
<% end %>
</div>
</div>
5 changes: 4 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet">

<script src="https://cdn.tailwindcss.com"></script>
<% if Rails.env.development? %>
<script src="https://cdn.tailwindcss.com"></script>
<% end %>

</head>

<body style="font-family: 'Roboto', sans-serif;">
Expand Down
2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
.first_or_create.update(code: 'invoice_number_max', value: '199999', format: 'integer', group: 'invoice', description: 'Maximum invoice number')

Setting.where(code: 'vat')
.first_or_create.update(code: 'vat', value: '20.0', format: 'float', group: 'invoice', description: 'VAT rate')
.first_or_create.update(code: 'vat', value: '22.0', format: 'float', group: 'invoice', description: 'VAT rate')

# BILLING GROUP:

Expand Down

0 comments on commit 60d7f6e

Please sign in to comment.