maglevcms-shrine 1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3658da12d4c22331a5ef2fc56973d3acfd58c9c7b7f83157d6f5bf785d013029
4
+ data.tar.gz: e2beeefd16aa6534f93ab155f37af56ee8fe71767eda96e1cd2bbb81db031b5b
5
+ SHA512:
6
+ metadata.gz: df9cc9c64dfe5efdabd79092a2fa1157e8d2573d4e646fd22d560c3d595763ebf1a0b6d375d008cd9e955c320599f0c7ae9c0c685debb28105c9f7648ff9eaa1
7
+ data.tar.gz: 445844f4cfd8256f54ef75f84251a3f34a8c66069f990f0ff25fb8409c301e510457cb79c4ca37b6fb6583b7a09efe2e9d66f68c3e14b57602aa49117c634ec9
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Maglev Shrine
2
+
3
+ Change [Maglev CMS](https://github.com/maglevhq/maglev-core) uploader (asset management) to use
4
+ Shrine instead of Active Storage.
5
+
6
+ ## Requirements
7
+
8
+ * Maglev 1.2+ (might work with earlier versions, but it's untested)
9
+ * Shrine 3.5+ (might work with earlier versions, but it's untested)
10
+
11
+ ## Installation
12
+
13
+ Add the gem to your Gemfile and run `bundle install`:
14
+
15
+ ```
16
+ gem 'maglevcms-shrine'
17
+ ```
18
+
19
+ Generate migration, run migrations and add a new initializer to configure the gem:
20
+
21
+ ```
22
+ bundle exec rails g maglev:shrine:instal
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ No changes are required from the application side. The gem takes care of doing all the necessary
28
+ changes so Maglev starts using Shrine.
29
+
30
+ **NOTE**: Active Storage needs to still be loaded in the application because `app/controllers/maglev/api/assets_controller.rb` in the maglev-core gem uses it.
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## TODO
39
+
40
+ * Add specs.
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddFileDataToMaglevAssets < ActiveRecord::Migration[7.0]
4
+
5
+ def change
6
+ add_column :maglev_assets, :file_data, :jsonb
7
+ end
8
+
9
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Maglev
4
+ module Shrine
5
+ class InstallGenerator < Rails::Generators::Base
6
+
7
+ desc 'Install Maglev Shrine integration'
8
+ source_root File.expand_path('templates/install', __dir__)
9
+
10
+ def migrations
11
+ rake 'maglev_shrine:install:migrations'
12
+ rake 'db:migrate'
13
+ end
14
+
15
+ def create_initializer
16
+ directory 'config'
17
+ end
18
+
19
+ def instructions
20
+ $stdout.puts <<~INFO
21
+ Done! 🎉
22
+
23
+ You can now tweak config/initializers/maglev_shrine.rb
24
+ INFO
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ Maglev::Shrine.configure do |config|
4
+ # Change accepted file formats when uploading assets.
5
+ # Used by the Shrine validation plugin.
6
+ # config.accepted_mime_types = %w[image/jpeg image/png image/webp image/gif image/heic]
7
+
8
+ # Change maximum allowed file size.
9
+ # Used by the Shrine validation plugin.
10
+ # config.max_file_size = 20.megabytes
11
+
12
+ # Change analyzer used to extract image dimensions.
13
+ # This value is used by the Shrine store_dimensions plugin. Refer to the plugin
14
+ # documentation for a list of supported values.
15
+ # https://shrinerb.com/docs/plugins/store_dimensions
16
+ # config.store_dimensions_plugin_analyzer = :ruby_vips
17
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Maglev
4
+ module Shrine
5
+ class AssetFileUploader < ::Shrine
6
+
7
+ plugin :determine_mime_type,
8
+ analyzer: :marcel,
9
+ analyzer_options: { filename_fallback: true }
10
+
11
+ plugin :store_dimensions,
12
+ analyzer: Maglev::Shrine.config.store_dimensions_plugin_analyzer
13
+
14
+ plugin :validation_helpers
15
+
16
+ Attacher.validate do
17
+ validate_max_size Maglev::Shrine.config.max_file_size
18
+ validate_mime_type Maglev::Shrine.config.accepted_mime_types
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Maglev
4
+ module Shrine
5
+ Config = Struct.new(:accepted_mime_types, :max_file_size,
6
+ :store_dimensions_plugin_analyzer)
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Maglev
4
+ module Shrine
5
+ class Engine < ::Rails::Engine
6
+
7
+ isolate_namespace Maglev::Shrine
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Maglev
4
+ module Shrine
5
+ module Uploader
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ before_save :set_metadata
10
+
11
+ include AssetFileUploader::Attachment(:file)
12
+
13
+ delegate :url, to: :file
14
+ end
15
+
16
+ def download
17
+ file.download.read
18
+ end
19
+
20
+ private
21
+
22
+ def set_metadata
23
+ metadata = file.metadata
24
+ assign_attributes(
25
+ filename: metadata['filename'],
26
+ content_type: metadata['mime_type'],
27
+ byte_size: metadata['size'],
28
+ height: metadata['height'],
29
+ width: metadata['width']
30
+ )
31
+ end
32
+
33
+ module ClassMethods
34
+ def optimized
35
+ all
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Maglev
4
+ module Shrine
5
+ VERSION = '1.0'
6
+ end
7
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'maglev/shrine/engine'
4
+ require 'maglev/shrine/config'
5
+
6
+ module Maglev
7
+ module Shrine
8
+ # We use autoload instead of require to load these classes after Rails
9
+ # initializers have loaded. Shrine uploaders inherit from the Shrine class
10
+ # At the time of loading the class, it copies all configuration from the
11
+ # Shrine class. This includes configuration done on the Shrine initializer
12
+ # by using Shrine.plugin or other method calls.
13
+ # If we require the AssetFileUploder class when requiring the gem, then it
14
+ # will not copy any configuration, because the Shrine initializer has not
15
+ # been run yet.
16
+ # By delaying requiring until the classes is requested, we make sure the
17
+ # Shrine base class already has all necessary configuration set up.
18
+ autoload :AssetFileUploader, 'maglev/shrine/asset_file_uploader'
19
+ autoload :Uploader, 'maglev/shrine/uploader'
20
+
21
+ class << self
22
+
23
+ def config
24
+ @config ||= Config.new.tap do |c|
25
+ c.accepted_mime_types = %w[image/jpeg image/png image/webp image/gif image/heic]
26
+ c.max_file_size = 20.megabytes
27
+ c.store_dimensions_plugin_analyzer = :ruby_vips
28
+ end
29
+ end
30
+
31
+ def configure
32
+ yield config
33
+ end
34
+
35
+ end
36
+ end
37
+
38
+ # Maglev allows to change the uploader by changing `config.uploader`.
39
+ # However it searches the uploader only inside the maglevcms gem, so it's not possible
40
+ # to specify a custom uploader. We override the `uploader` method to fix.
41
+ class << self
42
+
43
+ def uploader
44
+ Maglev::Shrine::Uploader
45
+ end
46
+
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maglevcms-shrine
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - MarsBased
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-10-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: maglevcms
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: shrine
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ description: Changes assets management in Maglev CMS to use Shrine.
42
+ email:
43
+ - dev@marsbased.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - README.md
49
+ - db/migrate/20231003115104_add_file_data_to_maglev_assets.rb
50
+ - lib/generators/maglev/shrine/install_generator.rb
51
+ - lib/generators/maglev/shrine/templates/install/config/initializers/maglev_shrine.rb
52
+ - lib/maglev/shrine/asset_file_uploader.rb
53
+ - lib/maglev/shrine/config.rb
54
+ - lib/maglev/shrine/engine.rb
55
+ - lib/maglev/shrine/uploader.rb
56
+ - lib/maglev/shrine/version.rb
57
+ - lib/maglevcms-shrine.rb
58
+ homepage: https://github.com/MarsBased/maglevcms-shrine
59
+ licenses:
60
+ - MIT
61
+ metadata:
62
+ homepage_uri: https://github.com/MarsBased/maglevcms-shrine
63
+ source_code_uri: https://github.com/MarsBased/maglevcms-shrine
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 2.7.0
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubygems_version: 3.4.20
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Integrates Maglev CMS with Shrine.
83
+ test_files: []