active_storage_base64 0.1.1 → 0.1.2
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/.gitignore +1 -0
- data/.travis.yml +12 -0
- data/README.md +3 -34
- data/active_storage_base64.gemspec +2 -1
- data/config.reek +2 -2
- data/lib/active_storage_support/base64_attach.rb +9 -9
- data/lib/active_storage_support/support_for_base64.rb +8 -13
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f06ab3b77fc8bc3dd993d1313ad0136d2ab2bdf6a776a0dc7414c5af9fd75c3
|
4
|
+
data.tar.gz: 5f00568a8b09f02018bb6049203d4a02ffaca06ba7769ac04b8c001e0061fa2b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74e9c1008d1f9dad166133f44014ae715d779eca9d03d1177d4923de2d54db4389840756fac7aadcd09f87beb0eae8b0603f39f520660dc380313557f245107f
|
7
|
+
data.tar.gz: cdc6293595a7187c91b2543bc67893ba05b605506072686002e123f02ad47d671876399d16acfa26fe1bcea7dfd17c8723db895dac9f23198de5f8d20ea1dead
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -12,6 +12,18 @@ rvm:
|
|
12
12
|
|
13
13
|
sudo: false
|
14
14
|
|
15
|
+
env:
|
16
|
+
global:
|
17
|
+
- CC_TEST_REPORTER_ID=7196b4aa257fde33f24463218af32db6a6efd23d9148204822f757fa614a093e
|
18
|
+
|
19
|
+
before_script:
|
20
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
21
|
+
- chmod +x ./cc-test-reporter
|
22
|
+
- ./cc-test-reporter before-build
|
23
|
+
|
15
24
|
script:
|
16
25
|
- bundle exec rake code_analysis
|
17
26
|
- bundle exec rspec
|
27
|
+
|
28
|
+
after_script:
|
29
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
[](https://travis-ci.org/rootstrap/active-storage-base64)
|
2
2
|
[](https://codeclimate.com/github/rootstrap/active-storage-base64/maintainability)
|
3
|
+
[](https://codeclimate.com/github/rootstrap/active-storage-base64/test_coverage)
|
3
4
|
|
4
5
|
# ActiveStorageBase64
|
5
6
|
|
@@ -67,22 +68,7 @@ on your controller you could do something like this:
|
|
67
68
|
class UsersController < ApplicationController
|
68
69
|
def create
|
69
70
|
user = User.create(user_params)
|
70
|
-
|
71
|
-
|
72
|
-
private
|
73
|
-
|
74
|
-
def user_params
|
75
|
-
params.require(:user).permit(:avatar[data:], :username, :email)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
```
|
79
|
-
|
80
|
-
Or you could also do:
|
81
|
-
```ruby
|
82
|
-
class UsersController < ApplicationController
|
83
|
-
def create
|
84
|
-
user = User.create(user_params)
|
85
|
-
user.avatar.attach(params[:avatar])
|
71
|
+
user.avatar.attach(data: params[:avatar])
|
86
72
|
end
|
87
73
|
|
88
74
|
private
|
@@ -98,7 +84,7 @@ Here's another option to achieve the same:
|
|
98
84
|
class UsersController < ApplicationController
|
99
85
|
def create
|
100
86
|
user = User.create(user_params)
|
101
|
-
user.avatar = params[:avatar]
|
87
|
+
user.avatar = { data: params[:avatar] }
|
102
88
|
end
|
103
89
|
|
104
90
|
private
|
@@ -127,23 +113,6 @@ class UsersController < ApplicationController
|
|
127
113
|
end
|
128
114
|
end
|
129
115
|
```
|
130
|
-
Or, in case you want to have the avatar attached as soon as the user is created you can do:
|
131
|
-
```ruby
|
132
|
-
class UsersController < ApplicationController
|
133
|
-
def create
|
134
|
-
user = User.create(user_params)
|
135
|
-
end
|
136
|
-
|
137
|
-
private
|
138
|
-
|
139
|
-
def user_params
|
140
|
-
params.require(:user).permit(:username, :email, avatar:[:data,
|
141
|
-
:filename,
|
142
|
-
:content_type,
|
143
|
-
:identify])
|
144
|
-
end
|
145
|
-
end
|
146
|
-
```
|
147
116
|
|
148
117
|
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.
|
149
118
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'active_storage_base64'
|
3
|
-
s.version = '0.1.
|
3
|
+
s.version = '0.1.2'
|
4
4
|
s.summary = 'Base64 support for ActiveStorage'
|
5
5
|
s.description = s.summary
|
6
6
|
|
@@ -25,4 +25,5 @@ Gem::Specification.new do |s|
|
|
25
25
|
s.add_development_dependency 'rspec-rails', '~> 3.8.0'
|
26
26
|
s.add_development_dependency 'sqlite3', '1.3.13'
|
27
27
|
s.add_development_dependency 'pry-rails', '~> 0.3.6'
|
28
|
+
s.add_development_dependency 'simplecov'
|
28
29
|
end
|
data/config.reek
CHANGED
@@ -9,10 +9,10 @@ ClassVariable:
|
|
9
9
|
exclude: []
|
10
10
|
ControlParameter:
|
11
11
|
enabled: true
|
12
|
-
exclude: []
|
12
|
+
exclude: ['ActiveStorageSupport::SupportForBase64#add_helper_method']
|
13
13
|
DataClump:
|
14
14
|
enabled: true
|
15
|
-
exclude: []
|
15
|
+
exclude: ['ActiveStorageSupport::SupportForBase64']
|
16
16
|
max_copies: 2
|
17
17
|
min_clump_size: 2
|
18
18
|
DuplicateMethodCall:
|
@@ -4,17 +4,17 @@ module ActiveStorageSupport
|
|
4
4
|
module_function
|
5
5
|
|
6
6
|
def attachment_from_data(attachment)
|
7
|
-
if attachment.is_a?(Hash)
|
8
|
-
base64_data = attachment.delete(:data)
|
9
|
-
if base64_data.try(:is_a?, String) && base64_data =~ /^data:(.*?);(.*?),(.*)$/
|
10
|
-
|
11
|
-
attachment[:io] = StringIO.new(Base64.decode64(Regexp.last_match(3)))
|
12
|
-
attachment[:content_type] = Regexp.last_match(1) unless attachment[:content_type]
|
13
|
-
attachment[:filename] = Time.current.to_i.to_s unless attachment[:filename]
|
14
|
-
end
|
15
|
-
end
|
7
|
+
fill_attachment_data(attachment, attachment.delete(:data)) if attachment.is_a?(Hash)
|
16
8
|
|
17
9
|
attachment
|
18
10
|
end
|
11
|
+
|
12
|
+
def fill_attachment_data(attachment, base64_data)
|
13
|
+
return unless base64_data.try(:is_a?, String) && base64_data =~ /^data:(.*?);(.*?),(.*)$/
|
14
|
+
|
15
|
+
attachment[:io] = StringIO.new(Base64.decode64(Regexp.last_match(3)))
|
16
|
+
attachment[:content_type] ||= Regexp.last_match(1)
|
17
|
+
attachment[:filename] ||= Time.current.to_i.to_s
|
18
|
+
end
|
19
19
|
end
|
20
20
|
end
|
@@ -8,29 +8,24 @@ module ActiveStorageSupport
|
|
8
8
|
def has_one_base64_attached(name, dependent: :purge_later)
|
9
9
|
has_one_attached name, dependent: dependent
|
10
10
|
|
11
|
-
|
12
|
-
def #{name}
|
13
|
-
@active_storage_attached_#{name} ||=
|
14
|
-
ActiveStorageSupport::Base64One.new("#{name}", self, dependent: #{dependent == :purge_later ? ':purge_later' : 'false'})
|
15
|
-
end
|
16
|
-
|
17
|
-
def #{name}=(data)
|
18
|
-
#{name}.attach(data)
|
19
|
-
end
|
20
|
-
CODE
|
11
|
+
add_helper_method(ActiveStorageSupport::Base64One, name, dependent: dependent)
|
21
12
|
end
|
22
13
|
|
23
14
|
def has_many_base64_attached(name, dependent: :purge_later)
|
24
15
|
has_many_attached name, dependent: dependent
|
25
16
|
|
17
|
+
add_helper_method(ActiveStorageSupport::Base64Many, name, dependent: dependent)
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_helper_method(type, name, dependent:)
|
26
21
|
class_eval <<-CODE, __FILE__, __LINE__ + 1
|
27
22
|
def #{name}
|
28
23
|
@active_storage_attached_#{name} ||=
|
29
|
-
|
24
|
+
#{type}.new("#{name}", self, dependent: #{dependent == :purge_later ? ':purge_later' : 'false'})
|
30
25
|
end
|
31
26
|
|
32
|
-
def #{name}=(
|
33
|
-
#{name}.attach(
|
27
|
+
def #{name}=(data)
|
28
|
+
#{name}.attach(data)
|
34
29
|
end
|
35
30
|
CODE
|
36
31
|
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
|
+
version: 0.1.2
|
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-01-
|
11
|
+
date: 2019-01-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -100,6 +100,20 @@ dependencies:
|
|
100
100
|
- - "~>"
|
101
101
|
- !ruby/object:Gem::Version
|
102
102
|
version: 0.3.6
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: simplecov
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
103
117
|
description: Base64 support for ActiveStorage
|
104
118
|
email: ricardo@rootstrap.com
|
105
119
|
executables: []
|