sample_filter 0.1.3 → 0.1.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
  SHA1:
3
- metadata.gz: 1ff44fc9766880a8b45d35e772878b18a58299f0
4
- data.tar.gz: 231830605bfaa85837008a3d88fe04582e9932bd
3
+ metadata.gz: d087c8414a36e1780a6fedaed77418690299809f
4
+ data.tar.gz: 62924512762cedbaf4e2369b0b4110a1dcce675c
5
5
  SHA512:
6
- metadata.gz: d924464af0cbf329af95928d4e9306008cec317ef299e18384add3064754fbe41186bbe407bebb39948dc719539f79303088731d81dc279d375816f72434e943
7
- data.tar.gz: e1cbde05ab4f0666da76de65fd2802232a2652bd22673b6bf191f390ecaba58ae109a5859897bdddc4da02f53ba0d69951ac4912c1a3a41bc4e895d0d0ff8ea0
6
+ metadata.gz: 702c40f2189f5805ae4e0f984eb13ce13da2b1d8e23d0050058063449a1beb145d13351c2fe64d357ab65d9f6cb85483e0d2b49e605c419a3dfe50c047c7b685
7
+ data.tar.gz: e022e507b78e4bcbaaca5255c0183498fd95f4fbc139200f96571cce17a168841b6309f44819b4e1096970a36d30a354a7e8c84f596c7bb969dc7df1f01ee50d
data/README.md CHANGED
@@ -5,7 +5,6 @@ SampleFilter is a Rails Engine plugin that makes to filter and sort ActiveRecord
5
5
 
6
6
  ## Installation
7
7
  Add this line to your application's Gemfile:
8
-
9
8
  ```ruby
10
9
  gem 'sample_filter'
11
10
  ```
@@ -24,6 +23,7 @@ Install javascripts air-datepicker dependency:
24
23
  ```bash
25
24
  $ yarn add air-datepicker
26
25
  ```
26
+
27
27
  Require SampleFilter javascripts with air-datepicker dependency in your `app/assets/javascripts/application.js`:
28
28
  ``` javascript
29
29
  //= require jquery
@@ -42,7 +42,6 @@ And then require styles in your `app/assets/stylesheets/application.css`:
42
42
 
43
43
  ### Configuring Models
44
44
  The SampleFilter method in your models accepts options to configure its modules. For example:
45
-
46
45
  ```ruby
47
46
  class Entity < ApplicationRecord
48
47
  sample_filter(
@@ -71,6 +70,13 @@ class Entity < ApplicationRecord
71
70
  end
72
71
  ```
73
72
 
73
+ for update values on demand in list or sorting fields, call method
74
+ ```ruby
75
+ Entity.filter_update_value(:kind, [:black, :white])
76
+ # or hash
77
+ Entity.filter_update_value(:kind, { black: 3, white: 4 })
78
+ ```
79
+
74
80
  ### Form helpers
75
81
  ```ruby
76
82
  <%= form_for_filter(Entity, as: :sample_filter, block: :after, html: { method: :get, id: :sample_filter}) do %>
@@ -95,7 +101,6 @@ In your `index` method:
95
101
 
96
102
  ### I18n
97
103
  SampleFilter uses labels with I18n:
98
-
99
104
  ```yaml
100
105
  en:
101
106
  sample_filter:
@@ -24,6 +24,10 @@ module SampleFilter
24
24
  ar_rel
25
25
  end
26
26
 
27
+ def filter_update_value(field, values)
28
+ filter_params_set.update_value(field, values)
29
+ end
30
+
27
31
  private
28
32
 
29
33
  def string_query(ar_rel, field, value)
@@ -26,6 +26,14 @@ module SampleFilter
26
26
  end
27
27
  end
28
28
 
29
+ def update_value(field, values)
30
+ field = field.to_sym
31
+ raise(ArgumentError, "#{field} not found error") unless fields.include?(field)
32
+ raise(ArgumentError, "#{field} values error") if invalid_values?(field, values)
33
+
34
+ options[field][:values] = values
35
+ end
36
+
29
37
  def permit_params(params)
30
38
  return {} unless params.present?
31
39
  _fields = fields.map{ |f| type_of(f).eql?(:date) ? Hash[f, [:from, :to]] : f }
@@ -46,7 +54,7 @@ module SampleFilter
46
54
  def define_and_assign_attr_accessors
47
55
  fields.each do |field|
48
56
  raise(ArgumentError, "#{field} type error") if invalid_type?(field)
49
- raise(ArgumentError, "#{field} values error") if invalid_values?(field)
57
+ raise(ArgumentError, "#{field} values error") if invalid_values?(field, values_for(field))
50
58
 
51
59
  self.class.send(:attr_reader, field)
52
60
  instance_variable_set("@#{field}", default_value_for(field))
@@ -57,11 +65,10 @@ module SampleFilter
57
65
  !FILTER_TYPES.include?(type_of(field))
58
66
  end
59
67
 
60
- def invalid_values?(field)
68
+ def invalid_values?(field, values)
61
69
  return false unless [:list, :sorting].include?(type_of(field))
62
- values = values_for(field)
63
-
64
70
  return true unless values.present?
71
+
65
72
  if type_of(field).eql?(:list)
66
73
  (!values.is_a?(Hash) && !values.is_a?(Array))
67
74
  elsif type_of(field).eql?(:sorting)
@@ -1,3 +1,3 @@
1
1
  module SampleFilter
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.4'
3
3
  end
@@ -0,0 +1,62 @@
1
+ context 'update values for params set field' do
2
+ let(:options) do
3
+ {
4
+ title: {type: :string},
5
+ amount: {type: :number},
6
+ 'created_at' => {'type' => 'date', default_value: {from: '09.11.2016', to: '17.11.2017'}},
7
+ kind: {
8
+ type: 'list',
9
+ values: {red: 1, 'green' => '2', red_and_green: [1, 2]},
10
+ default_value: [1,2]
11
+ },
12
+ status: {
13
+ type: :list,
14
+ values: [:confirm, 'unconfirm'],
15
+ default_value: :unconfirm
16
+ },
17
+ confirmed: {type: :boolean, default_value: false},
18
+ sort: {
19
+ type: :sorting,
20
+ values: [:id, :title, :amount],
21
+ default_value: 'id_desc'
22
+ }
23
+ }
24
+ end
25
+
26
+ subject { SampleFilter::ParamsSet.new(options) }
27
+
28
+ it 'should update values' do
29
+ expect(subject.values_for(:kind)).to eq({'red' => 1, 'green' => '2', 'red_and_green' => [1, 2]})
30
+ subject.update_value(:kind, {'blue' => 3, black: '4'})
31
+ expect(subject.values_for(:kind)).to eq({'blue' => 3, 'black' => '4'})
32
+ end
33
+
34
+ it 'should update array' do
35
+ expect(subject.values_for(:kind)).to eq({'red' => 1, 'green' => '2', 'red_and_green' => [1, 2]})
36
+ subject.update_value('kind', [:confirm, 'unconfirm'])
37
+ expect(subject.values_for(:kind)).to eq(['confirm', 'unconfirm'])
38
+ end
39
+
40
+ it 'should not update value' do
41
+ expect(subject.values_for(:title)).to be_nil
42
+ subject.update_value(:title, 'lalalala')
43
+ expect(subject.values_for(:title)).to be_nil
44
+ end
45
+
46
+ it { expect{ subject.update_value(:blablabla, [:confirm, 'unconfirm']) }.to raise_error(ArgumentError, 'blablabla not found error') }
47
+ it { expect{ subject.update_value(:kind, nil) }.to raise_error(ArgumentError, 'kind values error') }
48
+ it { expect{ subject.update_value(:kind, {}) }.to raise_error(ArgumentError, 'kind values error') }
49
+ it { expect{ subject.update_value(:kind, []) }.to raise_error(ArgumentError, 'kind values error') }
50
+ it { expect{ subject.update_value(:kind, 'string') }.to raise_error(ArgumentError, 'kind values error') }
51
+
52
+ it { expect{ subject.update_value(:status, nil) }.to raise_error(ArgumentError, 'status values error') }
53
+ it { expect{ subject.update_value(:status, {}) }.to raise_error(ArgumentError, 'status values error') }
54
+ it { expect{ subject.update_value(:status, []) }.to raise_error(ArgumentError, 'status values error') }
55
+ it { expect{ subject.update_value(:status, 'string') }.to raise_error(ArgumentError, 'status values error') }
56
+
57
+ it { expect{ subject.update_value(:sort, nil) }.to raise_error(ArgumentError, 'sort values error') }
58
+ it { expect{ subject.update_value(:sort, {}) }.to raise_error(ArgumentError, 'sort values error') }
59
+ it { expect{ subject.update_value(:sort, {test: :one}) }.to raise_error(ArgumentError, 'sort values error') }
60
+ it { expect{ subject.update_value(:sort, []) }.to raise_error(ArgumentError, 'sort values error') }
61
+ it { expect{ subject.update_value(:sort, 'string') }.to raise_error(ArgumentError, 'sort values error') }
62
+ end
@@ -0,0 +1,17 @@
1
+ require 'ar_helper'
2
+
3
+ context 'update entity field value' do
4
+ before do
5
+ class Entity < ActiveRecord::Base
6
+ sample_filter kind: {type: :list,values: [:confirm, 'unconfirm']}
7
+ end
8
+
9
+ Entity.create(title: 'text')
10
+ end
11
+
12
+ it 'shoud create user and define params set' do
13
+ expect(Entity.filter_params_set.values_for(:kind)).to eq(['confirm', 'unconfirm'])
14
+ Entity.filter_update_value('kind', [:black, :white])
15
+ expect(Entity.filter_params_set.values_for(:kind)).to eq(['black', 'white'])
16
+ end
17
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sample_filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - ilia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-14 00:00:00.000000000 Z
11
+ date: 2018-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -2673,6 +2673,8 @@ files:
2673
2673
  - spec/dummy/yarn.lock
2674
2674
  - spec/lib/params_set/new_spec.rb
2675
2675
  - spec/lib/params_set/update_attributes_spec.rb
2676
+ - spec/lib/params_set/update_value_spec.rb
2677
+ - spec/models/entities/filter_update_value_spec.rb
2676
2678
  - spec/models/entities/filtered_spec.rb
2677
2679
  - spec/models/entities/new_spec.rb
2678
2680
  - spec/spec_helper.rb
@@ -5275,6 +5277,8 @@ test_files:
5275
5277
  - spec/dummy/yarn.lock
5276
5278
  - spec/lib/params_set/new_spec.rb
5277
5279
  - spec/lib/params_set/update_attributes_spec.rb
5280
+ - spec/lib/params_set/update_value_spec.rb
5281
+ - spec/models/entities/filter_update_value_spec.rb
5278
5282
  - spec/models/entities/filtered_spec.rb
5279
5283
  - spec/models/entities/new_spec.rb
5280
5284
  - spec/spec_helper.rb