lazy_blob_storage 2.0.0.pre.beta.3 → 2.0.0.pre.beta.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c675223c056ffa300a4910dea9ff45c01ea77c8115d673e530105dc85e7fdc4e
4
- data.tar.gz: 54b144adb7025c08c862333874cb2943f84177020fa4b5965003232c8353bb27
3
+ metadata.gz: 45ca84928e6e354ad7966a97cf15c4d5ab6f93aa952c507f12ab405a30d12310
4
+ data.tar.gz: 0d4f5893ddb9acfa1631b63177f3951dc178492be3c17781ea661d141d887da9
5
5
  SHA512:
6
- metadata.gz: a88c198067cdf6ec766b975ee96c3c22553c7b6832491db51cc681d616b9b735abfc1dd02c151a5141bc22ac905a2cdb51fd343f6869328f9179fb2f720e1e6f
7
- data.tar.gz: 261c7c6e70daa001281b284edd136ea5551f55ac50173c5ab5c0bd3c471ca192ebf3e4db2c73a55474c49cfa1e1c5adc7b499b93db46902eced3d94b7d7fe2c4
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 !!!
@@ -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 "lazy_attached_#{name}"))
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 "lazy_attached_#{name}=", attachment)
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 #{(LazyBlobStorage.max_blob_size || LazyBlobStorage.default_max_blob_size) / 1_000} kb")
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("lazy_attached_#{name}_path") do
33
+ define_method("attached_#{name}_path") do
34
34
  if (send "#{name}_attached?")
35
- digest = (send "lazy_attached_#{name}").digest
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("lazy_attached_#{name}_url") do
42
+ define_method("attached_#{name}_url") do
43
43
  if (send "#{name}_attached?")
44
- digest = (send "lazy_attached_#{name}").digest
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 "lazy_attached_#{name}").present?
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 "lazy_attached_#{name}").presence
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 "lazy_attached_#{name}=", attachment)
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 "lazy_attached_#{name}".to_sym,
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 "with_lazy_attached_#{name}".to_sym, -> {
91
- includes("lazy_attached_#{name}")
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
@@ -19,4 +19,8 @@ class LazyAttachment < ActiveRecord::Base
19
19
  def url
20
20
  Rails.application.routes.url_helpers.lazy_blob_url(digest)
21
21
  end
22
+
23
+ def filename
24
+ LazyBlob.select(:id, :filename).find(lazy_blob_id).filename
25
+ end
22
26
  end
@@ -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.max_blob_size || LazyBlobStorage.default_max_blob_size }
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.max_blob_size || LazyBlobStorage.default_max_blob_size }
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
- create_table :lazy_blobs do |t|
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
- create_table :lazy_attachments do |t|
4
- t.references :lazy_blob
5
- t.references :record, polymorphic: true
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
@@ -1,3 +1,3 @@
1
1
  module LazyBlobStorage
2
- VERSION = "2.0.0-beta.3"
2
+ VERSION = "2.0.0-beta.5"
3
3
  end
@@ -6,4 +6,8 @@ module LazyBlobStorage
6
6
  def self.default_max_blob_size
7
7
  100_000.bytes
8
8
  end
9
+
10
+ def self._max_blob_size
11
+ max_blob_size || default_max_blob_size
12
+ end
9
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy_blob_storage
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.pre.beta.3
4
+ version: 2.0.0.pre.beta.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Corey Smedstad