filter_form 0.5.1 → 0.6.0

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
  SHA1:
3
- metadata.gz: 78fdf1d68e74091137048d29a94868cdfbddc346
4
- data.tar.gz: 550744655f66c00cd600d498516f048144535dfa
3
+ metadata.gz: 6189d3b110196894801cee283b727c4dc3018086
4
+ data.tar.gz: af46f72476125da17a24b32f887d6cba28ff58d2
5
5
  SHA512:
6
- metadata.gz: c91ac9e4cadbebc9a360b63ad1f85437a2910ffe8b830f5842444b6968e40badcba0f44edac83b4eef8e52f78eb11181b4d7a22196ed60eb9b326235387248ea
7
- data.tar.gz: 68025c40f3714c55947b4e20c553293fbb0173e0bd602a51d58f4dc2d2aafa432dc4f91ace31f7335ce4ab4e5862facdd215c38cb2ccd131adaf63efe3fcfe54
6
+ metadata.gz: 17c3223cc02252a317a0a6ad186ef4c28a938141abbad1ffa94292fd790df2d4b0aa2395dfb73d9ae4abad771ae3d4953ace647269d6750eb2435ffde8b07838
7
+ data.tar.gz: 56fbb4db9539de5d13bc1882a32295e8f1363f95e88fea3c87745d230b130d085fe653bf95ecaac8a2824fc43cbc3aa34834c0267cc40863bf9918c689d4ecf1
data/README.md CHANGED
@@ -82,6 +82,17 @@ You can show predicate selector:
82
82
  <% end %>
83
83
  ```
84
84
 
85
+ #### Money
86
+
87
+ To filter by monetized attribute please add your controller:
88
+
89
+ ```erb
90
+ class ApplicationController < ActionController::Base
91
+ include FilterForm::MoneyConverter
92
+ ...
93
+ end
94
+ ```
95
+
85
96
  #### Select2
86
97
 
87
98
  You can wrap your select in `select2`:
@@ -10,6 +10,10 @@ module FilterForm
10
10
  end
11
11
  end
12
12
 
13
+ def input_name
14
+ "q[#{ attribute_name }_#{ predicate }]"
15
+ end
16
+
13
17
  def input_attribute_name
14
18
  "#{ attribute_name }_cents"
15
19
  end
@@ -0,0 +1,28 @@
1
+ module FilterForm
2
+ module MoneyConverter
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ before_filter :convert_money_param_name_and_value, only: :index
7
+ end
8
+
9
+ private
10
+
11
+ def convert_money_param_name_and_value
12
+ money_column_name = resource_class.column_names.select { |c| c.end_with?('_cents') }.first
13
+
14
+ if money_column_name && params[:q]
15
+ money_attribute_name = money_column_name.dup.gsub('_cents', '')
16
+ money_param_name = params[:q].keys.select { |c| c.start_with?(money_attribute_name) }.first
17
+
18
+ if money_param_name
19
+ params[:q][money_param_name.gsub(money_attribute_name, "#{ money_attribute_name }_cents")] = params[:q].delete(money_param_name).to_f * 100
20
+ end
21
+ end
22
+ end
23
+
24
+ def resource_class
25
+ self.class.name.split('::').last.gsub('Controller', '').singularize.constantize
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module FilterForm
2
- VERSION = '0.5.1'
2
+ VERSION = '0.6.0'
3
3
  end
data/lib/filter_form.rb CHANGED
@@ -1,7 +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
+ require 'filter_form/money_converter'
5
5
 
6
6
  module FilterForm
7
7
  class Engine < ::Rails::Engine
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filter_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeny Li
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-28 00:00:00.000000000 Z
11
+ date: 2013-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: simple_form
@@ -120,7 +120,7 @@ files:
120
120
  - lib/filter_form/input_options/string/base.rb
121
121
  - lib/filter_form/input_options/string/date.rb
122
122
  - lib/filter_form/input_options_builder.rb
123
- - lib/filter_form/ransack/search.rb
123
+ - lib/filter_form/money_converter.rb
124
124
  - lib/filter_form/simple_form/form_builder.rb
125
125
  - lib/filter_form/version.rb
126
126
  homepage: https://github.com/exAspArk/filter_form
@@ -1,27 +0,0 @@
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