active_storage_base64 0.1.4 → 1.0.0

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
- SHA256:
3
- metadata.gz: a89024b9798e7c3adfef588638e62623c6891eb52ad0d6ba2aebfa1a603dd6ce
4
- data.tar.gz: 405e6a9a85a3b3755e06e8f4b958d5731d0558b73bc41ff71dc048dcd5b630c3
2
+ SHA1:
3
+ metadata.gz: 44449a327924040d5967252b57bcc967f07b568e
4
+ data.tar.gz: cb42213139e6db2b629104f72e392965e6e659d3
5
5
  SHA512:
6
- metadata.gz: 39d2cbb297b0e4cabc48543917b1c2c4ec7c5f1b12578f384cebd8ea1824a369a0561aac0ead6e72a8f4e28669e56c053d2dc59f242bf63d9bf526be7c7f49a6
7
- data.tar.gz: d1910ba15650555ece8837356b2c580a22a214f7357918422eaf38d6bdfebb88b8e4d452250d51bfab7be8f3b2fb4e141f45c625988dd2afd09491b059536b72
6
+ metadata.gz: 24750064b2e4a13f316b732fb6423ce0b8fb7f7d10cf862b7e0bab7aafa584096fb930c5d1900160511063a240613cc3eca4f3e1fbbe180ef53db092f7ab955d
7
+ data.tar.gz: 80cdc756a28d7014a278e503696d1f5276f90faf14a499a4937759308c5ad0c566cb2b56e244564dff78ed09e5e44ec706e0a4ab5e8216f941dc9b7c62de5060
data/.rubocop.yml CHANGED
@@ -19,6 +19,8 @@ Metrics/BlockLength:
19
19
  Exclude:
20
20
  - config/**/*
21
21
  - spec/**/*
22
+ ExcludedMethods:
23
+ - class_methods
22
24
 
23
25
  Metrics/BlockNesting:
24
26
  Max: 4
data/.travis.yml CHANGED
@@ -4,12 +4,14 @@ before_install:
4
4
  - gem install bundler -v "~> 1.17"
5
5
 
6
6
  rvm:
7
- - 2.2.8
8
- - 2.3.5
9
- - 2.4.2
10
7
  - 2.5.0
8
+ - 2.6.0
11
9
  - ruby-head
12
10
 
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: ruby-head
14
+
13
15
  sudo: false
14
16
 
15
17
  env:
data/README.md CHANGED
@@ -4,26 +4,22 @@
4
4
 
5
5
  # ActiveStorageBase64
6
6
 
7
- Gem used to add support for base64 images for Rails's ActiveStorage.
7
+ Adds support for base64 attachments to ActiveStorage.
8
8
 
9
- ## Getting Started
9
+ ## Installation
10
10
 
11
11
  In order to get the gem working on your project you just need to add the gem to your project like this:
12
12
  ```ruby
13
13
  gem 'active_storage_base64'
14
14
  ```
15
15
 
16
- ### Prerequisites
16
+ ## Prerequisites
17
17
 
18
- The only two prerequisites for using this gem are having Rails version 5.2.0 or higher installed on your project and having ActiveStorage setup properly (for more information on how to do this, check this [Active Storage Overview](https://edgeguides.rubyonrails.org/active_storage_overview.html))
18
+ The only prerequisites for using this gem are having Rails version 5.2.0 or higher installed on your project and having ActiveStorage properly set up (for more information on how to do this, check [Active Storage Overview](https://edgeguides.rubyonrails.org/active_storage_overview.html))
19
19
 
20
- ```ruby
21
- gem 'rails', '5.2.0'
22
- ```
20
+ ## Usage
23
21
 
24
- ### Installing
25
-
26
- In order to use the gem's functionality, you'll need to include the module into your ActiveRecord inheriting class.
22
+ In order to use the gem's functionality, you need to include the `ActiveStorageSupport::SupportForBase64` module in your ActiveRecord models.
27
23
  For example:
28
24
  ```ruby
29
25
  class User < ActiveRecord::Base
@@ -32,7 +28,7 @@ end
32
28
  ```
33
29
 
34
30
  Note:
35
- We highly recomment using an alternative class that inherits from ActiveRecord and includes the module so instead of including the module for each of your classes, you make them inherit from this new class, check below:
31
+ We highly recommend using an alternative class that inherits from `ActiveRecord::Base` and includes the module so instead of including the module for each of your classes, you make them inherit from this new class, check below:
36
32
  ```ruby
37
33
  class ApplicationRecord < ActiveRecord::Base
38
34
  include ActiveStorageSupport::SupportForBase64
@@ -43,7 +39,7 @@ class User < ApplicationRecord
43
39
  end
44
40
  ```
45
41
 
46
- After you have the module included in your class you'll be able to use the following two helper methods for working with base64 files:
42
+ After you have the module included in your class you'll be able to use the following two helper methods to work with base64 files:
47
43
  When you need a single image attached:
48
44
  ```ruby
49
45
  has_one_base64_attached
@@ -54,7 +50,7 @@ has_many_base64_attached
54
50
  ```
55
51
  These helpers will work just like the `has_one_attached` and `has_many_attached` helper methods from ActiveStorage.
56
52
 
57
- A working example for this, assuming we have a model `User` with only an `avatar` attached would be:
53
+ A working example for this, assuming we have a model `User` with an `avatar` attached would be:
58
54
  ```ruby
59
55
  class User < ActiveRecord::Base
60
56
  include ActiveStorageSupport::SupportForBase64
@@ -100,6 +96,7 @@ class UsersController < ApplicationController
100
96
  def create
101
97
  user = User.create(user_params)
102
98
  user.avatar = { data: params[:avatar] }
99
+ user.save
103
100
  end
104
101
 
105
102
  private
@@ -110,9 +107,9 @@ class UsersController < ApplicationController
110
107
  end
111
108
  ```
112
109
 
113
- ## Specifying a filename or content_type
110
+ ### Specifying a filename or content type
114
111
 
115
- If you are willing to add a specific filename to your attachment, or send in a specific content_type for your file, you can use `data:` to attach the base64 data and specify your `filename:`, `content_type:` and/or `identify:` hash keys.
112
+ If you are willing to add a specific filename to your attachment, or send in a specific content type for your file, you can use `data:` to attach the base64 data and specify your `filename:`, `content_type:` and/or `identify:` hash keys.
116
113
  Check the following example:
117
114
  ```ruby
118
115
  class UsersController < ApplicationController
@@ -147,7 +144,13 @@ class UsersController < ApplicationController
147
144
  end
148
145
  ```
149
146
 
150
- For more information on how to work with ActiveStorage, please check the [Active Storage Overview](https://edgeguides.rubyonrails.org/active_storage_overview.html) mentioned above, all points in there apply to this gem as well.
147
+ ### Data Format
148
+
149
+ To attach base64 data it is required to come in the form of [Data URIs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs) .
150
+ For example:
151
+ ```
152
+ data:image/png;base64,[base64 data]
153
+ ```
151
154
 
152
155
  ## Contributing
153
156
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'active_storage_base64'
3
- s.version = '0.1.4'
3
+ s.version = '1.0.0'
4
4
  s.summary = 'Base64 support for ActiveStorage'
5
5
  s.description = s.summary
6
6
 
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.required_ruby_version = '>= 2.2.2'
18
18
 
19
19
  # Dependencies
20
- s.add_dependency 'rails', '~> 5.2'
20
+ s.add_dependency 'rails', '~> 6.0'
21
21
 
22
22
  # Development dependencies
23
23
  s.add_development_dependency 'pry-rails', '~> 0.3.6'
@@ -25,5 +25,5 @@ Gem::Specification.new do |s|
25
25
  s.add_development_dependency 'rspec-rails', '~> 3.8.0'
26
26
  s.add_development_dependency 'rubocop', '~> 0.56.0'
27
27
  s.add_development_dependency 'simplecov'
28
- s.add_development_dependency 'sqlite3', '1.3.13'
28
+ s.add_development_dependency 'sqlite3', '1.4.1'
29
29
  end
data/config.reek CHANGED
@@ -9,10 +9,8 @@ ClassVariable:
9
9
  exclude: []
10
10
  ControlParameter:
11
11
  enabled: true
12
- exclude: ['ActiveStorageSupport::SupportForBase64#add_helper_method']
13
12
  DataClump:
14
13
  enabled: true
15
- exclude: ['ActiveStorageSupport::SupportForBase64']
16
14
  max_copies: 2
17
15
  min_clump_size: 2
18
16
  DuplicateMethodCall:
@@ -1,12 +1,14 @@
1
1
  module ActiveStorageSupport
2
2
  class Base64Many < ActiveStorage::Attached::Many
3
3
  def attach(*attachables)
4
- super base64_attachments(attachables)
4
+ super self.class.from_base64(attachables)
5
5
  end
6
6
 
7
- def base64_attachments(attachments)
8
- attachments.flatten.map do |attachment|
9
- ActiveStorageSupport::Base64Attach.attachment_from_data(attachment)
7
+ def self.from_base64(attachables)
8
+ attachables = [attachables] unless attachables.is_a?(Array)
9
+
10
+ attachables.flatten.map do |attachable|
11
+ ActiveStorageSupport::Base64Attach.attachment_from_data(attachable)
10
12
  end
11
13
  end
12
14
  end
@@ -1,8 +1,11 @@
1
1
  module ActiveStorageSupport
2
2
  class Base64One < ActiveStorage::Attached::One
3
3
  def attach(attachable)
4
- attachment = ActiveStorageSupport::Base64Attach.attachment_from_data(attachable)
5
- super attachment
4
+ super self.class.from_base64(attachable)
5
+ end
6
+
7
+ def self.from_base64(attachable)
8
+ ActiveStorageSupport::Base64Attach.attachment_from_data(attachable)
6
9
  end
7
10
  end
8
11
  end
@@ -8,24 +8,48 @@ module ActiveStorageSupport
8
8
  def has_one_base64_attached(name, dependent: :purge_later)
9
9
  has_one_attached name, dependent: dependent
10
10
 
11
- add_helper_method(ActiveStorageSupport::Base64One, name, dependent: dependent)
11
+ generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
12
+ def #{name}
13
+ @active_storage_attached_#{name} ||= ActiveStorageSupport::Base64One.new("#{name}", self)
14
+ end
15
+ def #{name}=(attachable)
16
+ attachment_changes["#{name}"] =
17
+ if attachable.nil?
18
+ ActiveStorage::Attached::Changes::DeleteOne.new("#{name}", self)
19
+ else
20
+ ActiveStorage::Attached::Changes::CreateOne.new(
21
+ "#{name}", self, ActiveStorageSupport::Base64One.from_base64(attachable)
22
+ )
23
+ end
24
+ end
25
+ CODE
12
26
  end
13
27
 
14
28
  def has_many_base64_attached(name, dependent: :purge_later)
15
29
  has_many_attached name, dependent: dependent
16
30
 
17
- add_helper_method(ActiveStorageSupport::Base64Many, name, dependent: dependent)
18
- end
19
-
20
- def add_helper_method(type, name, dependent:)
21
- class_eval <<-CODE, __FILE__, __LINE__ + 1
31
+ generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
22
32
  def #{name}
23
- @active_storage_attached_#{name} ||=
24
- #{type}.new("#{name}", self, dependent: #{dependent == :purge_later ? ':purge_later' : 'false'})
33
+ @active_storage_attached_#{name} ||= ActiveStorageSupport::Base64Many.new("#{name}", self)
25
34
  end
26
-
27
- def #{name}=(data)
28
- #{name}.attach(data)
35
+ def #{name}=(attachables)
36
+ if ActiveStorage.replace_on_assign_to_many
37
+ attachment_changes["#{name}"] =
38
+ if Array(attachables).none?
39
+ ActiveStorage::Attached::Changes::DeleteMany.new("#{name}", self)
40
+ else
41
+ ActiveStorage::Attached::Changes::CreateMany.new(
42
+ "#{name}", self, ActiveStorageSupport::Base64Many.from_base64(attachables)
43
+ )
44
+ end
45
+ else
46
+ if Array(attachables).any?
47
+ attachment_changes["#{name}"] =
48
+ ActiveStorage::Attached::Changes::CreateMany.new(
49
+ "#{name}", self, #{name}.blobs + ActiveStorageSupport::Base64Many.from_base64(attachables)
50
+ )
51
+ end
52
+ end
29
53
  end
30
54
  CODE
31
55
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_storage_base64
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricardo Cortio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-08 00:00:00.000000000 Z
11
+ date: 2019-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '5.2'
19
+ version: '6.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '5.2'
26
+ version: '6.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: pry-rails
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -100,14 +100,14 @@ dependencies:
100
100
  requirements:
101
101
  - - '='
102
102
  - !ruby/object:Gem::Version
103
- version: 1.3.13
103
+ version: 1.4.1
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - '='
109
109
  - !ruby/object:Gem::Version
110
- version: 1.3.13
110
+ version: 1.4.1
111
111
  description: Base64 support for ActiveStorage
112
112
  email: ricardo@rootstrap.com
113
113
  executables: []
@@ -152,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
152
  version: '0'
153
153
  requirements: []
154
154
  rubyforge_project:
155
- rubygems_version: 2.7.6
155
+ rubygems_version: 2.5.2.3
156
156
  signing_key:
157
157
  specification_version: 4
158
158
  summary: Base64 support for ActiveStorage