5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-26 07:16:21 +00:00

feat: pluck_param nos ayuda a encontrar un param semanticamente

This commit is contained in:
f 2024-07-10 17:04:59 -03:00
parent f1968a0b49
commit 5f53abe78a
No known key found for this signature in database
9 changed files with 61 additions and 38 deletions

View file

@ -5,9 +5,10 @@
# No necesitamos autenticación aun # No necesitamos autenticación aun
class CollaborationsController < ApplicationController class CollaborationsController < ApplicationController
include Pundit include Pundit
include StrongParamsHelper
def collaborate def collaborate
@site = Site.find_by_name(params[:site_id]) @site = Site.find_by_name(pluck_param(:site_id))
authorize Collaboration.new(@site) authorize Collaboration.new(@site)
@invitade = current_usuarie || @site.usuaries.build @invitade = current_usuarie || @site.usuaries.build
@ -21,7 +22,7 @@ class CollaborationsController < ApplicationController
# #
# * Si le usuarie existe y no está logueade, pedirle la contraseña # * Si le usuarie existe y no está logueade, pedirle la contraseña
def accept_collaboration def accept_collaboration
@site = Site.find_by_name(params[:site_id]) @site = Site.find_by_name(pluck_param(:site_id))
authorize Collaboration.new(@site) authorize Collaboration.new(@site)
@invitade = current_usuarie @invitade = current_usuarie

View file

@ -2,6 +2,8 @@
# Controlador para artículos # Controlador para artículos
class PostsController < ApplicationController class PostsController < ApplicationController
include StrongParamsHelper
before_action :authenticate_usuarie! before_action :authenticate_usuarie!
before_action :service_for_direct_upload, only: %i[new edit] before_action :service_for_direct_upload, only: %i[new edit]
@ -17,9 +19,9 @@ class PostsController < ApplicationController
# @todo Mover a tu propio scope # @todo Mover a tu propio scope
def new_array def new_array
@value = params.require(:value).strip @value = pluck_param(:value)
@name = params.require(:name).strip @name = pluck_param(:name)
id = params.require(:id).strip id = pluck_param(:id)
headers['HX-Trigger-After-Swap'] = 'htmx:resetForm' headers['HX-Trigger-After-Swap'] = 'htmx:resetForm'
@ -27,13 +29,13 @@ class PostsController < ApplicationController
end end
def new_array_value def new_array_value
@value = params.require(:value).strip @value = pluck_param(:value)
render layout: false render layout: false
end end
def new_related_post def new_related_post
@uuid = params.require(:value).strip @uuid = pluck_param(:value)
@indexed_post = site.indexed_posts.find_by!(post_id: @uuid) @indexed_post = site.indexed_posts.find_by!(post_id: @uuid)
@ -41,7 +43,7 @@ class PostsController < ApplicationController
end end
def new_has_one def new_has_one
@uuid = params.require(:value).strip @uuid = pluck_param(:value)
@indexed_post = site.indexed_posts.find_by!(post_id: @uuid) @indexed_post = site.indexed_posts.find_by!(post_id: @uuid)
@ -51,7 +53,7 @@ class PostsController < ApplicationController
# El formulario de un Post, si pasamos el UUID, estamos editando, sino # El formulario de un Post, si pasamos el UUID, estamos editando, sino
# estamos creando. # estamos creando.
def form def form
uuid = params.permit(:uuid).try(:[], :uuid).presence uuid = pluck_param(:uuid, optional: true)
locale locale
@post = @post =
@ -59,7 +61,7 @@ class PostsController < ApplicationController
site.indexed_posts.find_by!(post_id: uuid).post site.indexed_posts.find_by!(post_id: uuid).post
else else
# @todo Usar la base de datos # @todo Usar la base de datos
site.posts(lang: locale).build(layout: params.require(:layout)) site.posts(lang: locale).build(layout: pluck_param(:layout))
end end
swap_modals swap_modals
@ -107,7 +109,7 @@ class PostsController < ApplicationController
def new def new
authorize Post authorize Post
@post = site.posts(lang: locale).build(layout: params[:layout]) @post = site.posts(lang: locale).build(layout: pluck_param(:layout))
breadcrumb I18n.t('loaf.breadcrumbs.posts.new', layout: @post.layout.humanized_name.downcase), '' breadcrumb I18n.t('loaf.breadcrumbs.posts.new', layout: @post.layout.humanized_name.downcase), ''
end end
@ -128,17 +130,17 @@ class PostsController < ApplicationController
# condiciones. # condiciones.
if htmx? if htmx?
if post.persisted? if post.persisted?
triggers = { 'notification:show' => { 'id' => params.permit(:saved).values.first } } triggers = { 'notification:show' => { 'id' => pluck_param(:saved, optional: true) } }
swap_modals(triggers) swap_modals(triggers)
@value = post.title.value @value = post.title.value
@uuid = post.uuid.value @uuid = post.uuid.value
@name = params.require(:name) @name = pluck_param(:name)
render render_path_from_attribute, layout: false render render_path_from_attribute, layout: false
else else
headers['HX-Retarget'] = "##{params.require(:form)}" headers['HX-Retarget'] = "##{pluck_param(:form)}"
headers['HX-Reswap'] = 'outerHTML' headers['HX-Reswap'] = 'outerHTML'
render 'posts/form', layout: false, post: post, site: site, **params.permit(:form, :base, :dir, :locale) render 'posts/form', layout: false, post: post, site: site, **params.permit(:form, :base, :dir, :locale)
@ -171,13 +173,13 @@ class PostsController < ApplicationController
if htmx? if htmx?
if post.persisted? if post.persisted?
triggers = { 'notification:show' => params.permit(:saved).values.first } triggers = { 'notification:show' => pluck_param(:saved, optional: true) }
swap_modals(triggers) swap_modals(triggers)
@value = post.title.value @value = post.title.value
@uuid = post.uuid.value @uuid = post.uuid.value
@name = params.require(:name) @name = pluck_param(:name)
render render_path_from_attribute, layout: false render render_path_from_attribute, layout: false
else else
@ -272,7 +274,7 @@ class PostsController < ApplicationController
# @return [String] # @return [String]
def render_path_from_attribute def render_path_from_attribute
case params.require(:attribute) case pluck_param(:attribute)
when 'new_has_many' then 'posts/new_has_many_value' when 'new_has_many' then 'posts/new_has_many_value'
when 'new_belongs_to' then 'posts/new_belongs_to_value' when 'new_belongs_to' then 'posts/new_belongs_to_value'
when 'new_has_and_belongs_to_many' then 'posts/new_has_many_value' when 'new_has_and_belongs_to_many' then 'posts/new_has_many_value'

View file

@ -0,0 +1,20 @@
# frozen_string_literal: true
# Métodos reutilizables para trabajar con StrongParams
module StrongParamsHelper
# Obtiene el valor de un param
#
# @todo No hay una forma mejor de hacer esto?
# @param param [Symbol]
# @param :optional [Bool]
# @param :params [StrongParameters]
# @return [nil,String]
def pluck_param(param, optional: false, params: params)
if optional
params.permit(param).values.first.presence
else
params.require(param).presence
end
end
end

View file

@ -1,6 +1,6 @@
%hr/ %hr/
- locale = params.permit(:locale) - locale = pluck_param(:locale, optional: true)
- if controller_name != 'sessions' - if controller_name != 'sessions'
= link_to t('.sign_in'), new_session_path(resource_name, params: locale), = link_to t('.sign_in'), new_session_path(resource_name, params: locale),

View file

@ -18,22 +18,22 @@
:ruby :ruby
except = %i[date] except = %i[date]
if (inverse = params.permit(:inverse).try(:[], :inverse).presence) if (inverse = pluck_param(:inverse, optional: true))
except << inverse.to_sym except << inverse.to_sym
end end
options = { options = {
id: params.require(:form), id: pluck_param(:form),
multipart: true, multipart: true,
class: 'form post ', class: 'form post ',
'hx-swap': params.require(:swap), 'hx-swap': pluck_param(:swap),
'hx-target': "##{params.require(:target)}", 'hx-target': "##{pluck_param(:target)}",
'hx-validate': true, 'hx-validate': true,
data: { data: {
controller: 'form-validation', controller: 'form-validation',
action: 'form-validation#submit', action: 'form-validation#submit',
'form-validation-submitting-id-value': params.permit(:submitting).values.first, 'form-validation-submitting-id-value': pluck_param(:submitting, optional: true),
'form-validation-invalid-id-value': params.permit(:invalid).values.first, 'form-validation-invalid-id-value': pluck_param(:invalid, optional: true),
} }
} }
@ -68,20 +68,20 @@
= errors.first = errors.first
-# Parámetros para HTMX -# Parámetros para HTMX
%input{ type: 'hidden', name: 'hide', value: params.permit((post.errors.empty? ? :show : :hide)).try(:values).try(:first) } %input{ type: 'hidden', name: 'hide', value: pluck_param((post.errors.empty? ? :show : :hide), optional: true) }
%input{ type: 'hidden', name: 'show', value: params.permit((post.errors.empty? ? :hide : :show)).try(:values).try(:first) } %input{ type: 'hidden', name: 'show', value: pluck_param((post.errors.empty? ? :hide : :show), optional: true) }
%input{ type: 'hidden', name: 'name', value: params.require(:name) } %input{ type: 'hidden', name: 'name', value: pluck_param(:name) }
%input{ type: 'hidden', name: 'base', value: params.require(:base) } %input{ type: 'hidden', name: 'base', value: pluck_param(:base) }
%input{ type: 'hidden', name: 'form', value: options[:id] } %input{ type: 'hidden', name: 'form', value: options[:id] }
%input{ type: 'hidden', name: 'dir', value: dir } %input{ type: 'hidden', name: 'dir', value: dir }
%input{ type: 'hidden', name: 'locale', value: locale } %input{ type: 'hidden', name: 'locale', value: locale }
%input{ type: 'hidden', name: 'attribute', value: params.require(:attribute) } %input{ type: 'hidden', name: 'attribute', value: pluck_param(:attribute) }
%input{ type: 'hidden', name: 'target', value: params.require(:target) } %input{ type: 'hidden', name: 'target', value: pluck_param(:target) }
%input{ type: 'hidden', name: 'swap', value: params.require(:swap) } %input{ type: 'hidden', name: 'swap', value: pluck_param(:swap) }
- if params[:inverse].present? - if params[:inverse].present?
%input{ type: 'hidden', name: 'inverse', value: params.require(:inverse) } %input{ type: 'hidden', name: 'inverse', value: pluck_param(:inverse) }
- if params[:saved].present? - if params[:saved].present?
%input{ type: 'hidden', name: 'saved', value: params.require(:saved) } %input{ type: 'hidden', name: 'saved', value: pluck_param(:saved) }
= hidden_field_tag "#{base}[layout]", post.layout.name = hidden_field_tag "#{base}[layout]", post.layout.name
@ -92,6 +92,6 @@
Enviamos valores vacíos o arrastrados desde el formulario anterior Enviamos valores vacíos o arrastrados desde el formulario anterior
para los atributos ignorados para los atributos ignorados
- except.each do |attr| - except.each do |attr|
%input{ type: 'hidden', name: "#{base}[#{attr}]", value: params[attr].presence } %input{ type: 'hidden', name: "#{base}[#{attr}]", value: pluck_param(attr, optional: true) }
= yield(:post_form) = yield(:post_form)

View file

@ -4,4 +4,4 @@
@param :site [Site] @param :site [Site]
@param :post [Post] @param :post [Post]
= render 'posts/htmx_form', site: @site, post: @post, locale: @locale, dir: t("locales.#{@locale}.dir"), base: params.require(:base) = render 'posts/htmx_form', site: @site, post: @post, locale: @locale, dir: t("locales.#{@locale}.dir"), base: pluck_param(:base)

View file

@ -1 +1 @@
= render 'posts/new_has_one', post: @indexed_post, name: params.require(:name), value: @uuid = render 'posts/new_has_one', post: @indexed_post, name: pluck_param(:name), value: @uuid

View file

@ -1 +1 @@
= render 'posts/new_has_one', post: @post.to_index, name: params.require(:name), value: @uuid, modal_id: params.require(:show) = render 'posts/new_has_one', post: @post.to_index, name: pluck_param(:name), value: @uuid, modal_id: pluck_param(:show)

View file

@ -1 +1 @@
= render 'sites/build', site: @site, class: params.permit(:class)[:class] = render 'sites/build', site: @site, class: pluck_param(:class, optional: true)