lazy_blob_storage 2.0.0.pre.beta.2 → 2.0.0.pre.beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/app/models/concerns/lazy_attachable.rb +13 -13
- data/app/models/lazy_attachment.rb +4 -0
- data/app/models/lazy_blob.rb +2 -2
- data/db/migrate/20200213161207_create_lazy_blobs.rb +13 -1
- data/db/migrate/20200213164215_create_lazy_attachments.rb +13 -3
- data/lib/lazy_blob_storage/version.rb +1 -1
- data/lib/lazy_blob_storage.rb +4 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 45ca84928e6e354ad7966a97cf15c4d5ab6f93aa952c507f12ab405a30d12310
|
4
|
+
data.tar.gz: 0d4f5893ddb9acfa1631b63177f3951dc178492be3c17781ea661d141d887da9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad52f8ca8bcb8cb31f9a683b96ac88cd2d7ef92e6176affe55ea30d2a28822de2709c59914719b03a2bcde0e015ec80854be9f61b1d99889f7bcc6d3580a93fe
|
7
|
+
data.tar.gz: e0da5bf09d25d351e1cda6edac680efd82b2a18e56bf514f3e82e7e6b13c61ef4eb6be3263ca7b28fb1fc9c01a6865e05828363efd520e35664f0e43ba9c4890
|
data/README.md
CHANGED
@@ -19,9 +19,9 @@ Don't use this. I mean it.
|
|
19
19
|
|
20
20
|
## Breaking Changes in 2.0.0-beta.1
|
21
21
|
|
22
|
+
- Support UUID key types
|
22
23
|
- YOLO support for rails > 6 (it's probably fine)
|
23
24
|
- Migrations do not get added to rails paths automatically anymore. They must be copied with the `lazy_blob_storage:install:migrations` rake task.
|
24
|
-
- Prefixed most attachment methods with `lazy_` to avoid conflicts.
|
25
25
|
- The default max blob size is 100kb. It should probably be lower. You can set this to a number of bytes with `LazyBlobStorage.max_blob_size=`
|
26
26
|
- !!! Do not use this for high traffic projects. !!!
|
27
27
|
- !!! Do not use this to store big files !!!
|
@@ -37,7 +37,7 @@ Built with Rails 6. It could ~probably~ work with Rails 4/5, too.
|
|
37
37
|
## Usage
|
38
38
|
|
39
39
|
1. Add `gem "lazy_blob_storage" to Gemfile`
|
40
|
-
1. Run `rake
|
40
|
+
1. Run `rake lazy_blob_storage_engine:install:migrations`
|
41
41
|
1. Run `rake db:migrate`
|
42
42
|
1. Declare `has_lazy_attached :image` in some model
|
43
43
|
1. Add `f.file_field :image_upload` to the form template
|
@@ -7,7 +7,7 @@ module LazyAttachable
|
|
7
7
|
if (send "#{name}_upload").present? && (send "#{name}_upload_size_ok?")
|
8
8
|
blob = LazyBlob.from_upload(send "#{name}_upload")
|
9
9
|
|
10
|
-
if (attached = (send "
|
10
|
+
if (attached = (send "attached_#{name}"))
|
11
11
|
attached.lazy_blob = blob
|
12
12
|
else
|
13
13
|
attachment = LazyAttachment.new(
|
@@ -15,7 +15,7 @@ module LazyAttachable
|
|
15
15
|
record: self,
|
16
16
|
lazy_blob: blob
|
17
17
|
)
|
18
|
-
(send "
|
18
|
+
(send "attached_#{name}=", attachment)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
@@ -26,22 +26,22 @@ module LazyAttachable
|
|
26
26
|
|
27
27
|
define_method("validate_size_of_#{name}_upload") do
|
28
28
|
if (send "#{name}_upload").present? && !(send "#{name}_upload_size_ok?")
|
29
|
-
errors.add("#{name}_upload", "file too large (limit
|
29
|
+
errors.add("#{name}_upload", "file too large (limit #{LazyBlobStorage._max_blob_size / 1_000} kb")
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
define_method("
|
33
|
+
define_method("attached_#{name}_path") do
|
34
34
|
if (send "#{name}_attached?")
|
35
|
-
digest = (send "
|
35
|
+
digest = (send "attached_#{name}").digest
|
36
36
|
Rails.application.routes.url_helpers.lazy_blob_path(digest)
|
37
37
|
else
|
38
38
|
""
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
define_method("
|
42
|
+
define_method("attached_#{name}_url") do
|
43
43
|
if (send "#{name}_attached?")
|
44
|
-
digest = (send "
|
44
|
+
digest = (send "attached_#{name}").digest
|
45
45
|
Rails.application.routes.url_helpers.lazy_blob_url(digest)
|
46
46
|
else
|
47
47
|
""
|
@@ -49,12 +49,12 @@ module LazyAttachable
|
|
49
49
|
end
|
50
50
|
|
51
51
|
define_method("#{name}_attached?") do
|
52
|
-
(send "
|
52
|
+
(send "attached_#{name}").present?
|
53
53
|
end
|
54
54
|
|
55
55
|
define_method("handle_#{name}_digest") do
|
56
56
|
digest = (send "#{name}_digest").presence
|
57
|
-
attached = (send "
|
57
|
+
attached = (send "attached_#{name}").presence
|
58
58
|
same_as_attached = (attached && digest) && (attached.digest == digest)
|
59
59
|
|
60
60
|
if digest && !same_as_attached
|
@@ -67,7 +67,7 @@ module LazyAttachable
|
|
67
67
|
digest: digest
|
68
68
|
)
|
69
69
|
|
70
|
-
(send "
|
70
|
+
(send "attached_#{name}=", attachment)
|
71
71
|
end
|
72
72
|
end
|
73
73
|
end
|
@@ -80,15 +80,15 @@ module LazyAttachable
|
|
80
80
|
attr_accessor "#{name}_upload"
|
81
81
|
attr_accessor "#{name}_digest"
|
82
82
|
|
83
|
-
has_one "
|
83
|
+
has_one "attached_#{name}".to_sym,
|
84
84
|
-> { where(name: name) },
|
85
85
|
class_name: "LazyAttachment",
|
86
86
|
as: :record,
|
87
87
|
autosave: true,
|
88
88
|
dependent: :destroy
|
89
89
|
|
90
|
-
scope "
|
91
|
-
includes("
|
90
|
+
scope "with_attached_#{name}".to_sym, -> {
|
91
|
+
includes("attached_#{name}")
|
92
92
|
}
|
93
93
|
|
94
94
|
validate "validate_size_of_#{name}_upload".to_sym
|
data/app/models/lazy_blob.rb
CHANGED
@@ -7,9 +7,9 @@ class LazyBlob < ActiveRecord::Base
|
|
7
7
|
validates :content_type, presence: true
|
8
8
|
validates :content_length,
|
9
9
|
presence: true,
|
10
|
-
numericality: { greater_than: 0, less_than: LazyBlobStorage.
|
10
|
+
numericality: { greater_than: 0, less_than: LazyBlobStorage._max_blob_size }
|
11
11
|
validates :digest, presence: true, uniqueness: true
|
12
|
-
validates :content, presence: true, length: { maximum: LazyBlobStorage.
|
12
|
+
validates :content, presence: true, length: { maximum: LazyBlobStorage._max_blob_size }
|
13
13
|
|
14
14
|
def self.from_upload(uploaded_file)
|
15
15
|
file = File.open(uploaded_file.tempfile.path, 'rb')
|
@@ -1,6 +1,9 @@
|
|
1
1
|
class CreateLazyBlobs < ActiveRecord::Migration[6.0]
|
2
2
|
def change
|
3
|
-
|
3
|
+
# Use Active Record's configured type for primary and foreign keys
|
4
|
+
primary_key_type, foreign_key_type = primary_and_foreign_key_types
|
5
|
+
|
6
|
+
create_table :lazy_blobs, id: primary_key_type do |t|
|
4
7
|
t.string :filename
|
5
8
|
t.string :content_type
|
6
9
|
t.integer :content_length
|
@@ -9,4 +12,13 @@ class CreateLazyBlobs < ActiveRecord::Migration[6.0]
|
|
9
12
|
t.timestamps
|
10
13
|
end
|
11
14
|
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def primary_and_foreign_key_types
|
18
|
+
config = Rails.configuration.generators
|
19
|
+
setting = config.options[config.orm][:primary_key_type]
|
20
|
+
primary_key_type = setting || :primary_key
|
21
|
+
foreign_key_type = setting || :bigint
|
22
|
+
[primary_key_type, foreign_key_type]
|
23
|
+
end
|
12
24
|
end
|
@@ -1,11 +1,21 @@
|
|
1
1
|
class CreateLazyAttachments < ActiveRecord::Migration[6.0]
|
2
2
|
def change
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
primary_key_type, foreign_key_type = primary_and_foreign_key_types
|
4
|
+
|
5
|
+
create_table :lazy_attachments, id: primary_key_type do |t|
|
6
|
+
t.references :lazy_blob, type: foreign_key_type
|
7
|
+
t.references :record, polymorphic: true, type: foreign_key_type
|
6
8
|
t.string :name
|
7
9
|
t.index :name
|
8
10
|
t.timestamps
|
9
11
|
end
|
10
12
|
end
|
13
|
+
|
14
|
+
def primary_and_foreign_key_types
|
15
|
+
config = Rails.configuration.generators
|
16
|
+
setting = config.options[config.orm][:primary_key_type]
|
17
|
+
primary_key_type = setting || :primary_key
|
18
|
+
foreign_key_type = setting || :bigint
|
19
|
+
[primary_key_type, foreign_key_type]
|
20
|
+
end
|
11
21
|
end
|
data/lib/lazy_blob_storage.rb
CHANGED