Skip to content

Commit

Permalink
Provide configuration features through either initializer or YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredcwhite committed Oct 9, 2022
1 parent 1a4ddeb commit 4d53bac
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 90 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.1.0] - 2022-10-09

- Provide configuration features through either initializer or YAML
- Update the automation script

## [2.0.0] - 2022-10-08

Expand Down
14 changes: 7 additions & 7 deletions bridgetown.automation.rb
Expand Up @@ -6,13 +6,13 @@

add_bridgetown_plugin "bridgetown-cloudinary"

append_to_file "bridgetown.config.yml" do
<<~YAML
cloudinary:
cloud_name: #{cloud_name}
YAML
add_initializer :"bridgetown-cloudinary" do
<<~RUBY
do
cloud_name "#{cloud_name}"
end
end
end
say_status :cloudinary, "All set! Double-check the cloudinary block in your config file and review docs at"
say_status :cloudinary, "All set! Double-check the cloudinary block in your config/initializers.rb file and review docs at"
say_status :cloudinary, "https://github.com/bridgetownrb/bridgetown-cloudinary"
31 changes: 30 additions & 1 deletion lib/bridgetown-cloudinary.rb
Expand Up @@ -5,6 +5,35 @@
require "bridgetown-cloudinary/utils"
require "bridgetown-cloudinary/builder"

Bridgetown.initializer :"bridgetown-cloudinary" do |config|
# rubocop:disable Metrics/ParameterLists
Bridgetown.initializer :"bridgetown-cloudinary" do |config,
cloud_name: nil,
default_transformation: "open_graph",
default_image_format: "jpg",
transformations: {},
add_transformed_urls_to_image_front_matter: false|

options = {
default_transformation: default_transformation,
default_image_format: default_image_format,
transformations: {
open_graph: "c_fill,g_face:center,w_1600,h_900,q_50",
tiny: "w_300,c_limit,q_90",
small: "w_600,c_limit,q_85",
medium: "w_1200,c_limit,q_80",
large: "w_1800,c_limit,q_80",
xlarge: "w_2048,c_limit,q_75",
}.merge(transformations),
add_transformed_urls_to_image_front_matter: add_transformed_urls_to_image_front_matter,
}

if config.cloudinary
config.cloudinary Bridgetown::Utils.deep_merge_hashes(options, config.cloudinary)
else
config.cloudinary(options)
end
config.cloudinary.cloud_name = cloud_name if cloud_name

config.builder Bridgetown::Cloudinary::Builder
end
# rubocop:enable Metrics/ParameterLists
17 changes: 0 additions & 17 deletions lib/bridgetown-cloudinary/builder.rb
Expand Up @@ -5,23 +5,6 @@ module Cloudinary
class Builder < Bridgetown::Builder
include Bridgetown::Filters

CONFIG_DEFAULTS = {
cloudinary: {
cloud_name: nil,
default_transformation: "open_graph",
default_image_format: "jpg",
transformations: {
open_graph: "c_fill,g_face:center,w_1600,h_900,q_50",
tiny: "w_300,c_limit,q_90",
small: "w_600,c_limit,q_85",
medium: "w_1200,c_limit,q_80",
large: "w_1800,c_limit,q_80",
xlarge: "w_2048,c_limit,q_75",
},
add_transformed_urls_to_image_front_matter: false,
},
}.freeze

# Set up the Cloudinary configuration
def build
::Cloudinary.config({
Expand Down
2 changes: 1 addition & 1 deletion lib/bridgetown-cloudinary/version.rb
Expand Up @@ -2,6 +2,6 @@

module Bridgetown
module Cloudinary
VERSION = "2.0.0"
VERSION = "2.1.0"
end
end
131 changes: 70 additions & 61 deletions spec/bridgetown-cloudinary_spec.rb
Expand Up @@ -2,93 +2,102 @@

require "spec_helper"

module TestValues
class << self
attr_accessor :config
end
end

describe(Bridgetown::Cloudinary) do
let(:overrides) { {} }
let(:config) do
Bridgetown.configuration(Bridgetown::Utils.deep_merge_hashes({
let(:config) {
TestValues::config ||= Bridgetown.configuration(Bridgetown::Utils.deep_merge_hashes({
"full_rebuild" => true,
"root_dir" => root_dir,
"source" => source_dir,
"destination" => dest_dir
}, overrides)).tap do |conf|
conf.run_initializers! context: :static
end
end
let(:metadata_overrides) { {} }
let(:metadata_defaults) do
{
"name" => "My Awesome Site",
"author" => {
"name" => "Ada Lovejoy",
}
}
end
let(:site) { Bridgetown::Site.new(config) }
let(:contents) { File.read(dest_dir("index.html")) }
before(:each) do
metadata = metadata_defaults.merge(metadata_overrides).to_yaml.sub("---\n", "")
File.write(source_dir("_data/site_metadata.yml"), metadata)
site.process
FileUtils.rm(source_dir("_data/site_metadata.yml"))
end
}

context "Liquid tag" do
it "outputs with an inline id" do
expect(contents).to match '<img alt="This is inline" src="https://res.cloudinary.com/bridgetown_test/image/upload/c_fill,g_face:center,w_1600,h_900,q_50/inline_id.jpg" />'
describe "plugin behavior" do
let(:overrides) { {} }
let(:metadata_overrides) { {} }
let(:metadata_defaults) do
{
"name" => "My Awesome Site",
"author" => {
"name" => "Ada Lovejoy",
}
}
end

it "outputs with a page variable" do
expect(contents).to match '<img alt="This is page" src="https://res.cloudinary.com/bridgetown_test/image/upload/c_fill,g_face:center,w_1600,h_900,q_50/the_id_123.jpg" />'
let(:site) { Bridgetown::Site.new(config) }
let(:contents) { File.read(dest_dir("index.html")) }
before(:each) do
metadata = metadata_defaults.merge(metadata_overrides).to_yaml.sub("---\n", "")
File.write(source_dir("_data/site_metadata.yml"), metadata)
site.process
FileUtils.rm(source_dir("_data/site_metadata.yml"))
end

it "outputs with a local object" do
expect(contents).to match '<img alt="This is local object" src="https://res.cloudinary.com/bridgetown_test/image/upload/c_fill,g_face:center,w_1600,h_900,q_50/the_id_123.jpg" />'
end
context "Liquid tag" do
it "outputs with an inline id" do
expect(contents).to match '<img alt="This is inline" src="https://res.cloudinary.com/bridgetown_test/image/upload/c_fill,g_face:center,w_1600,h_900,q_50/inline_id.jpg" />'
end

it "outputs with a local variable" do
expect(contents).to match '<img alt="This is local variable" src="https://res.cloudinary.com/bridgetown_test/image/upload/c_fill,g_face:center,w_1600,h_900,q_50/local_id.jpg" />'
end
it "outputs with a page variable" do
expect(contents).to match '<img alt="This is page" src="https://res.cloudinary.com/bridgetown_test/image/upload/c_fill,g_face:center,w_1600,h_900,q_50/the_id_123.jpg" />'
end

it "escapes alt text" do
expect(contents).to match '<img alt="Alt text &quot; class=&quot;badclass" src="https://res.cloudinary.com/bridgetown_test/image/upload/w_4096,c_limit,q_65/local_id.jpg" />'
end
end
it "outputs with a local object" do
expect(contents).to match '<img alt="This is local object" src="https://res.cloudinary.com/bridgetown_test/image/upload/c_fill,g_face:center,w_1600,h_900,q_50/the_id_123.jpg" />'
end

context "transformations" do
it "outputs a custom transformation" do
expect(contents).to match '<img alt="Transformation" src="https://res.cloudinary.com/bridgetown_test/image/upload/w_4096,c_limit,q_65/local_id.jpg" />'
end
end
it "outputs with a local variable" do
expect(contents).to match '<img alt="This is local variable" src="https://res.cloudinary.com/bridgetown_test/image/upload/c_fill,g_face:center,w_1600,h_900,q_50/local_id.jpg" />'
end

context "Liquid filter" do
it "outputs a URL" do
expect(contents).to match "https://res.cloudinary.com/bridgetown_test/image/upload/c_fill,g_face:center,w_1600,h_900,q_50/filtered_id.jpg"
it "escapes alt text" do
expect(contents).to match '<img alt="Alt text &quot; class=&quot;badclass" src="https://res.cloudinary.com/bridgetown_test/image/upload/w_4096,c_limit,q_65/local_id.jpg" />'
end
end

it "outputs a URL with custom transformation" do
expect(contents).to match "https://res.cloudinary.com/bridgetown_test/image/upload/w_1200,c_limit,q_80/filtered_id_max.jpg"
context "transformations" do
it "outputs a custom transformation" do
expect(contents).to match '<img alt="Transformation" src="https://res.cloudinary.com/bridgetown_test/image/upload/w_4096,c_limit,q_65/local_id.jpg" />'
end
end
end

context "image generator" do
it "sets the page's image variable default path" do
expect(contents).to match "image: https://res.cloudinary.com/bridgetown_test/image/upload/c_fill,g_face:center,w_1600,h_900,q_50/the_id_123.jpg"
end
context "Liquid filter" do
it "outputs a URL" do
expect(contents).to match "https://res.cloudinary.com/bridgetown_test/image/upload/c_fill,g_face:center,w_1600,h_900,q_50/filtered_id.jpg"
end

it "sets the page's image variable tiny path" do
expect(contents).to match "tiny: https://res.cloudinary.com/bridgetown_test/image/upload/w_300,c_limit,q_90/the_id_123.jpg"
it "outputs a URL with custom transformation" do
expect(contents).to match "https://res.cloudinary.com/bridgetown_test/image/upload/w_1200,c_limit,q_80/filtered_id_max.jpg"
end
end

it "supports custom transformations" do
expect(contents).to match "maxsize: https://res.cloudinary.com/bridgetown_test/image/upload/w_4096,c_limit,q_65/the_id_123.jpg"
context "image generator" do
it "sets the page's image variable default path" do
expect(contents).to match "image: https://res.cloudinary.com/bridgetown_test/image/upload/c_fill,g_face:center,w_1600,h_900,q_50/the_id_123.jpg"
end

it "sets the page's image variable tiny path" do
expect(contents).to match "tiny: https://res.cloudinary.com/bridgetown_test/image/upload/w_300,c_limit,q_90/the_id_123.jpg"
end

it "supports custom transformations" do
expect(contents).to match "maxsize: https://res.cloudinary.com/bridgetown_test/image/upload/w_4096,c_limit,q_65/the_id_123.jpg"
end
end
end

context "check resource engine too" do
let(:overrides) { { content_engine: "resource" } }
context "check resource engine too" do
let(:overrides) { { content_engine: "resource" } }

it "sets the page's image variable default path" do
expect(contents).to match "image: https://res.cloudinary.com/bridgetown_test/image/upload/c_fill,g_face:center,w_1600,h_900,q_50/the_id_123.jpg"
it "sets the page's image variable default path" do
expect(contents).to match "image: https://res.cloudinary.com/bridgetown_test/image/upload/c_fill,g_face:center,w_1600,h_900,q_50/the_id_123.jpg"
end
end
end
end
3 changes: 1 addition & 2 deletions spec/fixtures/bridgetown.config.yml
Expand Up @@ -2,8 +2,7 @@ timezone: UTC

# content_engine: resource

# partial config here, also in initializers.rb
cloudinary:
cloud_name: bridgetown_test
transformations:
maxsize: w_4096,c_limit,q_65
add_transformed_urls_to_image_front_matter: true
6 changes: 5 additions & 1 deletion spec/fixtures/config/initializers.rb
@@ -1,5 +1,9 @@
# frozen_string_literal: true

Bridgetown.configure do
init :"bridgetown-cloudinary"
# partial config here, also in bridgetown.config.yml
init :"bridgetown-cloudinary" do
cloud_name "bridgetown_test"
add_transformed_urls_to_image_front_matter true
end
end

0 comments on commit 4d53bac

Please sign in to comment.