Skip to content

Commit

Permalink
Merge pull request #22 from chonla/main
Browse files Browse the repository at this point in the history
Add preview image for social share
  • Loading branch information
chonla authored Oct 25, 2024
2 parents b590cb3 + 44726f6 commit c814d0a
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .direnv/flake-profile
1 change: 0 additions & 1 deletion .direnv/flake-profile-1-link

This file was deleted.

1 change: 1 addition & 0 deletions .direnv/flake-profile-4-link
10 changes: 10 additions & 0 deletions .github/workflows/rubyonrails.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ jobs:
with:
ruby-version: '3.3.5'
bundler-cache: true
- name: Install dependencies
run: |
sudo apt-get update && sudo apt-get install libfuse2
mkdir -p "$HOME/.local/bin"
curl -O 'https://imagemagick.org/archive/binaries/magick'
chmod u+x magick
mv magick "$HOME/.local/bin"
echo "$HOME/.local/bin" >> "$GITHUB_PATH"
- name: Verify dependencies
run: magick -version
- name: Set up database schema
working-directory: ./cdmm
run: bundle install && bin/rails db:schema:load && bin/rails db:migrate
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@

/cdmm/app/assets/builds/*
!/cdmm/app/assets/builds/.keep

.DS_Store
2 changes: 2 additions & 0 deletions cdmm/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ gem "bootsnap", require: false

gem "meta-tags"

gem 'rmagick'

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

Expand Down
6 changes: 6 additions & 0 deletions cdmm/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,12 @@ GEM
racc (~> 1.4)
nokogiri (1.16.7-x86_64-linux)
racc (~> 1.4)
observer (0.1.2)
parallel (1.26.3)
parser (3.3.5.0)
ast (~> 2.4.1)
racc
pkg-config (1.5.7)
psych (5.1.2)
stringio
public_suffix (6.0.1)
Expand Down Expand Up @@ -231,6 +233,9 @@ GEM
reline (0.5.10)
io-console (~> 0.5)
rexml (3.3.8)
rmagick (6.0.1)
observer (~> 0.1)
pkg-config (~> 1.4)
rubocop (1.67.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
Expand Down Expand Up @@ -347,6 +352,7 @@ DEPENDENCIES
puma (>= 5.0)
rails (~> 7.2.1, >= 7.2.1.1)
rails_live_reload
rmagick
rubocop-rails-omakase
selenium-webdriver
sprockets-rails
Expand Down
3 changes: 2 additions & 1 deletion cdmm/app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//= link_tree ../images
//= link_directory ../fonts .ttf
//= link_directory ../stylesheets .css
//= link_tree ../../javascript .js
//= link_tree ../../../vendor/javascript .js
//= link_tree ../builds
//= link_tree ../builds
Binary file added cdmm/app/assets/fonts/Prompt-Bold.ttf
Binary file not shown.
Binary file added cdmm/app/assets/fonts/Prompt-Regular.ttf
Binary file not shown.
Binary file added cdmm/app/assets/images/preview-background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 84 additions & 0 deletions cdmm/app/controllers/cdmm_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'rmagick'

class CdmmController < ApplicationController
include ApplicationHelper
# Viewing
Expand All @@ -13,6 +15,88 @@ class CdmmController < ApplicationController
# Purging
# draft form will be purged regularly

def preview
options = {
:dimension => {
:width => 820,
:height => 320
},
:border_bar => {
:height => 15,
:color => "#336699"
},
:padding => {
:left => 50,
:top => 50
},
:title => {
:size => 32,
:font => Rails.root.join('app', 'assets', 'fonts', 'Prompt-Bold.ttf').to_s,
:color => "black"
},
:subtitle => {
:size => 24,
:font => Rails.root.join('app', 'assets', 'fonts', 'Prompt-Regular.ttf').to_s,
:color => "#505050"
},
:background => {
:image => Rails.root.join('app', 'assets', 'images', 'preview-background.png').to_s,
}
}


@form = Evaluation.find_by(form_key: params[:form_key])
if @form
image = Magick::Image.new(options[:dimension][:width], options[:dimension][:height]) { |options| options.background_color = 'white' }
draw = Magick::Draw.new

# Draw background
background_image = Magick::Image.read(options[:background][:image]).first
background_image.resize!(0.40)

image = image.composite(background_image, Magick::SouthEastGravity, Magick::OverCompositeOp)

# Draw top border
draw.fill(options[:border_bar][:color])
draw.rectangle(0, 0, options[:dimension][:width], options[:border_bar][:height])
draw.draw(image)

# Draw title
draw.font_weight = Magick::BoldWeight
draw.pointsize = options[:title][:size]
draw.gravity = Magick::NorthWestGravity
draw.annotate(image,
options[:dimension][:width] - (options[:padding][:left] * 2),
options[:dimension][:height] - (options[:padding][:top] * 2),
options[:padding][:left],
options[:padding][:top],
@form[:title]) do |opts|
opts.font = options[:title][:font]
opts.fill = options[:title][:color]
end

# Draw subtitle
draw.font_weight = Magick::NormalWeight
draw.pointsize = options[:subtitle][:size]
draw.gravity = Magick::NorthWestGravity
draw.annotate(image,
options[:dimension][:width] - (options[:padding][:left] * 2),
options[:dimension][:height] - (options[:padding][:top] * 2),
options[:padding][:left],
options[:padding][:top] + (options[:title][:size] * 1.3),
"Continuous Delivery Maturity Model") do |opts|
opts.font = options[:subtitle][:font]
opts.fill = options[:subtitle][:color]
end

image.format = 'PNG'

send_data image.to_blob, type: 'image/png', disposition: 'inline'
else
render_not_found
end
end

def purge
deleted_rows = Evaluation.where(:form_status => :draft, :created_at => ...6.hours.ago)
deleted_count = deleted_rows.count
Expand Down
1 change: 1 addition & 0 deletions cdmm/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
get "cdmm" => "cdmm#index", as: :evaluation_index
post "cdmm" => "cdmm#save", as: :evaluation_save
get "cdmm/:form_key" => "cdmm#show", as: :evaluation_show
get "cdmm/:form_key/preview" => "cdmm#preview"
get "schedule/purge" => "cdmm#purge"

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
${system}.default = nixpkgs.legacyPackages.${system}.mkShell {
buildInputs = with nixpkgs.legacyPackages.${system}; [
ruby_3_3
imagemagick
# playwright
# playwright-driver
];
Expand Down

0 comments on commit c814d0a

Please sign in to comment.