From 32313e595c2139014e719a3cc773521135b65a83 Mon Sep 17 00:00:00 2001 From: f Date: Sat, 3 Oct 2020 21:32:32 -0300 Subject: [PATCH] usar ruby en lugar de rails :B --- app/controllers/api/v1/protected_controller.rb | 2 +- app/controllers/application_controller.rb | 4 ++-- app/controllers/posts_controller.rb | 4 ++-- app/models/deploy.rb | 2 +- app/models/metadata_related_posts.rb | 4 ++-- app/models/metadata_slug.rb | 2 +- app/models/metadata_template.rb | 2 +- app/models/post.rb | 5 ++--- app/models/post/template_field.rb | 2 +- app/models/post_relation.rb | 2 +- app/models/site.rb | 6 +++--- app/models/site/forms.rb | 2 +- app/services/post_service.rb | 4 ++-- app/services/site_service.rb | 4 ++-- app/views/active_storage/blobs/_blob.html.erb | 2 +- app/views/devise/mailer/email_changed.html.haml | 2 +- app/views/devise/mailer/email_changed.text.haml | 2 +- app/views/posts/_attribute_feedback.haml | 2 +- app/views/posts/attributes/_lang.haml | 2 +- app/views/posts/index.haml | 2 +- test/jobs/deploy_job_test.rb | 2 +- 21 files changed, 29 insertions(+), 30 deletions(-) diff --git a/app/controllers/api/v1/protected_controller.rb b/app/controllers/api/v1/protected_controller.rb index b4e4db52..4e49f3a8 100644 --- a/app/controllers/api/v1/protected_controller.rb +++ b/app/controllers/api/v1/protected_controller.rb @@ -50,7 +50,7 @@ module Api # puede ser que haya tardado más de media hora en enviar el # formulario. def cookie_is_valid? - return if (site_cookie.try(:[], 'expires') || 0) > Time.now.to_i + return if (site_cookie&.dig('expires') || 0) > Time.now.to_i @reason = 'expired_or_invalid_cookie' render plain: Rails.env.production? ? nil : @reason, status: :precondition_required diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6786491d..abf12296 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -13,7 +13,7 @@ class ApplicationController < ActionController::Base rescue_from ActionController::RoutingError, with: :page_not_found before_action do - Rack::MiniProfiler.authorize_request if current_usuarie.try(:ends_with?, '@' + ENV.fetch('SUTTY', 'sutty.nl')) + Rack::MiniProfiler.authorize_request if current_usuarie&.email&.ends_with?('@' + ENV.fetch('SUTTY', 'sutty.nl')) end # No tenemos índice de sutty, vamos directamente a ver el listado de @@ -45,7 +45,7 @@ class ApplicationController < ActionController::Base # corresponde con el idioma de los artículos, porque puede querer # traducirlos. def set_locale(&action) - I18n.with_locale(current_usuarie.try(:lang) || I18n.default_locale, &action) + I18n.with_locale(current_usuarie&.lang || I18n.default_locale, &action) end # Muestra una página 404 diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 395a85d1..ecf5af88 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -9,7 +9,7 @@ class PostsController < ApplicationController # Las URLs siempre llevan el idioma actual o el de le usuarie def default_url_options - { locale: params[:locale] || current_usuarie.try(:lang) || I18n.locale } + { locale: params[:locale] || current_usuarie&.lang || I18n.locale } end def index @@ -145,7 +145,7 @@ class PostsController < ApplicationController # solicite a le usuarie crear el nuevo idioma y que esto lo agregue al # _config.yml del sitio en lugar de mezclar idiomas. def locale - @site.try(:locales).try(:find, -> { I18n.locale.to_s }) do |l| + @site&.locales&.find(-> { I18n.locale.to_s }) do |l| l == params[:locale] end end diff --git a/app/models/deploy.rb b/app/models/deploy.rb index a5dd1537..98e6d0a1 100644 --- a/app/models/deploy.rb +++ b/app/models/deploy.rb @@ -65,7 +65,7 @@ class Deploy < ApplicationRecord stat.bytes = size stat.save - r.try :success? + r&.success? end # rubocop:enable Metrics/MethodLength end diff --git a/app/models/metadata_related_posts.rb b/app/models/metadata_related_posts.rb index 5b73857c..e38538a8 100644 --- a/app/models/metadata_related_posts.rb +++ b/app/models/metadata_related_posts.rb @@ -23,12 +23,12 @@ class MetadataRelatedPosts < MetadataArray end def title(post) - post.try(:title).try(:value) || post.try(:slug).try(:value) + post&.title&.value || post&.slug&.value end # TODO: Traer el idioma actual de otra forma def lang - post.try(:lang).try(:value) || I18n.locale + post&.lang&.value || I18n.locale end # Encuentra el filtro diff --git a/app/models/metadata_slug.rb b/app/models/metadata_slug.rb index 7798c7c5..1f12475c 100644 --- a/app/models/metadata_slug.rb +++ b/app/models/metadata_slug.rb @@ -40,6 +40,6 @@ class MetadataSlug < MetadataTemplate # Devuelve el título a menos que sea privado y no esté vacío def title - post.title.try(:value).try(:to_s) unless post.title.private? && !post.title.try(:value).try(:blank?) + post.title&.value&.to_s unless post.title.private? && !post.title&.value&.blank? end end diff --git a/app/models/metadata_template.rb b/app/models/metadata_template.rb index 81ee17f8..35dbf455 100644 --- a/app/models/metadata_template.rb +++ b/app/models/metadata_template.rb @@ -18,7 +18,7 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type, # Valores posibles, busca todos los valores actuales en otros # artículos del mismo sitio def values - site.everything_of(name, lang: post.try(:lang).try(:value)) + site.everything_of(name, lang: post&.lang&.value) end # Valor actual o por defecto. Al memoizarlo podemos modificarlo diff --git a/app/models/post.rb b/app/models/post.rb index affec148..215a8ef0 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -17,9 +17,8 @@ class Post < OpenStruct # # TODO: Reemplazar cuando leamos el contenido del Document # a demanda? - def find_layout(doc) - IO.foreach(doc.path).lazy.grep(/^layout: /).take(1).first - .try(:split, ' ').try(:last).try(:to_sym) + def find_layout(path) + IO.foreach(path).lazy.grep(/^layout: /).take(1).first&.split(' ')&.last&.to_sym end end diff --git a/app/models/post/template_field.rb b/app/models/post/template_field.rb index 8b4b6928..08da933b 100644 --- a/app/models/post/template_field.rb +++ b/app/models/post/template_field.rb @@ -305,7 +305,7 @@ class Post # Procesamos el valor, buscando : como separador de campos que # queremos encontrar y luego los unimos - _value = (values.try(:split, ':', 2) || []).map do |v| + _value = (values&.split(':', 2) || []).map do |v| # Tenemos hasta tres niveles de búsqueda collection, attr, subattr = v.split('/', 3) diff --git a/app/models/post_relation.rb b/app/models/post_relation.rb index 1e5c5280..acf9058a 100644 --- a/app/models/post_relation.rb +++ b/app/models/post_relation.rb @@ -135,7 +135,7 @@ class PostRelation < Array def build_layout(layout = nil) return layout if layout.is_a? Layout - site.layouts[layout.try(:to_sym) || :post] + site.layouts[layout&.to_sym || :post] end # Devuelve una colección Jekyll que hace pasar el documento diff --git a/app/models/site.rb b/app/models/site.rb index 9392eff5..4a6dfbfc 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -221,8 +221,8 @@ class Site < ApplicationRecord # No fallar si no existe colección para este idioma # XXX: queremos fallar silenciosamente? - (collections[lang.to_s].try(:docs) || []).each do |doc| - layout = layouts[Post.find_layout(doc)] + (collections[lang.to_s]&.docs || []).each do |doc| + layout = layouts[Post.find_layout(doc.path)] @posts[lang].build(document: doc, layout: layout, lang: lang) end @@ -288,7 +288,7 @@ class Site < ApplicationRecord attr = attr.to_sym posts(lang: lang).flat_map do |p| - p.send(attr).try(:value) if p.attribute? attr + p[attr].value if p.attribute? attr end.uniq.compact end diff --git a/app/models/site/forms.rb b/app/models/site/forms.rb index b65be162..8abd6cce 100644 --- a/app/models/site/forms.rb +++ b/app/models/site/forms.rb @@ -37,7 +37,7 @@ class Site # # @return Array Formularios disponibles para este sitio def forms - @forms ||= data.dig('forms').try(:keys) || [] + @forms ||= data.dig('forms')&.keys || [] end # El nombre del formulario está disponible diff --git a/app/services/post_service.rb b/app/services/post_service.rb index 5ee1f2fd..40562a85 100644 --- a/app/services/post_service.rb +++ b/app/services/post_service.rb @@ -58,7 +58,7 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do # # { uuid => 2, uuid => 1, uuid => 0 } def reorder - reorder = params.require(:post).permit(reorder: {}).try(:[], :reorder).transform_values(&:to_i) + reorder = params.require(:post).permit(reorder: {})&.dig(:reorder)&.transform_values(&:to_i) posts = site.posts(lang: locale).where(uuid: reorder.keys) files = posts.map do |post| @@ -86,7 +86,7 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do usuarie: usuarie, remove: action == :destroyed, message: I18n.t("post_service.#{action}", - title: post.try(:title).try(:value))) + title: post&.title&.value)) end # Solo permitir cambiar estos atributos de cada articulo diff --git a/app/services/site_service.rb b/app/services/site_service.rb index a9b5af4e..24e5bc08 100644 --- a/app/services/site_service.rb +++ b/app/services/site_service.rb @@ -11,7 +11,7 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do add_role temporal: false, rol: 'usuarie' - I18n.with_locale(usuarie.try(:lang) || I18n.default_locale) do + I18n.with_locale(usuarie&.lang || I18n.default_locale) do site.save && site.config.write && commit_config(action: :create) @@ -24,7 +24,7 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do # Actualiza el sitio y guarda los cambios en la configuración def update - I18n.with_locale(usuarie.try(:lang) || I18n.default_locale) do + I18n.with_locale(usuarie&.lang || I18n.default_locale) do site.update(params) && site.config.write && commit_config(action: :update) diff --git a/app/views/active_storage/blobs/_blob.html.erb b/app/views/active_storage/blobs/_blob.html.erb index 49ba357d..a515cf76 100644 --- a/app/views/active_storage/blobs/_blob.html.erb +++ b/app/views/active_storage/blobs/_blob.html.erb @@ -4,7 +4,7 @@ <% end %>
- <% if caption = blob.try(:caption) %> + <% if caption = blob&.caption %> <%= caption %> <% else %> <%= blob.filename %> diff --git a/app/views/devise/mailer/email_changed.html.haml b/app/views/devise/mailer/email_changed.html.haml index 8ae4d38a..6b505fa1 100644 --- a/app/views/devise/mailer/email_changed.html.haml +++ b/app/views/devise/mailer/email_changed.html.haml @@ -1,5 +1,5 @@ %p= t('.greeting', recipient: @email) -- if @resource.try(:unconfirmed_email?) +- if @resource&.unconfirmed_email? %p= t('.message', email: @resource.unconfirmed_email) - else %p= t('.message', email: @resource.email) diff --git a/app/views/devise/mailer/email_changed.text.haml b/app/views/devise/mailer/email_changed.text.haml index e5216a5a..4df926aa 100644 --- a/app/views/devise/mailer/email_changed.text.haml +++ b/app/views/devise/mailer/email_changed.text.haml @@ -1,6 +1,6 @@ = t('.greeting', recipient: @email) \ -- if @resource.try(:unconfirmed_email?) +- if @resource&.unconfirmed_email? = t('.message', email: @resource.unconfirmed_email) - else = t('.message', email: @resource.email) diff --git a/app/views/posts/_attribute_feedback.haml b/app/views/posts/_attribute_feedback.haml index d2345f65..9852c7b6 100644 --- a/app/views/posts/_attribute_feedback.haml +++ b/app/views/posts/_attribute_feedback.haml @@ -3,5 +3,5 @@ .invalid-feedback{ id: id_for_feedback(*attribute) } - if metadata.required = t('posts.attributes.required.feedback') - - metadata.errors.try :each do |error| + - metadata.errors&.each do |error| = error diff --git a/app/views/posts/attributes/_lang.haml b/app/views/posts/attributes/_lang.haml index a41fbb35..b7484804 100644 --- a/app/views/posts/attributes/_lang.haml +++ b/app/views/posts/attributes/_lang.haml @@ -1 +1 @@ -= hidden_field_tag 'post[lang]', post.try(:lang).try(:value) += hidden_field_tag 'post[lang]', post&.lang&.value diff --git a/app/views/posts/index.haml b/app/views/posts/index.haml index d9327e02..a11ff81a 100644 --- a/app/views/posts/index.haml +++ b/app/views/posts/index.haml @@ -86,7 +86,7 @@ %td = post.date.value.strftime('%F') %br/ - = post.try(:order).try(:value) + = post&.order&.value %td - if @usuarie || policy(post).edit? = link_to t('posts.edit'), diff --git a/test/jobs/deploy_job_test.rb b/test/jobs/deploy_job_test.rb index 6d1d321d..a0fabfc4 100644 --- a/test/jobs/deploy_job_test.rb +++ b/test/jobs/deploy_job_test.rb @@ -13,7 +13,7 @@ class DeployJobTest < ActiveSupport::TestCase assert_not ActionMailer::Base.deliveries.empty? site.deploys.each do |d| - assert File.exist?(d.try(:path) || d.try(:destination)) + assert File.exist?(d.respond_to?(:path) ? d.path : d.destination) end site.destroy