filter_form 0.0.2 → 0.0.3

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: 0687dda492a9e82d4e3b5555586410a5679a52e7
4
- data.tar.gz: 167fb3b2e8dc2cd001b2dd8c618088632cb12263
3
+ metadata.gz: 9426e908a32ac701218c4ba33aaaadef4e6575cb
4
+ data.tar.gz: 78121225fa25f3cf8008e5677f16403c7d0497e5
5
5
  SHA512:
6
- metadata.gz: 78954032e35a6c3e71dbdfb2e5e678a90a41a6d163e658d0f76240c775f7ec517dd0c26af01927135ad888548b9fac813f1a1d337da5540a773990cecbadb755
7
- data.tar.gz: c6ed716a620c7465a14c1318c1dd130e37dad616588f276c58f9dc4ce49356f6ff32d186cbfc9bd90b2d5371f8d67752384cc622a161c53b91d319e5b393a2bf
6
+ metadata.gz: 1e2ec010b96b771e578d82f082392f1f9ee1543c1610e7ecf050e84611382b4d546213b559720403cee58c2aba5a0358df3b30ed15e76816774cdc31197063a2
7
+ data.tar.gz: 2a48d297c0ef88a01d1c9cb66cd2f1952a62e78830a91b4caf43d23e7aa8a6a9f0e7bc606618750d1423d413bf07a85cd8825bd2ce4481d93e8a3585e1af067b
data/README.md CHANGED
@@ -45,7 +45,7 @@ For `string` attribute (like name) it will automatically create a text input wit
45
45
 
46
46
  For `integer` type (age) it will set predicate `eq`.
47
47
 
48
- For association's `foreign key` (city_id) it will automatically build a select tag.
48
+ For association's belongs_to `foreign key` (city_id) it will automatically build a select tag.
49
49
 
50
50
  For `date` and `datetime` (birthday) it will automatically add jQuery [datepicker](http://jqueryui.com/datepicker/) and set predicate `eq`.
51
51
 
@@ -0,0 +1,59 @@
1
+ require 'filter_form/inputs/base'
2
+ require 'filter_form/inputs/string'
3
+
4
+ require 'filter_form/inputs/belongs_to_eq'
5
+ require 'filter_form/inputs/string_cont'
6
+ require 'filter_form/inputs/integer_eq'
7
+ require 'filter_form/inputs/date_eq'
8
+
9
+ module FilterForm
10
+ class InputBuilder
11
+ include ActiveModel::Model
12
+
13
+ attr_accessor :attribute_name, :object
14
+
15
+ def build
16
+ "FilterForm::Inputs::#{ input_type.camelize }".constantize.new(attribute_name: attribute_name, object: object)
17
+ end
18
+
19
+ private
20
+
21
+ def input_type
22
+ "#{ type }_#{ predicate }"
23
+ end
24
+
25
+ def predicate
26
+ case attribute_type
27
+ when :string
28
+ :cont
29
+ else
30
+ :eq
31
+ end
32
+ end
33
+
34
+ def type
35
+ case attribute_type
36
+ when :string
37
+ :string
38
+ when :integer
39
+ :integer
40
+ when :datetime, :date
41
+ :date
42
+ when :belongs_to
43
+ :belongs_to
44
+ end
45
+ end
46
+
47
+ def association_belongs_to?
48
+ object.klass.reflections[attribute_name] && object.klass.reflections[attribute_name].belongs_to?
49
+ end
50
+
51
+ def attribute_type
52
+ if association_belongs_to?
53
+ :belongs_to
54
+ else
55
+ object.klass.columns_hash[attribute_name.to_s].type
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,34 @@
1
+ module FilterForm
2
+ module Inputs
3
+ class Base
4
+ include ActiveModel::Model
5
+
6
+ attr_accessor :attribute_name, :object
7
+
8
+ def options
9
+ {
10
+ required: false,
11
+ input_html: { name: input_name }
12
+ }
13
+ end
14
+
15
+ private
16
+
17
+ def input_name
18
+ "q[#{ attribute_name }_#{ predicate }]"
19
+ end
20
+
21
+ def input_value
22
+ object_condition.values.first.value if object_condition
23
+ end
24
+
25
+ def predicate
26
+ self.class.to_s.underscore.split('_').last
27
+ end
28
+
29
+ def object_condition
30
+ object.base.conditions.select { |c| c.a.first.name == attribute_name.to_s }.first
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,36 @@
1
+ module FilterForm
2
+ module Inputs
3
+ class BelongsToEq < FilterForm::Inputs::Base
4
+ def options
5
+ super.merge(additional_options)
6
+ end
7
+
8
+ private
9
+
10
+ def additional_options
11
+ {
12
+ as: :select,
13
+ collection: collection,
14
+ include_blank: true,
15
+ selected: input_value
16
+ }
17
+ end
18
+
19
+ def object_condition
20
+ object.base.conditions.select { |c| c.a.first.name == custom_attribute_name }.first
21
+ end
22
+
23
+ def collection
24
+ attribute_name.to_s.camelize.constantize.all
25
+ end
26
+
27
+ def input_name
28
+ "q[#{ custom_attribute_name }_#{ predicate }]"
29
+ end
30
+
31
+ def custom_attribute_name
32
+ "#{ attribute_name }_id"
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ module FilterForm
2
+ module Inputs
3
+ class DateEq < FilterForm::Inputs::String
4
+ DATE_CLASS = 'filter_form_date'
5
+
6
+ private
7
+
8
+ def additional_input_options
9
+ super.merge(class: DATE_CLASS)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ module FilterForm
2
+ module Inputs
3
+ class IntegerEq < FilterForm::Inputs::String
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,19 @@
1
+ module FilterForm
2
+ module Inputs
3
+ class String < FilterForm::Inputs::Base
4
+ def options
5
+ result = super.merge(as: :string)
6
+ result[:input_html].merge!(additional_input_options)
7
+ result
8
+ end
9
+
10
+ private
11
+
12
+ def additional_input_options
13
+ {
14
+ value: input_value
15
+ }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,6 @@
1
+ module FilterForm
2
+ module Inputs
3
+ class StringCont < FilterForm::Inputs::String
4
+ end
5
+ end
6
+ end
@@ -1,10 +1,11 @@
1
- require 'filter_form/input'
1
+ require 'filter_form/input_builder'
2
2
 
3
3
  module SimpleForm
4
4
  class FormBuilder < ActionView::Helpers::FormBuilder
5
5
  def filter_input(attribute_name, options = {}, &block)
6
- filter_input = FilterForm::Input.new(attribute_name: attribute_name, object: object)
7
- options.reverse_merge!(filter_input.options)
6
+ filter_form_input = FilterForm::InputBuilder.new(attribute_name: attribute_name, object: object).build
7
+
8
+ options.reverse_merge!(filter_form_input.options)
8
9
 
9
10
  input(attribute_name, options, &block)
10
11
  end
@@ -1,3 +1,3 @@
1
1
  module FilterForm
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeny Li
@@ -95,7 +95,13 @@ files:
95
95
  - lib/assets/javascripts/filter_form.js.coffee
96
96
  - lib/filter_form.rb
97
97
  - lib/filter_form/form_helper.rb
98
- - lib/filter_form/input.rb
98
+ - lib/filter_form/input_builder.rb
99
+ - lib/filter_form/inputs/base.rb
100
+ - lib/filter_form/inputs/belongs_to_eq.rb
101
+ - lib/filter_form/inputs/date_eq.rb
102
+ - lib/filter_form/inputs/integer_eq.rb
103
+ - lib/filter_form/inputs/string.rb
104
+ - lib/filter_form/inputs/string_cont.rb
99
105
  - lib/filter_form/simple_form/form_builder.rb
100
106
  - lib/filter_form/version.rb
101
107
  homepage: https://github.com/exAspArk/filter_form
@@ -1,113 +0,0 @@
1
- module FilterForm
2
- class Input
3
- DATE_CLASS = 'filter_form_date'
4
-
5
- include ActiveModel::Model
6
-
7
- attr_accessor :attribute_name, :object
8
-
9
- def options
10
- result = default_options
11
-
12
- case type
13
- when :select
14
- result.merge!(select_options)
15
- else
16
- result[:input_html].merge!(value: input_value)
17
- end
18
-
19
- case attribute_type
20
- when :datetime, :date
21
- result[:input_html].merge!(class: DATE_CLASS)
22
- end
23
-
24
- result
25
- end
26
-
27
- private
28
-
29
- def default_options
30
- {
31
- as: type,
32
- required: false,
33
- input_html: { name: input_name }
34
- }
35
- end
36
-
37
- # Select options ------------------------------------------------------------
38
-
39
- def select_options
40
- {
41
- collection: collection,
42
- include_blank: true,
43
- selected: selected
44
- }
45
- end
46
-
47
- def collection
48
- if reference_attribute?
49
- attribute_name.to_s.gsub('_id', '').camelize.constantize.all
50
- else
51
- object.klass.pluck(attribute_name).uniq
52
- end
53
- end
54
-
55
- def selected
56
- if object_condition.present?
57
- input_value
58
- else
59
- false
60
- end
61
- end
62
-
63
- # ---------------------------------------------------------------------------
64
-
65
- def input_name
66
- "q[#{ input_attribute_name }]"
67
- end
68
-
69
- def type
70
- return :select if reference_attribute?
71
-
72
- case attribute_type
73
- when :string, :integer, :datetime, :date
74
- :string
75
- when :float
76
- :float
77
- when :boolean
78
- :radio_buttons
79
- else
80
- :select
81
- end
82
- end
83
-
84
- def input_value
85
- object_condition.values.first.value if object_condition
86
- end
87
-
88
- def reference_attribute?
89
- attribute_name.to_s.end_with?('_id')
90
- end
91
-
92
- def object_condition
93
- object.base.conditions.select { |c| c.a.first.name == attribute_name.to_s }.first
94
- end
95
-
96
- def input_attribute_name
97
- "#{ attribute_name }_#{ predicate_name }"
98
- end
99
-
100
- def attribute_type
101
- object.klass.columns_hash[attribute_name.to_s].type
102
- end
103
-
104
- def predicate_name
105
- case attribute_type
106
- when :string
107
- 'cont'
108
- else
109
- 'eq'
110
- end
111
- end
112
- end
113
- end