readymade 0.1.8 → 0.2.3

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: a4fa8901d76050e8ef3597f3e079ebbfa6bcd565dbc05a22dc3578616da556b1
4
- data.tar.gz: e52a10d9513e21c61501305edae074bd7e80aa00a993f4f3bba8b266d1e420c1
3
+ metadata.gz: f26bb52b5176c6325c252ac67735fbd6f21f26ad194d0dbdc9e915e421421840
4
+ data.tar.gz: b0500fc448b0681c6e436ad2f22fd2f43d880542500042a7bb9853a69d58fdd0
5
5
  SHA512:
6
- metadata.gz: b5fca515c5b236aa72f49f84326d4e6b02e50d7af50636e8fd79774fb5d246d42a7f82341864729f25051dbf24f9d937c72ef7ae9a76ab794ecca6e0b61eeb3c
7
- data.tar.gz: 671f7bdf6ba9362306e790bc6ed02d7c08d3007110d6f256072b4efd5cca0f88719a080489fe023f1e60c3d7ae7de050da252637d2a638fa28665e2f337d7b58
6
+ metadata.gz: 93cde55d0896d28b379fa699846abed6e3d8e72d2dbae8f7027b2813cc4a5fa277d8b1c07f8d0fe1905890cbf0b2fa654caeed7a8b791ff4ada4ac995d0edfd9
7
+ data.tar.gz: b1928db06558f7d28c83cda0827f065f3e8278567647844ab9f768fdf70183b2b1126e6fc016cb93df0d36f258152713372c0f8ef899bfbb026b1d11e35c327f
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.3] - 2022-05-03
5
+
6
+ ### Features
7
+
8
+ * Add `Readymade::Model::ApiAttachable` - add base64 format to your ActiveStorage
9
+
10
+ ## [0.2.1] - 2022-04-21
11
+
12
+ ### Improvements
13
+
14
+ * Update collection_response controller helper
15
+
16
+ ## [0.2.0] - 2022-04-13
17
+
18
+ ### Improvements
19
+
20
+ * Add support for ruby 3.0, 3.1
21
+
4
22
  ## [0.1.8] - 2022-03-30
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.1.7)
4
+ readymade (0.2.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,7 +1,15 @@
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
 
7
+ ### Tested with ruby:
8
+
9
+ - 3.1
10
+ - 3.0
11
+ - 2.7
12
+
5
13
  ## Installation
6
14
 
7
15
  Add this line to your application's Gemfile:
@@ -32,7 +40,7 @@ Inherit your components from:
32
40
  ```ruby
33
41
  response = Readymade::Response.new(:success, my_data: data)
34
42
  response.success? # true
35
- response =Readymade::Response.new(:fail, errors: errors)
43
+ response = Readymade::Response.new(:fail, errors: errors)
36
44
  response.success? # false
37
45
  response.fail? # true
38
46
  response.status # 'fail'
@@ -143,6 +151,49 @@ class Orders::Operations::Create < Readymade::Operation
143
151
  end
144
152
  ```
145
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
+ ```
146
197
 
147
198
  ## Development
148
199
 
@@ -12,11 +12,10 @@ module Readymade
12
12
 
13
13
  attr_reader :args, :data
14
14
 
15
- def initialize(**args)
15
+ def initialize(args = {})
16
16
  raise NonKeywordArgumentsError if args.present? && !args.is_a?(Hash)
17
17
 
18
18
  @args = @data = args
19
-
20
19
  @args.each do |name, value|
21
20
  instance_variable_set("@#{name}", value)
22
21
  end
@@ -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
@@ -177,7 +177,7 @@ module Readymade
177
177
  end
178
178
 
179
179
  def to_h
180
- raise Readymade::Error.new('define form_options on your form') unless (f = @f_class.new({}, @args)).respond_to?(:form_options)
180
+ raise Readymade::Error.new('define form_options on your form') unless (f = @f_class.new({}, **@args)).respond_to?(:form_options)
181
181
 
182
182
  f.form_options
183
183
  end
@@ -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_attachment_to_uploaded(af) if api_attachable_format?(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.1.8'
4
+ VERSION = '0.2.3'
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.1.8
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - OrestF
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-30 00:00:00.000000000 Z
11
+ date: 2022-05-03 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
@@ -102,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
103
  - !ruby/object:Gem::Version
103
104
  version: '0'
104
105
  requirements: []
105
- rubygems_version: 3.1.6
106
+ rubygems_version: 3.3.7
106
107
  signing_key:
107
108
  specification_version: 4
108
109
  summary: Set of base classes for ABDI architecture