filter_form 0.1.0 → 0.2.1

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: b830a861ee39acd8f2c1b5ed04935ad77f3b509a
4
- data.tar.gz: c311699b26fb7f0bca2ef57e6173287121735121
3
+ metadata.gz: 72ec8aa3c0d7466d80687239848d0e71ac1091a4
4
+ data.tar.gz: d59e186f531264052324f54ad175fc5cc90a25a0
5
5
  SHA512:
6
- metadata.gz: 69267fd08e65bbb07f3757f1f37a2d4893c802fb12515a1043c864744d9ef8c7ec7d0f87b2251725c2edf243677fbd627cdce8c3dd337bc3b2271ff4bd97ad05
7
- data.tar.gz: 72c09ffcc2dd31f588dd6866f3ad7a7655c3b1e6c414f7000a8879d936046e6784f70dcd86aafdfd4112b841b473ea578b4ad7c0c1244284d9cd7d5ac6bde616
6
+ metadata.gz: 3ee9bd15ec95c1485d19f8e464a11647ddf35e0b307e77b1fcb67e4088da8c9a33b994424b21a11470aebc2f7cad8871bea64a0c1168eb915ae36fdd7dd02dc9
7
+ data.tar.gz: 4bebe228358abc58495d3bf955c7cf616d1ad69e21a65fe5205f049e96d10cba5d634f15f84ba3520308af331c4a3f611c85db681fa5ba6c9c4f5a760d206e6c
data/README.md CHANGED
@@ -47,7 +47,6 @@ In your view file:
47
47
  Mapping | Database Column Type | Default predicate | Generated HTML Element |
48
48
  --------------- |:------------------------------------------------|:----------------------|:--------------------------|
49
49
  `string` | `string` | `cont` | `input[type=text]` |
50
- `select2` | `string` | `eq` | `select` |
51
50
  `integer` | `integer` | `eq` | `input[type=text]` |
52
51
  `datetime` | `datetime` | `eq` | `input[type=text]` |
53
52
  `date` | `date` | `eq` | `input[type=text]` |
@@ -60,7 +59,7 @@ Of course you can customize your filter, like:
60
59
 
61
60
  ```erb
62
61
  <%= filter_form_for @q do |f| %>
63
- <%= f.filter_input :title, as: :select2 %>
62
+ <%= f.filter_input :title, as: :select, in: :select2 %>
64
63
  <%= f.filter_input :year, as: :select, collection: (2000..2013).to_a, predicate: :not_eq %>
65
64
  <%= f.button :submit %>
66
65
  <% end %>
@@ -1,16 +1,12 @@
1
1
  module FilterForm
2
- module Inputs
2
+ module InputOptions
3
3
  class Base
4
4
  include ActiveModel::Model
5
5
 
6
- INPUT_CLASS = ''
6
+ attr_accessor :attribute_name, :object, :predicate, :options
7
7
 
8
- attr_accessor :attribute_name, :object, :predicate
9
-
10
- def options
11
- result = default_options.merge(additional_options)
12
- result[:input_html].merge!(additional_input_options)
13
- result
8
+ def simple_form_options
9
+ default_options.merge(additional_options).merge(options)
14
10
  end
15
11
 
16
12
  private
@@ -18,7 +14,7 @@ module FilterForm
18
14
  def default_options
19
15
  {
20
16
  required: false,
21
- input_html: { name: input_name }
17
+ input_html: { name: input_name }.merge(additional_input_options).merge(options.delete(:input_html) || {})
22
18
  }
23
19
  end
24
20
 
@@ -27,7 +23,11 @@ module FilterForm
27
23
  end
28
24
 
29
25
  def additional_input_options
30
- { class: self.class::INPUT_CLASS }
26
+ input_class ? { class: input_class } : {}
27
+ end
28
+
29
+ def input_class
30
+ nil
31
31
  end
32
32
 
33
33
  def input_name
@@ -1,7 +1,7 @@
1
1
  module FilterForm
2
- module Inputs
2
+ module InputOptions
3
3
  module Select
4
- class Base < FilterForm::Inputs::Base
4
+ class Base < FilterForm::InputOptions::Base
5
5
  private
6
6
 
7
7
  def additional_options
@@ -14,7 +14,11 @@ module FilterForm
14
14
  end
15
15
 
16
16
  def collection
17
- object.klass.uniq.pluck(attribute_name)
17
+ options[:collection] || object.klass.uniq.pluck(attribute_name)
18
+ end
19
+
20
+ def input_class
21
+ options[:in] == :select2 ? 'filter_form_select2' : super
18
22
  end
19
23
  end
20
24
  end
@@ -1,7 +1,7 @@
1
1
  module FilterForm
2
- module Inputs
2
+ module InputOptions
3
3
  module Select
4
- class BelongsTo < FilterForm::Inputs::Select::Base
4
+ class BelongsTo < FilterForm::InputOptions::Select::Base
5
5
  private
6
6
 
7
7
  def object_condition
@@ -9,7 +9,7 @@ module FilterForm
9
9
  end
10
10
 
11
11
  def collection
12
- attribute_name.to_s.camelize.constantize.all
12
+ options[:collection] || attribute_name.to_s.camelize.constantize.all
13
13
  end
14
14
 
15
15
  def input_name
@@ -1,7 +1,7 @@
1
1
  module FilterForm
2
- module Inputs
2
+ module InputOptions
3
3
  module String
4
- class Base < FilterForm::Inputs::Base
4
+ class Base < FilterForm::InputOptions::Base
5
5
  private
6
6
 
7
7
  def additional_options
@@ -0,0 +1,13 @@
1
+ module FilterForm
2
+ module InputOptions
3
+ module String
4
+ class Date < FilterForm::InputOptions::String::Base
5
+ private
6
+
7
+ def input_class
8
+ 'filter_form_date'
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,7 +1,7 @@
1
1
  module FilterForm
2
- module Inputs
2
+ module InputOptions
3
3
  module String
4
- class Money < FilterForm::Inputs::String::Base
4
+ class Money < FilterForm::InputOptions::String::Base
5
5
  private
6
6
 
7
7
  def input_value
@@ -1,31 +1,34 @@
1
- require 'filter_form/inputs/base'
1
+ require 'filter_form/input_options/base'
2
2
 
3
- require 'filter_form/inputs/select/base'
4
- require 'filter_form/inputs/select/belongs_to'
5
- require 'filter_form/inputs/select/select2'
3
+ require 'filter_form/input_options/select/base'
4
+ require 'filter_form/input_options/select/belongs_to'
6
5
 
7
- require 'filter_form/inputs/string/base'
8
- require 'filter_form/inputs/string/date'
9
- require 'filter_form/inputs/string/money'
6
+ require 'filter_form/input_options/string/base'
7
+ require 'filter_form/input_options/string/date'
8
+ require 'filter_form/input_options/string/money'
10
9
 
11
10
  module FilterForm
12
- class InputBuilder
11
+ class InputOptionsBuilder
13
12
  include ActiveModel::Model
14
13
 
15
14
  attr_accessor :attribute_name, :object, :custom_predicate, :custom_type
16
15
 
17
- def build
18
- input_class.new(attribute_name: attribute_name, object: object, predicate: predicate)
16
+ def build(options)
17
+ input_options_class.new(attribute_name: attribute_name, object: object, predicate: predicate, options: options)
19
18
  end
20
19
 
21
20
  private
22
21
 
23
- def input_class
24
- result = "FilterForm::Inputs::#{ "#{ type }".camelize }".constantize
22
+ def input_options_class
23
+ result = constantize_input_options_class("#{ type }".camelize)
25
24
  raise NameError if result.class == Module
26
25
  result
27
26
  rescue NameError
28
- "FilterForm::Inputs::#{ "#{ type }/base".camelize }".constantize
27
+ constantize_input_options_class("#{ type }/base".camelize)
28
+ end
29
+
30
+ def constantize_input_options_class(class_name)
31
+ "FilterForm::InputOptions::#{ class_name }".constantize
29
32
  end
30
33
 
31
34
  def predicate
@@ -1,16 +1,16 @@
1
- require 'filter_form/input_builder'
1
+ require 'filter_form/input_options_builder'
2
2
 
3
3
  module SimpleForm
4
4
  class FormBuilder < ActionView::Helpers::FormBuilder
5
5
  def filter_input(attribute_name, options = {}, &block)
6
- input_builder = FilterForm::InputBuilder.new attribute_name: attribute_name,
7
- object: object,
8
- custom_type: options.delete(:as),
9
- custom_predicate: options[:predicate]
6
+ input_options_builder = FilterForm::InputOptionsBuilder.new attribute_name: attribute_name,
7
+ object: object,
8
+ custom_type: options.delete(:as),
9
+ custom_predicate: options.delete(:predicate)
10
10
 
11
- options.reverse_merge!(input_builder.build.options)
11
+ filter_form_input_options = input_options_builder.build(options)
12
12
 
13
- input(attribute_name, options, &block)
13
+ input(attribute_name, filter_form_input_options.simple_form_options, &block)
14
14
  end
15
15
  end
16
16
  end
@@ -1,3 +1,3 @@
1
1
  module FilterForm
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.1'
3
3
  end
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.1.0
4
+ version: 0.2.1
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-08 00:00:00.000000000 Z
11
+ date: 2013-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: simple_form
@@ -110,14 +110,13 @@ files:
110
110
  - lib/assets/stylesheets/filter_form.css.sass
111
111
  - lib/filter_form.rb
112
112
  - lib/filter_form/form_helper.rb
113
- - lib/filter_form/input_builder.rb
114
- - lib/filter_form/inputs/base.rb
115
- - lib/filter_form/inputs/select/base.rb
116
- - lib/filter_form/inputs/select/belongs_to.rb
117
- - lib/filter_form/inputs/select/select2.rb
118
- - lib/filter_form/inputs/string/base.rb
119
- - lib/filter_form/inputs/string/date.rb
120
- - lib/filter_form/inputs/string/money.rb
113
+ - lib/filter_form/input_options/base.rb
114
+ - lib/filter_form/input_options/select/base.rb
115
+ - lib/filter_form/input_options/select/belongs_to.rb
116
+ - lib/filter_form/input_options/string/base.rb
117
+ - lib/filter_form/input_options/string/date.rb
118
+ - lib/filter_form/input_options/string/money.rb
119
+ - lib/filter_form/input_options_builder.rb
121
120
  - lib/filter_form/ransack/search.rb
122
121
  - lib/filter_form/simple_form/form_builder.rb
123
122
  - lib/filter_form/version.rb
@@ -1,9 +0,0 @@
1
- module FilterForm
2
- module Inputs
3
- module Select
4
- class Select2 < FilterForm::Inputs::Select::Base
5
- INPUT_CLASS = 'filter_form_select2'
6
- end
7
- end
8
- end
9
- end
@@ -1,9 +0,0 @@
1
- module FilterForm
2
- module Inputs
3
- module String
4
- class Date < FilterForm::Inputs::String::Base
5
- INPUT_CLASS = 'filter_form_date'
6
- end
7
- end
8
- end
9
- end