simple_form_ransack 0.0.11 → 0.0.12

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: 8a1915bb4fc37a6163a9415b73e2555fd5b8ffe8
4
- data.tar.gz: 939a9a718795a1624be9237d16415ae264151860
3
+ metadata.gz: 006388474436da19f6fdddf9f5d8841df249294d
4
+ data.tar.gz: 6b3c4b4d309a654566c72e3c963fad0389c4e19f
5
5
  SHA512:
6
- metadata.gz: 062fb8029ac90464a9717c6264386c3c3145a3e4a7d7a1644a784fab22ffa333764a6ea87c9d468fa5785494aa7b535156d9e7efd7613eba4fb36b658d56abc6
7
- data.tar.gz: bb6f45e1de84c9897b8622f24d02d614ccbb8a2bb97e6ca55f7c460346ab54d2a19c7bf88412a17abc74b86c3410769095cc5e8b612a500c5d51d50e4d5c9531
6
+ metadata.gz: 48d2f85c01f488af2caf19b92525ec86faf7e52f04573f3261bf7bf8a44f3efb3eff7521085968dfaace16b2b677da981342d5064b3221c79f96ee3b1e96b80e
7
+ data.tar.gz: 098d43e27853fc4fb77a8b5b41f8f7a456f2a145eeecf4252d6ad422a1d29a34292884555337786d4dcb4eec880242c053f2686eed34c0ce5165747f4008633d
@@ -1,5 +1,5 @@
1
1
  module SimpleFormRansackHelper
2
- def simple_search_form_for(resource, params, *args)
2
+ def simple_search_form_for(ransack, *args)
3
3
  if args.last.is_a?(Hash)
4
4
  opts = args.pop
5
5
  else
@@ -10,14 +10,16 @@ module SimpleFormRansackHelper
10
10
  opts[:method] = "get" unless opts[:method]
11
11
  args << opts
12
12
 
13
- model_class = resource.klass
13
+ model_class = ransack.klass
14
14
  sample_model = model_class.new
15
15
 
16
+ search_key = ransack.context.search_key
17
+
16
18
  simple_form_for(sample_model, *args) do |form|
17
19
  form_proxy = SimpleFormRansack::FormProxy.new(
18
- resource: resource,
20
+ ransack: ransack,
19
21
  form: form,
20
- params: params
22
+ params: params[search_key] || {}
21
23
  )
22
24
 
23
25
  yield form_proxy
@@ -0,0 +1,9 @@
1
+ da:
2
+ simple_form_ransack:
3
+ words:
4
+ or: eller
5
+ and: og
6
+ match_types:
7
+ cont: indeholder
8
+ gteq: større eller lig med
9
+ lteq: mindre eller lig med
@@ -0,0 +1,9 @@
1
+ en:
2
+ simple_form_ransack:
3
+ words:
4
+ or: or
5
+ and: and
6
+ match_types:
7
+ cont: contains
8
+ gteq: greather than or equals
9
+ lteq: less than or equals
@@ -1,6 +1,18 @@
1
1
  module SimpleFormRansack
2
2
  autoload :AttributeInspector, "simple_form_ransack/attribute_inspector"
3
+ autoload :InputManipulator, "simple_form_ransack/input_manipulator"
3
4
  autoload :FormProxy, "simple_form_ransack/form_proxy"
5
+
6
+ def self.locale_files
7
+ files = []
8
+
9
+ I18n.available_locales.each do |locale|
10
+ path = "#{File.realpath("#{File.dirname(__FILE__)}/../config/locales")}/#{locale}.yml"
11
+ files << File.realpath(path) if File.exist?(path)
12
+ end
13
+
14
+ files
15
+ end
4
16
  end
5
17
 
6
18
  require_relative "../app/helpers/simple_form_ransack_helper"
@@ -2,10 +2,11 @@
2
2
  class SimpleFormRansack::AttributeInspector
3
3
  def initialize(args)
4
4
  @args = args
5
- @ransack_name = args[:name]
5
+ @ransack_name = args.fetch(:name)
6
6
  @name_parts = @ransack_name.to_s.split("_")
7
- @instance = args[:instance]
8
- @clazz = args[:clazz]
7
+ @instance = args.fetch(:instance)
8
+ @clazz = args.fetch(:clazz)
9
+ @as = args.fetch(:as)
9
10
  @debug = args[:debug]
10
11
 
11
12
  @generated_name_classes = []
@@ -13,9 +14,7 @@ class SimpleFormRansack::AttributeInspector
13
14
  @current_clazz = @clazz
14
15
  @name_builtup = []
15
16
 
16
- has_attribute_directly = @clazz.attribute_names.select { |name| name.to_s == @ransack_name.to_s }.any?
17
-
18
- research unless has_attribute_directly
17
+ research unless attribute_directly?
19
18
  end
20
19
 
21
20
  # Loop through the name parts and inspectors reflections with it.
@@ -26,15 +25,9 @@ class SimpleFormRansack::AttributeInspector
26
25
  # The last part should be the attribute name.
27
26
  if index == @name_parts.length - 1
28
27
  attribute_result = attribute_by_builtup
29
-
30
- if attribute_result
31
- puts "Attribute was: #{attribute_result.fetch(:name)}" if @debug
32
- @attribute = attribute_result.fetch(:name)
33
- break
34
- else
35
- puts "Not found: #{@name_builtup.join("_")}" if @debug
36
- next
37
- end
28
+ next unless attribute_result
29
+ @attribute = attribute_result.fetch(:name)
30
+ break
38
31
  end
39
32
 
40
33
  # Try next - maybe next key need to be added? (which is common!)
@@ -42,12 +35,8 @@ class SimpleFormRansack::AttributeInspector
42
35
  next unless reflection_result
43
36
 
44
37
  @name_builtup = []
45
- name = reflection_result.fetch(:name)
46
38
  reflection = reflection_result.fetch(:reflection)
47
39
 
48
- puts "Name: #{name}" if @debug
49
- puts "Reflection: #{reflection}" if @debug
50
-
51
40
  @current_clazz = reflection.klass
52
41
  @generated_name_classes << {clazz: @current_clazz, reflection: reflection}
53
42
  end
@@ -61,27 +50,34 @@ class SimpleFormRansack::AttributeInspector
61
50
 
62
51
  # Generates the complicated label and returns it.
63
52
  def generated_label
64
- name = ""
53
+ @generated_label = ""
65
54
 
66
55
  if @generated_name_classes.last
67
56
  clazz = @generated_name_classes.last.fetch(:clazz)
68
57
  reflection = @generated_name_classes.last.fetch(:reflection)
69
58
 
70
59
  if reflection.collection?
71
- name << clazz.model_name.human(count: 2)
60
+ @generated_label << clazz.model_name.human(count: 2)
72
61
  else
73
- name << clazz.model_name.human
62
+ @generated_label << clazz.model_name.human
74
63
  end
75
64
  end
76
65
 
77
- name << " " unless name.empty?
78
- name << @current_clazz.human_attribute_name(@attribute).to_s.downcase
79
-
80
- name
66
+ add_id_to_generated_label
67
+ @generated_label
81
68
  end
82
69
 
83
70
  private
84
71
 
72
+ def add_id_to_generated_label
73
+ if @attribute == "id" && @as != "string"
74
+ # Don't add "id" to label, because it is being shown as a collection
75
+ else
76
+ @generated_label << " " unless @generated_label.empty?
77
+ @generated_label << @current_clazz.human_attribute_name(@attribute).to_s.downcase
78
+ end
79
+ end
80
+
85
81
  def reflection_by_builtup
86
82
  total_name = @name_builtup.join("_")
87
83
  result = @current_clazz.reflections.find { |name, _reflection| name.to_s == total_name }
@@ -95,4 +91,8 @@ private
95
91
  return {name: result} if result
96
92
  false
97
93
  end
94
+
95
+ def attribute_directly?
96
+ @clazz.attribute_names.find { |name| name.to_s == @ransack_name.to_s }
97
+ end
98
98
  end
@@ -14,9 +14,9 @@ class SimpleFormRansack::FormProxy
14
14
  end
15
15
 
16
16
  def initialize(args)
17
- @resource = args.fetch(:resource)
18
- @object = @resource.object
19
- @class = @resource.klass
17
+ @ransack = args.fetch(:ransack)
18
+ @object = @ransack.object
19
+ @class = @ransack.klass
20
20
  @params = args.fetch(:params)
21
21
  @form = args.fetch(:form)
22
22
 
@@ -24,109 +24,17 @@ class SimpleFormRansack::FormProxy
24
24
  end
25
25
 
26
26
  def input(name, *args)
27
- if args.last.is_a?(Hash)
28
- opts = args.pop
29
- else
30
- opts = {}
31
- end
32
-
33
- attribute_name = real_name(name)
34
- as = as_from_opts(attribute_name, opts)
35
- input_html = opts.delete(:input_html) || {}
36
- set_value(as, name, opts, input_html)
37
- set_name(as, name, input_html)
38
- set_label(attribute_name, opts, input_html)
27
+ input_manipulator = SimpleFormRansack::InputManipulator.new(
28
+ name: name,
29
+ args: args,
30
+ params: @params,
31
+ class: @class
32
+ )
39
33
 
40
- opts[:required] = false unless opts.key?(:required)
41
- opts[:input_html] = input_html
42
- args << opts
43
- @form.input(attribute_name, *args)
34
+ @form.input(input_manipulator.attribute_name, *args)
44
35
  end
45
36
 
46
37
  def method_missing(method_name, *args, &blk)
47
38
  @form.__send__(method_name, *args, &blk)
48
39
  end
49
-
50
- private
51
-
52
- def set_label(attribute_name, opts, _input_html)
53
- unless opts.key?(:label)
54
- attribute_inspector = ::SimpleFormRansack::AttributeInspector.new(
55
- name: attribute_name,
56
- instance: @object,
57
- clazz: @class
58
- )
59
-
60
- if attribute_inspector.generated_label?
61
- opts[:label] = attribute_inspector.generated_label
62
- end
63
- end
64
- end
65
-
66
- def set_name(as, name, input_html)
67
- return if input_html.key?(:name)
68
-
69
- input_html[:name] = "q[#{name}]"
70
- input_html[:name] << "[]" if as == "check_boxes"
71
- end
72
-
73
- def set_value(as, name, opts, input_html)
74
- if as == "select"
75
- unless opts.key?(:selected)
76
- if @params[name]
77
- opts[:selected] = @params[name]
78
- else
79
- opts[:selected] = ""
80
- end
81
- end
82
- elsif as == "check_boxes" || as == "radio_buttons"
83
- unless opts.key?(:checked)
84
- if @params[name]
85
- opts[:checked] = @params[name]
86
- else
87
- opts[:checked] = ""
88
- end
89
- end
90
- elsif as == "boolean"
91
- unless input_html.key?(:checked)
92
- input_html[:checked] = ("checked" if @params[name] == "1")
93
- end
94
- else
95
- unless input_html.key?(:value)
96
- if @params[name]
97
- input_html[:value] = @params[name]
98
- else
99
- input_html[:value] = ""
100
- end
101
- end
102
- end
103
- end
104
-
105
- def as_list?(opts)
106
- return true if as_from_opts(opts) == "select"
107
- false
108
- end
109
-
110
- def as_from_opts(attribute_name, opts)
111
- if opts[:as].present?
112
- return opts.fetch(:as).to_s
113
- elsif opts[:collection] || attribute_name.end_with?("country")
114
- return "select"
115
- end
116
-
117
- column = @class.columns_hash[attribute_name]
118
- return "boolean" if column && column.type == :boolean
119
-
120
- "text"
121
- end
122
-
123
- def real_name(name)
124
- match = name.to_s.match(SimpleFormRansack::FormProxy.predicates_regex)
125
-
126
- if match
127
- return match[1]
128
- else
129
- raise "Couldn't figure out attribute name from: #{name}"
130
- end
131
- end
132
40
  end
@@ -0,0 +1,176 @@
1
+ class SimpleFormRansack::InputManipulator
2
+ attr_reader :args
3
+
4
+ def initialize(args)
5
+ @args = args.fetch(:args)
6
+ @name = args.fetch(:name)
7
+ @params = args.fetch(:params)
8
+ @class = args.fetch(:class)
9
+
10
+ if @args.last.is_a?(Hash)
11
+ @opts = @args.pop
12
+ else
13
+ @opts = {}
14
+ end
15
+
16
+ @input_html = @opts[:input_html] || {}
17
+
18
+ calculate_name
19
+ calculate_as
20
+
21
+ set_values
22
+ end
23
+
24
+ def attribute_name
25
+ @attribute_name || @name
26
+ end
27
+
28
+ private
29
+
30
+ def set_values
31
+ @opts[:required] = false unless @opts.key?(:required)
32
+ @opts[:input_html] = @input_html
33
+
34
+ set_value
35
+ set_name
36
+ set_label if @attribute_name && !@opts.key?(:label)
37
+
38
+ @args << @opts
39
+ end
40
+
41
+ def calculate_name
42
+ match = @name.to_s.match(SimpleFormRansack::FormProxy.predicates_regex)
43
+ return unless match
44
+
45
+ @attribute_name = match[1]
46
+ @match_type = match[2]
47
+
48
+ @name_parts = @attribute_name.split(/_(or|and)_/)
49
+ @name_parts_length = @name_parts.length
50
+ end
51
+
52
+ def calculate_as
53
+ column = @class.columns_hash[@attribute_name]
54
+
55
+ if @opts[:as].present?
56
+ @as = @opts.fetch(:as).to_s
57
+ elsif @opts[:collection]
58
+ @as = "select"
59
+ elsif @attribute_name.to_s.end_with?("country")
60
+ @as = "country"
61
+ elsif column && column.type == :boolean
62
+ @as = "boolean"
63
+ end
64
+
65
+ @as ||= "string"
66
+ end
67
+
68
+ def set_name
69
+ @input_html[:id] = "q_#{@name}"
70
+
71
+ return if @input_html.key?(:name)
72
+
73
+ @input_html[:name] = "q[#{@name}]"
74
+ @input_html[:name] << "[]" if @as == "check_boxes"
75
+ end
76
+
77
+ def set_label
78
+ label_parts = []
79
+
80
+ @name_parts.each_with_index do |attribute_name_part, index|
81
+ if attribute_name_part == "and" || attribute_name_part == "or"
82
+ label_parts << add_between_label(index, attribute_name_part) unless label_parts.empty?
83
+ next
84
+ end
85
+
86
+ label = label_part(attribute_name_part)
87
+
88
+ if label
89
+ label[0] = label[0].downcase if label_parts.any?
90
+ label_parts << label
91
+ end
92
+ end
93
+
94
+ label_from_parts(label_parts)
95
+ end
96
+
97
+ def add_between_label(index, attribute_name_part)
98
+ if index == @name_parts_length - 2
99
+ " #{I18n.t("simple_form_ransack.words.#{attribute_name_part}")} "
100
+ else
101
+ ", "
102
+ end
103
+ end
104
+
105
+ def label_from_parts(label_parts)
106
+ return if label_parts.empty?
107
+
108
+ @opts[:label] = label_parts.join
109
+ prepend_label_for = %w(cont gteq lteq)
110
+
111
+ return unless prepend_label_for.include?(@match_type)
112
+ @opts[:label] << " #{I18n.t("simple_form_ransack.match_types.#{@match_type}")}"
113
+ end
114
+
115
+ def label_part(attribute_name_part)
116
+ attribute_inspector = ::SimpleFormRansack::AttributeInspector.new(
117
+ name: attribute_name_part,
118
+ instance: @object,
119
+ clazz: @class,
120
+ as: @as
121
+ )
122
+
123
+ if attribute_inspector.generated_label?
124
+ attribute_inspector.generated_label
125
+ else
126
+ @class.human_attribute_name(attribute_name_part)
127
+ end
128
+ end
129
+
130
+ def set_value
131
+ if @as == "select" || @as == "country"
132
+ set_value_for_select
133
+ elsif @as == "check_boxes" || @as == "radio_buttons"
134
+ set_value_for_checked
135
+ elsif @as == "boolean"
136
+ set_value_for_boolean
137
+ else
138
+ set_value_for_input
139
+ end
140
+ end
141
+
142
+ def set_value_for_select
143
+ return if @opts.key?(:selected)
144
+
145
+ if @params[@name]
146
+ @opts[:selected] = @params[@name]
147
+ else
148
+ @opts[:selected] = ""
149
+ end
150
+ end
151
+
152
+ def set_value_for_checked
153
+ return if @opts.key?(:checked)
154
+
155
+ if @params[@name]
156
+ @opts[:checked] = @params[@name]
157
+ else
158
+ @opts[:checked] = ""
159
+ end
160
+ end
161
+
162
+ def set_value_for_boolean
163
+ return if @input_html.key?(:checked)
164
+ @input_html[:checked] = ("checked" if @params[@name] == "1")
165
+ end
166
+
167
+ def set_value_for_input
168
+ return if @input_html.key?(:value)
169
+
170
+ if @params[@name]
171
+ @input_html[:value] = @params[@name]
172
+ else
173
+ @input_html[:value] = ""
174
+ end
175
+ end
176
+ end
@@ -1,3 +1,3 @@
1
1
  module SimpleFormRansack
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_form_ransack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Johansen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-21 00:00:00.000000000 Z
11
+ date: 2016-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,34 +24,48 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: capybara
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 2.6.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 2.6.0
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: sqlite3
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - ">="
45
+ - - '='
32
46
  - !ruby/object:Gem::Version
33
- version: '0'
47
+ version: 1.3.9
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - ">="
52
+ - - '='
39
53
  - !ruby/object:Gem::Version
40
- version: '0'
54
+ version: 1.3.9
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rspec-rails
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - ">="
59
+ - - '='
46
60
  - !ruby/object:Gem::Version
47
- version: '0'
61
+ version: 3.4.0
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - ">="
66
+ - - '='
53
67
  - !ruby/object:Gem::Version
54
- version: '0'
68
+ version: 3.4.0
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: factory_girl_rails
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -175,9 +189,12 @@ files:
175
189
  - MIT-LICENSE
176
190
  - Rakefile
177
191
  - app/helpers/simple_form_ransack_helper.rb
192
+ - config/locales/da.yml
193
+ - config/locales/en.yml
178
194
  - lib/simple_form_ransack.rb
179
195
  - lib/simple_form_ransack/attribute_inspector.rb
180
196
  - lib/simple_form_ransack/form_proxy.rb
197
+ - lib/simple_form_ransack/input_manipulator.rb
181
198
  - lib/simple_form_ransack/version.rb
182
199
  - lib/tasks/simple_form_ransack_tasks.rake
183
200
  homepage: https://www.github.com/kaspernj/simple_form_ransack