filter_form 0.0.6 → 0.0.7

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: 7a7c1d58fb2ba9912bb9c2c660473c86ccef6b15
4
- data.tar.gz: 3eb6fb9a2b37a54faf157db67bc20ebaeb8dbad8
3
+ metadata.gz: 4ba17c92cfab5acf0f06e3b806295e51792f71cf
4
+ data.tar.gz: 0e5b396897ede23f430c1d889a342453621fac43
5
5
  SHA512:
6
- metadata.gz: eefd55897f30e589ec381a3adc540f3a866c9e38b6bed0a1313e35e746533fe736905ff20da89a99caf4b7bb766d13065f97f4daa963a360869a66885685872e
7
- data.tar.gz: 075413fb4d0ba0084573ca482b628fb00a7ffeb394374610f0bf6f3ec12a682079de7a63dcd7ee578faf61c9d655fdfbe563f350541896f9ba6fefabd7817e59
6
+ metadata.gz: 753b1d457e8548713aea1914312a384473e200d4d434e3b1b622ef1de9750f883607256ba8e89b12db35203e4485d0903da625140d54119a9fefb18804d306b0
7
+ data.tar.gz: b58d8c0548ab36ae2941af0d8f90a3f97648ce2f4c7e091bff7bd375d571b9d28694d9b2340262552b2f9305709c3342a0f1001c304246530d45392de48cdcb7
data/README.md CHANGED
@@ -33,20 +33,38 @@ In your view file:
33
33
 
34
34
  ```erb
35
35
  <%= filter_form_for @q do |f| %>
36
- <%= f.filter_input :name %>
37
- <%= f.filter_input :age %>
38
- <%= f.filter_input :city %>
39
- <%= f.filter_input :birthday %>
36
+ <%= f.filter_input :name # string %>
37
+ <%= f.filter_input :age # integer %>
38
+ <%= f.filter_input :city # belongs_to %>
39
+ <%= f.filter_input :birthday # date %>
40
+ <%= f.filter_input :amount # money %>
40
41
  <%= f.button :submit %>
41
42
  <% end %>
42
43
  ```
43
44
 
44
- * For `string` attribute (like name) it will automatically create a text input with predicate `cont` (contains).
45
- * For `integer` type (age) it will set predicate `eq`.
46
- * For association `belongs_to` (city) it will automatically build a select tag with `eq`.
47
- * For `date` and `datetime` (birthday) it will automatically add jQuery [datepicker](http://jqueryui.com/datepicker/) and set predicate `eq`.
45
+ ### Available types
48
46
 
49
- To use datepicker add to your application.js file:
47
+ Mapping | Database Column Type | Default predicate | Generated HTML Element |
48
+ --------------- |:------------------------------------------------|:----------------------|:--------------------------|
49
+ `string` | `string` | `cont` | `input[type=text]` |
50
+ `integer` | `integer` | `eq` | `input[type=text]` |
51
+ `datetime` | `datetime` | `eq` | `input[type=text]` |
52
+ `date` | `date` | `eq` | `input[type=text]` |
53
+ `belongs_to` | `belongs_to` association | `eq` | `select` |
54
+ `money` | `money` [monetized](https://github.com/RubyMoney/money-rails) attribute | `eq` | `input[type=text]` |
55
+
56
+ ### Customization
57
+
58
+ Of course you can customize your filter, like:
59
+
60
+ ```erb
61
+ <%= filter_form_for @q do |f| %>
62
+ <%= f.filter_input :year, as: :select, collection: ((Date.today.year - 3)..(Date.today.year + 3)).to_a, predicate: :eq %>
63
+ <%= f.button :submit %>
64
+ <% end %>
65
+ ```
66
+
67
+ If you want to use jQuery [datepicker](http://jqueryui.com/datepicker/) for `date` and `datetime` automatically, just add to your application.js file:
50
68
 
51
69
  ```js
52
70
  //= require jquery
@@ -60,14 +78,7 @@ And application.css:
60
78
  *= require jquery.ui.datepicker
61
79
  ```
62
80
 
63
- Of course you can customize your filter, like:
64
-
65
- ```erb
66
- <%= filter_form_for @q do |f| %>
67
- <%= f.filter_input :year, as: :select, collection: ((Date.today.year - 3)..(Date.today.year + 3)).to_a, predicate: :eq %>
68
- <%= f.button :submit %>
69
- <% end %>
70
- ```
81
+ ### Other sources
71
82
 
72
83
  For more information about predicates visit [ransack](https://github.com/ernie/ransack).
73
84
 
@@ -5,6 +5,7 @@ require 'filter_form/inputs/select/belongs_to'
5
5
 
6
6
  require 'filter_form/inputs/string/base'
7
7
  require 'filter_form/inputs/string/date'
8
+ require 'filter_form/inputs/string/money'
8
9
 
9
10
  module FilterForm
10
11
  class InputBuilder
@@ -38,15 +39,21 @@ module FilterForm
38
39
  end
39
40
 
40
41
  def type
41
- return custom_type if custom_type
42
+ custom_type ? map_type(custom_type) : map_type(attribute_type)
43
+ end
42
44
 
43
- case attribute_type
44
- when :string, :integer
45
- :string
45
+ def map_type(_type)
46
+ case _type
47
+ when :integer
48
+ 'string/base'
46
49
  when :datetime, :date
47
50
  'string/date'
51
+ when :money
52
+ 'string/money'
48
53
  when :belongs_to
49
54
  'select/belongs_to'
55
+ else
56
+ _type
50
57
  end
51
58
  end
52
59
 
@@ -54,9 +61,15 @@ module FilterForm
54
61
  object.klass.reflections[attribute_name] && object.klass.reflections[attribute_name].belongs_to?
55
62
  end
56
63
 
64
+ def money?
65
+ object.klass.columns_hash["#{ attribute_name }_cents"].present?
66
+ end
67
+
57
68
  def attribute_type
58
69
  if association_belongs_to?
59
70
  :belongs_to
71
+ elsif money?
72
+ :money
60
73
  else
61
74
  object.klass.columns_hash[attribute_name.to_s].type
62
75
  end
@@ -0,0 +1,19 @@
1
+ module FilterForm
2
+ module Inputs
3
+ module String
4
+ class Money < FilterForm::Inputs::String::Base
5
+ private
6
+
7
+ def input_value
8
+ if object_condition
9
+ object_condition.values.first.value.to_f / 100
10
+ end
11
+ end
12
+
13
+ def object_condition
14
+ object.base.conditions.select { |c| c.a.first.name == "#{ attribute_name }_cents" }.first
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ module Ransack
2
+ class Search
3
+ def initialize(object, params = {}, options = {})
4
+ (params ||= {}).delete_if { |k, v| v.blank? && v != false }
5
+ convert_money_param_name_and_value!(object, params)
6
+ @context = Context.for(object, options)
7
+ @context.auth_object = options[:auth_object]
8
+ @base = Nodes::Grouping.new(@context, 'and')
9
+ build(params.with_indifferent_access)
10
+ end
11
+
12
+ private
13
+
14
+ def convert_money_param_name_and_value!(object, params)
15
+ money_attribute_name = object.column_names.select { |c| c.end_with?('_cents') }.first
16
+
17
+ if money_attribute_name
18
+ money_attribute_name = money_attribute_name.dup.gsub('_cents', '')
19
+ money_param_name = params.keys.select { |c| c.start_with?(money_attribute_name) }.first
20
+
21
+ if money_param_name
22
+ params[money_param_name.gsub(money_attribute_name, "#{ money_attribute_name }_cents")] = params.delete(money_param_name).to_f * 100
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module FilterForm
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.7'
3
3
  end
data/lib/filter_form.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'filter_form/version'
2
2
  require 'filter_form/form_helper'
3
3
  require 'filter_form/simple_form/form_builder'
4
+ require 'filter_form/ransack/search'
4
5
 
5
6
  module FilterForm
6
7
  class Engine < ::Rails::Engine
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filter_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeny Li
@@ -101,6 +101,8 @@ files:
101
101
  - lib/filter_form/inputs/select/belongs_to.rb
102
102
  - lib/filter_form/inputs/string/base.rb
103
103
  - lib/filter_form/inputs/string/date.rb
104
+ - lib/filter_form/inputs/string/money.rb
105
+ - lib/filter_form/ransack/search.rb
104
106
  - lib/filter_form/simple_form/form_builder.rb
105
107
  - lib/filter_form/version.rb
106
108
  homepage: https://github.com/exAspArk/filter_form