Skip to content

Commit 4835c1c

Browse files
authored
Merge pull request #3371 from AlchemyCMS/dragonfly-adapter-configurable-url-classes
Allow to configure url classes in Dragonfly adapter
2 parents ec99df6 + 3327305 commit 4835c1c

5 files changed

Lines changed: 63 additions & 25 deletions

File tree

app/models/alchemy/storage_adapter/dragonfly.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,30 @@ def self.included(base)
3838

3939
CONVERTIBLE_FILE_FORMATS = %w[gif jpg jpeg png webp].freeze
4040

41+
# Allows to set a custom Attachment Url class
42+
def attachment_url_class=(klass)
43+
@_attachment_url_class = klass
44+
end
45+
46+
# Returns the class used to generate attachment urls
47+
# @return [Class] - defaults to Alchemy::StorageAdapter::Dragonfly::AttachmentUrl
4148
def attachment_url_class
42-
AttachmentUrl
49+
@_attachment_url_class ||= AttachmentUrl
4350
end
4451

4552
def preprocessor_class
4653
Preprocessor
4754
end
4855

56+
# Allows to set a custom Picture Url class
57+
def picture_url_class=(klass)
58+
@_picture_url_class = klass
59+
end
60+
61+
# Returns the class used to generate picture urls
62+
# @return [Class] - defaults to Alchemy::StorageAdapter::Dragonfly::PictureUrl
4963
def picture_url_class
50-
PictureUrl
64+
@_picture_url_class ||= PictureUrl
5165
end
5266

5367
def file_formats(class_name, scope:)

app/models/alchemy/storage_adapter/dragonfly/picture_url.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ def initialize(picture)
2626
#
2727
# @return [String]
2828
#
29-
def call(options = {})
30-
@variant = PictureVariant.new(picture, options.slice(*TRANSFORMATION_OPTIONS))
29+
def call(variant_options = {})
30+
set_variant(variant_options)
31+
3132
params = {
3233
basename: picture.name,
3334
ext: variant.render_format,
@@ -41,6 +42,10 @@ def call(options = {})
4142

4243
private
4344

45+
def set_variant(options = {})
46+
@variant = PictureVariant.new(picture, options.slice(*TRANSFORMATION_OPTIONS))
47+
end
48+
4449
def processible_image?
4550
variant.image.is_a?(::Dragonfly::Job)
4651
end

lib/alchemy/install/tasks.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,6 @@ def inject_routes(auto_accept = false)
2222
{after: SENTINEL, verbose: true}
2323
end
2424

25-
def set_primary_language(code: "en", name: "English", auto_accept: false)
26-
unless auto_accept
27-
code = ask("- What is the language code of your site's primary language?", default: code)
28-
end
29-
unless auto_accept
30-
name = ask("- What is the name of your site's primary language?", default: name)
31-
end
32-
gsub_file "./config/alchemy/config.yml", /default_language:\n\s\scode:\sen\n\s\sname:\sEnglish/m do
33-
"default_language:\n code: #{code}\n name: #{name}"
34-
end
35-
end
36-
3725
def inject_seeder
3826
seed_file = Rails.root.join("db", "seeds.rb")
3927
args = [seed_file, "Alchemy::Seeder.seed!\n"]

lib/generators/alchemy/install/install_generator.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,6 @@ def install_gutentag_migrations
8989
rake "gutentag:install:migrations"
9090
end
9191

92-
def set_primary_language
93-
header
94-
install_tasks.set_primary_language(
95-
code: options[:default_language_code],
96-
name: options[:default_language_name],
97-
auto_accept: options[:auto_accept]
98-
)
99-
end
100-
10192
def setup_database
10293
rake("db:create", abort_on_failure: true) unless options[:skip_db_create]
10394
# We can't invoke this rake task, because Rails will use wrong engine names otherwise

spec/models/alchemy/storage_adapter/dragonfly_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,46 @@
7171
end
7272
end
7373

74+
describe ".attachment_url_class" do
75+
subject { described_class.attachment_url_class }
76+
77+
it { is_expected.to eq(Alchemy::StorageAdapter::Dragonfly::AttachmentUrl) }
78+
end
79+
80+
describe ".attachment_url_class=" do
81+
let(:custom_class) { Class.new }
82+
83+
subject { described_class.attachment_url_class }
84+
85+
around do |example|
86+
described_class.attachment_url_class = custom_class
87+
example.run
88+
described_class.attachment_url_class = nil
89+
end
90+
91+
it { is_expected.to eq(custom_class) }
92+
end
93+
94+
describe ".picture_url_class" do
95+
subject { described_class.picture_url_class }
96+
97+
it { is_expected.to eq(Alchemy::StorageAdapter::Dragonfly::PictureUrl) }
98+
end
99+
100+
describe ".picture_url_class=" do
101+
let(:custom_class) { Class.new }
102+
103+
subject { described_class.picture_url_class }
104+
105+
around do |example|
106+
described_class.picture_url_class = custom_class
107+
example.run
108+
described_class.picture_url_class = nil
109+
end
110+
111+
it { is_expected.to eq(custom_class) }
112+
end
113+
74114
describe ".file_formats" do
75115
subject(:file_formats) do
76116
described_class.file_formats(class_name, scope: scope)

0 commit comments

Comments
 (0)