From 95e7c215df89608a5b10bebb87968c9a713bbf3d Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Tue, 17 Oct 2023 15:05:36 -0400 Subject: Added uploading images to blog posts --- .gitignore | 1 + Gemfile | 2 ++ Gemfile.lock | 13 +++++++++++++ app/assets/javascripts/admin/records.coffee | 2 ++ app/assets/stylesheets/admin/layout.scss | 25 +++++++++++++++++++------ app/controllers/admin/blogs_controller.rb | 2 +- app/models/blog.rb | 7 +++++-- app/views/admin/blogs/_form.html.haml | 8 ++++++++ config/deploy.rb | 2 +- 9 files changed, 52 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index ae472fd..7e20915 100644 --- a/.gitignore +++ b/.gitignore @@ -29,5 +29,6 @@ tags *.swo /public/uploads config/database.yml +/storage misc diff --git a/Gemfile b/Gemfile index 47c1ffa..3044a2f 100644 --- a/Gemfile +++ b/Gemfile @@ -80,3 +80,5 @@ gem 'will_paginate', '~> 4.0' gem 'redcarpet' gem 'rouge' gem 'akismet' +gem 'active_storage_validations' +gem "image_processing", ">= 1.2" diff --git a/Gemfile.lock b/Gemfile.lock index f60e3d6..4ba3db9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -73,6 +73,11 @@ GEM erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0) + active_storage_validations (1.0.4) + activejob (>= 5.2.0) + activemodel (>= 5.2.0) + activestorage (>= 5.2.0) + activesupport (>= 5.2.0) activejob (7.0.8) activesupport (= 7.0.8) globalid (>= 0.3.6) @@ -166,6 +171,9 @@ GEM highline (2.1.0) i18n (1.14.1) concurrent-ruby (~> 1.0) + image_processing (1.12.2) + mini_magick (>= 4.9.5, < 5) + ruby-vips (>= 2.0.17, < 3) jbuilder (2.11.5) actionview (>= 5.0.0) activesupport (>= 5.0.0) @@ -197,6 +205,7 @@ GEM mimemagic (0.3.10) nokogiri (~> 1) rake + mini_magick (4.12.0) mini_mime (1.1.5) mini_portile2 (2.8.4) mini_racer (0.8.0) @@ -272,6 +281,8 @@ GEM railties (>= 5.2) rexml (3.2.6) rouge (4.1.3) + ruby-vips (2.1.4) + ffi (~> 1.12) rubyzip (2.3.2) sassc (2.4.0) ffi (~> 1.9) @@ -339,6 +350,7 @@ PLATFORMS ruby DEPENDENCIES + active_storage_validations acts-as-taggable-on akismet audited (~> 5.0) @@ -354,6 +366,7 @@ DEPENDENCIES enumerize haml highline + image_processing (>= 1.2) jbuilder (~> 2.5) jquery-rails jquery-ui-rails diff --git a/app/assets/javascripts/admin/records.coffee b/app/assets/javascripts/admin/records.coffee index 27cdb78..3862b09 100644 --- a/app/assets/javascripts/admin/records.coffee +++ b/app/assets/javascripts/admin/records.coffee @@ -28,3 +28,5 @@ $(document).on "turbolinks:load", -> $(".tags-input input[type=text]").autocomplete({ source: Routes.suggest_tags_path() }) + $(".image-uploads pre").click -> + navigator.clipboard.writeText($(this).text()); diff --git a/app/assets/stylesheets/admin/layout.scss b/app/assets/stylesheets/admin/layout.scss index bf15b0d..9585414 100644 --- a/app/assets/stylesheets/admin/layout.scss +++ b/app/assets/stylesheets/admin/layout.scss @@ -82,19 +82,16 @@ body { } #entry-form { - display: flex; + display: grid; + grid-template-columns: 73% 27%; height: 100%; fieldset { border: 0; } - #content { - width: 77%; - } - #details { - width: 23%; + min-width: 0; display: flex; flex-direction: column; padding-left: 0; @@ -201,6 +198,22 @@ body { border-radius: 5px; padding: .5em; margin-bottom: 1em; + + h4 { + margin-top: 0; + margin-bottom: 0.5em; + } + + img { + max-width: 100%; + } +} + +.image-uploads { + pre { + overflow: auto; + max-width: 100%; + } } .should-create-record-field { diff --git a/app/controllers/admin/blogs_controller.rb b/app/controllers/admin/blogs_controller.rb index f0ce519..35f3514 100644 --- a/app/controllers/admin/blogs_controller.rb +++ b/app/controllers/admin/blogs_controller.rb @@ -57,7 +57,7 @@ class Admin::BlogsController < Admin::AdminController private def blog_params - params.require(:blog).permit(:title, :body, :slug, :published, :published_at, :tag_list, records_attributes: [:description, :_destroy]) + params.require(:blog).permit(:title, :body, :slug, :published, :published_at, :tag_list, images: [], records_attributes: [:description, :_destroy]) end def set_section diff --git a/app/models/blog.rb b/app/models/blog.rb index 362f69f..6db75ec 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb @@ -6,12 +6,15 @@ class Blog < ApplicationRecord has_many :comments belongs_to :user + has_many_attached :images do |attachable| + attachable.variant :thumb, resize_to_limit: [300, 300] + end + validates :title, presence: true validates :body, presence: true, if: :published validates :slug, presence: true, format: /\A[-a-z0-9]+\z/, if: :published validates :user, presence: true - - has_many_attached :images + validates :images, content_type: ['image/png', 'image/jpeg'] before_validation :set_draft_title before_save :set_published_at diff --git a/app/views/admin/blogs/_form.html.haml b/app/views/admin/blogs/_form.html.haml index 625c85e..c3720ba 100644 --- a/app/views/admin/blogs/_form.html.haml +++ b/app/views/admin/blogs/_form.html.haml @@ -39,4 +39,12 @@ = builder.label :_destroy, "Create record?" .record-description-field = builder.text_area :description, placeholder: "record text" + .details-module.image-uploads + %h4 Images + - f.object.images.each do |image| + = image_tag image.variant(:thumb) + = f.hidden_field :images, multiple: true, value: image.signed_id + %pre= url_for image + %hr + = f.file_field :images, multiple: true .details-module= f.submit diff --git a/config/deploy.rb b/config/deploy.rb index 05b137d..6c15029 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -24,7 +24,7 @@ set :deploy_to, "/srv/www/thoughts" append :linked_files, "config/database.yml", "config/secrets.yml", "config/lingo.yml", "config/akismet.yml", "config/mail.yml" # Default value for linked_dirs is [] -append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/uploads" +append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/uploads", "storage" # Default value for default_env is {} # set :default_env, { path: "/opt/ruby/bin:$PATH" } -- cgit 1.4.1