readymade 0.2.0 → 0.2.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf49169463a122cc9e591dbf0bfa5933c9cac537368da20cc4917b29751658cc
4
- data.tar.gz: 77a1e8ada0ef9dcbf71e646ba50dbfadf88b5d2ea7ab5fc5a0a4facd555748f1
3
+ metadata.gz: 1a6e8e90fddf55567acfe262c31551b786d880250075f4cd9c5c651cc4838dfe
4
+ data.tar.gz: e2349c3664e83acbeb0321d822ae80ddab1ad3a8ec8f97377cc0b2a2117ede68
5
5
  SHA512:
6
- metadata.gz: 9d1d70fabd566948f6f0993aed6a4446a1259810a53e826809fe752cccfa08cd9654619bbae7316c6a427db23815edc3703c56f727c53808f91f8b0326fc19fa
7
- data.tar.gz: 84aa64e32f1e6465a24734947777507884b6227d2d3e6054b24781f01e609c8a4a7eb02fcc6c59837d8138947f0f6318f6b74e4576d7b37068a3f75351a65273
6
+ metadata.gz: e71191a5f142db70281225f3593f26b3f1e063c5a700ee029db55ca99486ed5e734490da0bd9ed360abb0544f8d4d567949c10f8baef33653a6497934dc99320
7
+ data.tar.gz: e1c077924ee5fa51a9e2e2ce9f7e520fcf75dda30c3a1400afc938154366abb587b8a0fda092d4dfb283d576a66b90ea34ee7823d21e4a8c880fd09f08c898ef
data/CHANGELOG.md CHANGED
@@ -1,6 +1,24 @@
1
1
  Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## [0.2.4] - 2022-05-12
5
+
6
+ ### Fixes
7
+
8
+ * Fix ApiAttachable empty attachments for non hash assignement
9
+
10
+ ## [0.2.3] - 2022-05-03
11
+
12
+ ### Features
13
+
14
+ * Add `Readymade::Model::ApiAttachable` - add base64 format to your ActiveStorage
15
+
16
+ ## [0.2.1] - 2022-04-21
17
+
18
+ ### Improvements
19
+
20
+ * Update collection_response controller helper
21
+
4
22
  ## [0.2.0] - 2022-04-13
5
23
 
6
24
  ### Improvements
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- readymade (0.2.0)
4
+ readymade (0.2.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,12 +1,14 @@
1
- # Readymade 0.1.8
1
+ # Readymade
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/readymade.png)](https://badge.fury.io/rb/readymade)
2
4
 
3
5
  This gems contains basic components to follow [ABDI architecture](https://github.com/OrestF/OrestF/blob/master/abdi/ABDI_architecture.md)
4
6
 
5
7
  ### Tested with ruby:
6
8
 
7
- - 2.7
8
- - 3.0
9
9
  - 3.1
10
+ - 3.0
11
+ - 2.7
10
12
 
11
13
  ## Installation
12
14
 
@@ -38,7 +40,7 @@ Inherit your components from:
38
40
  ```ruby
39
41
  response = Readymade::Response.new(:success, my_data: data)
40
42
  response.success? # true
41
- response =Readymade::Response.new(:fail, errors: errors)
43
+ response = Readymade::Response.new(:fail, errors: errors)
42
44
  response.success? # false
43
45
  response.fail? # true
44
46
  response.status # 'fail'
@@ -149,6 +151,49 @@ class Orders::Operations::Create < Readymade::Operation
149
151
  end
150
152
  ```
151
153
 
154
+ ### Readymade::Controller::Serialization
155
+
156
+ ```ruby
157
+ class MyController < ApplicationController
158
+ include Readymade::Controller::Serialization
159
+ end
160
+ ```
161
+
162
+ Serialization helpers for controllers.
163
+ Dependencies that must be installed on your own:
164
+ - [blueprinter](https://rubygems.org/gems/blueprinter/)
165
+ - [pagy](https://rubygems.org/gems/pagy)
166
+ - [api-pagination](https://rubygems.org/gems/api-pagination)
167
+
168
+ ### Readymade::Model::ApiAttachable
169
+
170
+ Add base64 attachments format for your models
171
+
172
+ ```ruby
173
+ class User < ApplicationRecord
174
+ has_one_attached :avatar
175
+ has_many_attached :images
176
+ include Readymade::Model::ApiAttachable
177
+ # must be included after has_one_attached, has_many_attached declaration
178
+ # api_file = {
179
+ # base64: 'iVBORw0KGgoAAA....',
180
+ # filename: 'my_avatar.png'
181
+ # }
182
+ # record.avatar = api_file 🎉
183
+ end
184
+ ```
185
+
186
+ copy [spec/support/api_attachable.rb](./spec/support/api_attachable.rb)
187
+ ```ruby
188
+ def to_api_file(file)
189
+ { base64: Base64.encode64(file.read), filename: file.original_filename }
190
+ end
191
+ ```
192
+ ```ruby
193
+ # rspec example
194
+ let(:avatar) { Rack::Test::UploadedFile.new(Rails.root.join('spec/support/assets/test-image.png'), 'image/png') }
195
+ let(:params) { { user: attributes_for(:user).merge!(avatar: to_api_file(avatar)) } }
196
+ ```
152
197
 
153
198
  ## Development
154
199
 
@@ -25,7 +25,7 @@ module Readymade
25
25
 
26
26
  render_json(
27
27
  {
28
- items: serialize_collection(paginate(collection, options), options),
28
+ (options.delete(:root).presence || :items) => serialize_collection(paginate(collection, options), options),
29
29
  count: collection.count
30
30
  },
31
31
  options[:status] || :ok
@@ -0,0 +1,61 @@
1
+ require 'active_support/concern'
2
+
3
+ module Readymade
4
+ module Model
5
+ module ApiAttachable
6
+ extend ActiveSupport::Concern
7
+ # must be included after has_one_attached, has_many_attached declaration
8
+ # api_file = {
9
+ # base64: 'iVBORw0KGgoAAA....',
10
+ # filename: 'my_avatar.png'
11
+ # }
12
+ # record.avatar = api_file 🎉
13
+
14
+ included do
15
+ has_one_attached_reflections.map(&:name).each do |attachment_method_name|
16
+ define_method("#{attachment_method_name}=") do |attachment_file|
17
+ attachment_file = api_attachment_to_uploaded(attachment_file) if api_attachable_format?(attachment_file)
18
+ super(attachment_file)
19
+ end
20
+ end
21
+
22
+ has_many_attached_reflections.map(&:name).each do |attachment_method_name|
23
+ define_method("#{attachment_method_name}=") do |attachment_file|
24
+ attachment_file = Array.wrap(attachment_file).map do |af|
25
+ api_attachable_format?(af) ? api_attachment_to_uploaded(af) : af
26
+ end
27
+ super(attachment_file)
28
+ end
29
+ end
30
+ end
31
+
32
+ class_methods do
33
+ # rubocop:disable Naming/PredicateName
34
+ def has_one_attached_reflections
35
+ reflect_on_all_attachments.filter { |association| association.instance_of?(ActiveStorage::Reflection::HasOneAttachedReflection) }
36
+ end
37
+
38
+ def has_many_attached_reflections
39
+ reflect_on_all_attachments.filter { |association| association.instance_of?(ActiveStorage::Reflection::HasManyAttachedReflection) }
40
+ end
41
+ # rubocop:enable Naming/PredicateName
42
+ end
43
+
44
+ def api_attachable_format?(attachment_file)
45
+ attachment_file.respond_to?(:key?) && attachment_file.key?('base64') && attachment_file.key?('filename')
46
+ end
47
+
48
+ def api_attachment_to_uploaded(attachment_file)
49
+ ActionDispatch::Http::UploadedFile.new(
50
+ tempfile: Tempfile.new(attachment_file[:filename]).tap do |tf|
51
+ tf.binmode
52
+ tf.write(Base64.decode64(attachment_file[:base64]))
53
+ end,
54
+ filename: attachment_file[:filename],
55
+ type: Mime::Type.lookup_by_extension(File.extname(attachment_file[:filename])[1..]).to_s
56
+ )
57
+ end
58
+ end
59
+ end
60
+ end
61
+
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Readymade
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.4'
5
5
  end
data/lib/readymade.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'readymade/model/api_attachable'
3
4
  require 'readymade/controller/serialization'
4
5
  require 'readymade/action'
5
6
  require 'readymade/form'
data/readymade.gemspec CHANGED
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
26
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
27
  end
28
+ spec.files << 'lib/readymade/model/api_attachable.rb'
28
29
  spec.files << 'lib/readymade/controller/serialization.rb'
29
30
  spec.bindir = 'exe'
30
31
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: readymade
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - OrestF
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-13 00:00:00.000000000 Z
11
+ date: 2022-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug
@@ -76,6 +76,7 @@ files:
76
76
  - lib/readymade/controller/serialization.rb
77
77
  - lib/readymade/form.rb
78
78
  - lib/readymade/instant_form.rb
79
+ - lib/readymade/model/api_attachable.rb
79
80
  - lib/readymade/operation.rb
80
81
  - lib/readymade/response.rb
81
82
  - lib/readymade/version.rb