readymade 0.2.5 → 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: d8b0938bb39253068f0b78cd62841b08aa118f9ae51fb8e8e49c59a47cc5d6dc
4
- data.tar.gz: 59fa97d3a17ab1b7330a1af92399a09aceb8843b81f1adb4f667580e495685f1
3
+ metadata.gz: 9f32e2e65f330e17be52ef81b4b998905a3ebf586026ad53fd93c4d6349f66ad
4
+ data.tar.gz: 67b118d934e376684a0e9a7b76116a5f71f070a13fe6ae4a736cf1c0b5852388
5
5
  SHA512:
6
- metadata.gz: 5496ed0f230582f69b423e15561ba247d5967b44242fe3baa0611f4056e207ab8752dd2360380dc20a3072fea94ceac176f6098b84a07ea263ecd314ff735f3e
7
- data.tar.gz: 99a797b196fc7d20c60026b39e5500c7b5a048ce743d92ae37b55412370b429c52a9623cc74031535811ce9743ba43dc197d923cae99fa4a5623ee368cec9ca2
6
+ metadata.gz: ad656ea48e81585d91232caa3a8f107fb9eaa06174c3df6cef346e66c4efb0986dce0b0729b10facb4d1954c907e04a501ab48ae910c367af84026443ed5d712
7
+ data.tar.gz: bd994daea95faa0a0354ad7483cf92c33ee0915a6a85f3e56fd698c64ee5919c059d5a63ab49f6da2a2a2e90fb306b3c1aa7343f38af3884c332c53baa115ba8
data/CHANGELOG.md CHANGED
@@ -1,9 +1,15 @@
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
+
4
10
  ## [0.2.5] - 2022-05-19
5
11
 
6
- # Improvements
12
+ ### Improvements
7
13
 
8
14
  * Form#required_attributes returns `[]` if `params[:_destroy]` present
9
15
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- readymade (0.2.5)
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.
@@ -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.5'
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.5
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-19 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