readymade 0.2.3 → 0.2.6

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: f26bb52b5176c6325c252ac67735fbd6f21f26ad194d0dbdc9e915e421421840
4
- data.tar.gz: b0500fc448b0681c6e436ad2f22fd2f43d880542500042a7bb9853a69d58fdd0
3
+ metadata.gz: 9f32e2e65f330e17be52ef81b4b998905a3ebf586026ad53fd93c4d6349f66ad
4
+ data.tar.gz: 67b118d934e376684a0e9a7b76116a5f71f070a13fe6ae4a736cf1c0b5852388
5
5
  SHA512:
6
- metadata.gz: 93cde55d0896d28b379fa699846abed6e3d8e72d2dbae8f7027b2813cc4a5fa277d8b1c07f8d0fe1905890cbf0b2fa654caeed7a8b791ff4ada4ac995d0edfd9
7
- data.tar.gz: b1928db06558f7d28c83cda0827f065f3e8278567647844ab9f768fdf70183b2b1126e6fc016cb93df0d36f258152713372c0f8ef899bfbb026b1d11e35c327f
6
+ metadata.gz: ad656ea48e81585d91232caa3a8f107fb9eaa06174c3df6cef346e66c4efb0986dce0b0729b10facb4d1954c907e04a501ab48ae910c367af84026443ed5d712
7
+ data.tar.gz: bd994daea95faa0a0354ad7483cf92c33ee0915a6a85f3e56fd698c64ee5919c059d5a63ab49f6da2a2a2e90fb306b3c1aa7343f38af3884c332c53baa115ba8
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.6] - 2022-05-24
5
+
6
+ ### Features
7
+
8
+ * Add `Readymade::Model::Filterable` - model concern for scopes filtering
9
+
10
+ ## [0.2.5] - 2022-05-19
11
+
12
+ ### Improvements
13
+
14
+ * Form#required_attributes returns `[]` if `params[:_destroy]` present
15
+
16
+ ## [0.2.4] - 2022-05-12
17
+
18
+ ### Fixes
19
+
20
+ * Fix ApiAttachable empty attachments for non hash assignement
21
+
4
22
  ## [0.2.3] - 2022-05-03
5
23
 
6
24
  ### Features
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- readymade (0.2.3)
4
+ readymade (0.2.6)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -195,6 +195,21 @@ let(:avatar) { Rack::Test::UploadedFile.new(Rails.root.join('spec/support/assets
195
195
  let(:params) { { user: attributes_for(:user).merge!(avatar: to_api_file(avatar)) } }
196
196
  ```
197
197
 
198
+ ### Readymade::Model::Filterable
199
+
200
+ ```ruby
201
+ class User < ApplicationRecord
202
+ include Readyamde::Model::Filterable
203
+
204
+ scope :by_status, ->(status) { where(status: status) }
205
+ scope :by_role, ->(role) { where(role: role) }
206
+ end
207
+ ```
208
+
209
+ ```ruby
210
+ User.all.filter_collection({ by_status: 'active', by_role: 'manager' })
211
+ ```
212
+
198
213
  ## Development
199
214
 
200
215
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -45,6 +45,8 @@ module Readymade
45
45
  end
46
46
 
47
47
  def required_attributes
48
+ return [] if params.try(:[], :_destroy).present?
49
+
48
50
  @required_attributes ||= self.class::REQUIRED_ATTRIBUTES
49
51
  end
50
52
 
@@ -79,9 +81,17 @@ module Readymade
79
81
 
80
82
  # copy errors from nested forms into parent form
81
83
  def sync_nested_errors(nested_forms)
82
- nested_forms.each do |n_form|
83
- n_form.errors.each do |code, text|
84
- errors.add("#{n_form.humanized_name}.#{code}", text)
84
+ if rails_errors_v2?
85
+ nested_forms.each do |n_form|
86
+ n_form.errors.each do |code|
87
+ errors.add("#{n_form.humanized_name}.#{code.attribute}", code.message)
88
+ end
89
+ end
90
+ else
91
+ nested_forms.each do |n_form|
92
+ n_form.errors.each do |code, text|
93
+ errors.add("#{n_form.humanized_name}.#{code}", text)
94
+ end
85
95
  end
86
96
  end
87
97
 
@@ -92,7 +102,7 @@ module Readymade
92
102
  def sync_errors(from: self, to: record)
93
103
  return if [from, to].any?(&:blank?)
94
104
 
95
- if Rails.version.to_f > 6.0
105
+ if rails_errors_v2?
96
106
  from.errors.messages.each do |key, values|
97
107
  Array.wrap(values).uniq.each do |uv|
98
108
  to.errors.add(key, uv)
@@ -145,6 +155,12 @@ module Readymade
145
155
  {}
146
156
  end
147
157
 
158
+ private
159
+
160
+ def rails_errors_v2?
161
+ Rails.version.to_f > 6.0
162
+ end
163
+
148
164
  # EXAMPLE
149
165
  # class Items::Forms::Create::Value < ::Readymade::Form
150
166
  # PERMITTED_ATTRIBUTES = %i[vat_percent price_type item_category].freeze
@@ -22,7 +22,7 @@ module Readymade
22
22
  has_many_attached_reflections.map(&:name).each do |attachment_method_name|
23
23
  define_method("#{attachment_method_name}=") do |attachment_file|
24
24
  attachment_file = Array.wrap(attachment_file).map do |af|
25
- api_attachment_to_uploaded(af) if api_attachable_format?(af)
25
+ api_attachable_format?(af) ? api_attachment_to_uploaded(af) : af
26
26
  end
27
27
  super(attachment_file)
28
28
  end
@@ -0,0 +1,34 @@
1
+ require 'active_support/concern'
2
+
3
+ module Readymade
4
+ module Model
5
+ module Filterable
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ def send_chain(methods, scope = self)
10
+ return scope if methods.blank?
11
+
12
+ if methods.respond_to?(:keys)
13
+ methods.inject(scope) do |obj, (method, value)|
14
+ obj.send(method, value)
15
+ end
16
+ else
17
+ methods.inject(scope) do |obj, method|
18
+ obj.send(method)
19
+ end
20
+ end
21
+ end
22
+
23
+ def filter_collection(filtering_params)
24
+ filtering_params.permit! if filtering_params.respond_to?(:permit)
25
+
26
+ regular_params = filtering_params.select { |_key, value| value.present? }.to_h
27
+ custom_params = filtering_params.to_h.select { |_key, value| value.is_a?(String) && value.start_with?('without_') }.values
28
+
29
+ send_chain(regular_params, send_chain(custom_params)).distinct
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Readymade
4
- VERSION = '0.2.3'
4
+ VERSION = '0.2.6'
5
5
  end
data/lib/readymade.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'readymade/model/api_attachable'
4
+ require 'readymade/model/filterable'
4
5
  require 'readymade/controller/serialization'
5
6
  require 'readymade/action'
6
7
  require 'readymade/form'
data/readymade.gemspec CHANGED
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
26
26
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
27
  end
28
28
  spec.files << 'lib/readymade/model/api_attachable.rb'
29
+ spec.files << 'lib/readymade/model/filterable.rb'
29
30
  spec.files << 'lib/readymade/controller/serialization.rb'
30
31
  spec.bindir = 'exe'
31
32
  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.3
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - OrestF
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-05-03 00:00:00.000000000 Z
11
+ date: 2022-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug
@@ -77,6 +77,7 @@ files:
77
77
  - lib/readymade/form.rb
78
78
  - lib/readymade/instant_form.rb
79
79
  - lib/readymade/model/api_attachable.rb
80
+ - lib/readymade/model/filterable.rb
80
81
  - lib/readymade/operation.rb
81
82
  - lib/readymade/response.rb
82
83
  - lib/readymade/version.rb