diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index fbe9036d..8c47c49f 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -14,12 +14,16 @@ class PostsController < ApplicationController @posts = @site.posts(lang: lang) @posts.sort_by!(:order, :date).reverse! @usuarie = @site.usuarie? current_usuarie + + fresh_when @posts end def show @site = find_site @post = @site.posts(lang: lang).find params[:id] authorize @post + + fresh_when @post end def new diff --git a/app/controllers/sites_controller.rb b/app/controllers/sites_controller.rb index 031cec6d..44c35a74 100644 --- a/app/controllers/sites_controller.rb +++ b/app/controllers/sites_controller.rb @@ -9,6 +9,8 @@ class SitesController < ApplicationController def index authorize Site @sites = current_usuarie.sites.order(:title) + + fresh_when @sites end # No tenemos propiedades de un sitio aún, así que vamos al listado de diff --git a/app/models/post.rb b/app/models/post.rb index 0f73399e..36b36e27 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -70,6 +70,11 @@ class Post < OpenStruct document.read! if File.exist? document.path end + # Devuelve una llave para poder guardar el post en una cache + def cache_key + 'posts/' + uuid.value + end + # TODO: Convertir a UUID? def id path.basename diff --git a/app/views/posts/index.haml b/app/views/posts/index.haml index 534805a8..81e60887 100644 --- a/app/views/posts/index.haml +++ b/app/views/posts/index.haml @@ -7,15 +7,16 @@ %main.row %aside.menu.col-md-3 - %h1 - = link_to @site.title, @site.url - %p.lead= @site.description + - cache @site do + %h1 + = link_to @site.title, @site.url + %p.lead= @site.description - %h3= t('posts.new') - %ul - - @site.layouts.to_h.keys.each do |layout| - %li= link_to @site.i18n.dig('layouts', layout.to_s) || layout.to_s.humanize, - new_site_post_path(@site, layout: layout) + %h3= t('posts.new') + %ul + - @site.layouts.to_h.keys.each do |layout| + %li= link_to @site.i18n.dig('layouts', layout.to_s) || layout.to_s.humanize, + new_site_post_path(@site, layout: layout) - if policy(@site).edit? = link_to t('sites.edit.btn', site: @site.title), @@ -43,43 +44,47 @@ - if @layout - next unless post.layout.name == @layout - next unless @usuarie || policy(post).show? - %tr - %td - .handle - = image_tag 'arrows-alt-v.svg' - -# Orden más alto es mayor prioridad - = hidden_field 'post[reorder]', post.uuid.value, - value: @posts.length - i, class: 'reorder' - %td - %small - = link_to @site.i18n.dig('layouts', post.layout.name.to_s) || post.layout.name.to_s.humanize, - site_posts_path(@site, layout: post.layout.name) - %br/ - = link_to post.title.value, - site_post_path(@site, post.id) - - if post.attributes.include? :draft - - if post.draft.value - %span.badge.badge-primary - = post_label_t(:draft, post: post) - - if post.attributes.include? :categories - - unless post.categories.value.empty? - %br - %small - - post.categories.value.each do |c| - = link_to c, site_posts_path(@site, category: c) + -# + TODO: Solo les usuaries cachean porque tenemos que separar + les botones por permisos. + - cache_if @site.usuarie?(current_usuarie), post do + %tr + %td + .handle + = image_tag 'arrows-alt-v.svg' + -# Orden más alto es mayor prioridad + = hidden_field 'post[reorder]', post.uuid.value, + value: @posts.length - i, class: 'reorder' + %td + %small + = link_to @site.i18n.dig('layouts', post.layout.name.to_s) || post.layout.name.to_s.humanize, + site_posts_path(@site, layout: post.layout.name) + %br/ + = link_to post.title.value, + site_post_path(@site, post.id) + - if post.attributes.include? :draft + - if post.draft.value + %span.badge.badge-primary + = post_label_t(:draft, post: post) + - if post.attributes.include? :categories + - unless post.categories.value.empty? + %br + %small + - post.categories.value.each do |c| + = link_to c, site_posts_path(@site, category: c) - %td - = post.date.value.strftime('%F') - %br/ - = post.try(:order).try(:value) - %td - - if @usuarie || policy(post).edit? - = link_to t('posts.edit'), - edit_site_post_path(@site, post.id), - class: 'btn' - - if @usuarie || policy(post).destroy? - = link_to t('posts.destroy'), - site_post_path(@site, post.id), - class: 'btn', - method: :delete, - data: { confirm: t('posts.confirm_destroy') } + %td + = post.date.value.strftime('%F') + %br/ + = post.try(:order).try(:value) + %td + - if @usuarie || policy(post).edit? + = link_to t('posts.edit'), + edit_site_post_path(@site, post.id), + class: 'btn' + - if @usuarie || policy(post).destroy? + = link_to t('posts.destroy'), + site_post_path(@site, post.id), + class: 'btn', + method: :delete, + data: { confirm: t('posts.confirm_destroy') } diff --git a/app/views/sites/index.haml b/app/views/sites/index.haml index 759e9e38..21cf7161 100644 --- a/app/views/sites/index.haml +++ b/app/views/sites/index.haml @@ -15,46 +15,50 @@ %table.table.table-condensed %tbody - @sites.each do |site| - %tr - %td - %h2 - - if policy(site).show? - = link_to site.title, site_path(site) + -# + TODO: Solo les usuaries cachean porque tenemos que separar + les botones por permisos. + - cache_if site.usuarie?(current_usuarie), site do + %tr + %td + %h2 + - if policy(site).show? + = link_to site.title, site_path(site) + - else + = site.title + %p.lead= site.description + %br + = link_to t('.visit'), site.url, class: 'btn' + - if current_usuarie.rol_for_site(site).temporal + = button_to t('sites.invitations.accept'), + site_usuaries_accept_invitation_path(site), + data: { toggle: 'tooltip' }, + title: t('help.sites.invitations.accept'), + method: :patch, + class: 'btn' + = button_to t('sites.invitations.reject'), + site_usuaries_reject_invitation_path(site), + data: { toggle: 'tooltip' }, + title: t('help.sites.invitations.reject'), + method: :patch, + class: 'btn' - else - = site.title - %p.lead= site.description - %br - = link_to t('.visit'), site.url, class: 'btn' - - if current_usuarie.rol_for_site(site).temporal - = button_to t('sites.invitations.accept'), - site_usuaries_accept_invitation_path(site), - data: { toggle: 'tooltip' }, - title: t('help.sites.invitations.accept'), - method: :patch, - class: 'btn' - = button_to t('sites.invitations.reject'), - site_usuaries_reject_invitation_path(site), - data: { toggle: 'tooltip' }, - title: t('help.sites.invitations.reject'), - method: :patch, - class: 'btn' - - else - - if policy(site).show? - = render 'layouts/btn_with_tooltip', - tooltip: t('help.sites.edit_posts'), - type: 'success', - link: site_path(site), - text: t('sites.posts') - - if policy(SiteUsuarie.new(site, current_usuarie)).index? - = render 'layouts/btn_with_tooltip', - tooltip: t('usuaries.index.help.self'), - text: t('usuaries.index.title'), - type: 'info', - link: site_usuaries_path(site) - - if policy(site).pull? && site.repository.needs_pull? - = render 'layouts/btn_with_tooltip', - tooltip: t('help.sites.pull'), - text: t('.pull'), - type: 'info', - link: site_pull_path(site) - = render 'sites/build', site: site + - if policy(site).show? + = render 'layouts/btn_with_tooltip', + tooltip: t('help.sites.edit_posts'), + type: 'success', + link: site_path(site), + text: t('sites.posts') + - if policy(SiteUsuarie.new(site, current_usuarie)).index? + = render 'layouts/btn_with_tooltip', + tooltip: t('usuaries.index.help.self'), + text: t('usuaries.index.title'), + type: 'info', + link: site_usuaries_path(site) + - if policy(site).pull? && site.repository.needs_pull? + = render 'layouts/btn_with_tooltip', + tooltip: t('help.sites.pull'), + text: t('.pull'), + type: 'info', + link: site_pull_path(site) + = render 'sites/build', site: site diff --git a/config/environments/development.rb b/config/environments/development.rb index ac21523e..a292fd67 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -21,7 +21,7 @@ Rails.application.configure do if Rails.root.join('tmp/caching-dev.txt').exist? config.action_controller.perform_caching = true - config.cache_store = :redis_cache_store + config.cache_store = :redis_cache_store, { url: ENV['REDIS_SERVER'] } config.public_file_server.headers = { 'Cache-Control' => "public, max-age=#{2.days.to_i}" } diff --git a/config/environments/production.rb b/config/environments/production.rb index 04239954..c21ba862 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -29,13 +29,10 @@ Rails.application.configure do # Compress CSS using a preprocessor. config.assets.css_compressor = :sass - - # rubocop:disable Lint/HandleExceptions begin config.assets.js_compressor = Uglifier.new(harmony: true) rescue NameError end - # rubocop:enable Lint/HandleExceptions # Do not fallback to assets pipeline if a precompiled asset is missed. config.assets.compile = false @@ -63,7 +60,7 @@ Rails.application.configure do config.log_tags = [:request_id] # Use a different cache store in production. - config.cache_store = :redis_cache_store + config.cache_store = :redis_cache_store, { url: ENV['REDIS_SERVER'] } # Use a real queuing backend for Active Job (and separate queues per # environment) @@ -142,7 +139,7 @@ Rails.application.configure do config.middleware.use ExceptionNotification::Rack, email: { - email_prefix: '[ERROR]', + email_prefix: '[ERROR] ', sender_address: ENV['DEFAULT_FROM'], exception_recipients: ENV['EXCEPTION_TO'] }