simple_form_ransack 0.0.10 → 0.0.11

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: 5b4c0e122f80c13f24d01d45df9b5dbdab1500bc
4
- data.tar.gz: d805d5d2b30de2c16e9559813de19dfc01cf96ed
3
+ metadata.gz: 8a1915bb4fc37a6163a9415b73e2555fd5b8ffe8
4
+ data.tar.gz: 939a9a718795a1624be9237d16415ae264151860
5
5
  SHA512:
6
- metadata.gz: d455e609222521307cc50fc1f664220ed987997613df53b761434a7cf38519413bd418fcf5e187480247a3d06be62a76a235c7b9b34f96317f232e177b42181a
7
- data.tar.gz: 7fc9f976a887e44919f4c3e25d2aaed4fe67129b0f3ea53b32f0887cc333a97f5cfb13c98198476499fe70b48f376905ec37574c34869ae522f3f3f94b248706
6
+ metadata.gz: 062fb8029ac90464a9717c6264386c3c3145a3e4a7d7a1644a784fab22ffa333764a6ea87c9d468fa5785494aa7b535156d9e7efd7613eba4fb36b658d56abc6
7
+ data.tar.gz: bb6f45e1de84c9897b8622f24d02d614ccbb8a2bb97e6ca55f7c460346ab54d2a19c7bf88412a17abc74b86c3410769095cc5e8b612a500c5d51d50e4d5c9531
data/Rakefile CHANGED
@@ -1,21 +1,17 @@
1
1
  begin
2
- require 'bundler/setup'
2
+ require "bundler/setup"
3
3
  rescue LoadError
4
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
4
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
5
5
  end
6
6
 
7
- require 'rdoc/task'
7
+ require "rdoc/task"
8
8
 
9
9
  RDoc::Task.new(:rdoc) do |rdoc|
10
- rdoc.rdoc_dir = 'rdoc'
11
- rdoc.title = 'SimpleFormRansack'
12
- rdoc.options << '--line-numbers'
13
- rdoc.rdoc_files.include('README.rdoc')
14
- rdoc.rdoc_files.include('lib/**/*.rb')
10
+ rdoc.rdoc_dir = "rdoc"
11
+ rdoc.title = "SimpleFormRansack"
12
+ rdoc.options << "--line-numbers"
13
+ rdoc.rdoc_files.include("README.rdoc")
14
+ rdoc.rdoc_files.include("lib/**/*.rb")
15
15
  end
16
16
 
17
-
18
-
19
-
20
17
  Bundler::GemHelper.install_tasks
21
-
@@ -17,7 +17,7 @@ module SimpleFormRansackHelper
17
17
  form_proxy = SimpleFormRansack::FormProxy.new(
18
18
  resource: resource,
19
19
  form: form,
20
- params: params,
20
+ params: params
21
21
  )
22
22
 
23
23
  yield form_proxy
@@ -1,6 +1,6 @@
1
1
  module SimpleFormRansack
2
2
  autoload :AttributeInspector, "simple_form_ransack/attribute_inspector"
3
- autoload :FormProxy, 'simple_form_ransack/form_proxy'
3
+ autoload :FormProxy, "simple_form_ransack/form_proxy"
4
4
  end
5
5
 
6
6
  require_relative "../app/helpers/simple_form_ransack_helper"
@@ -13,7 +13,7 @@ class SimpleFormRansack::AttributeInspector
13
13
  @current_clazz = @clazz
14
14
  @name_builtup = []
15
15
 
16
- has_attribute_directly = @clazz.attribute_names.select{ |name| name.to_s == @ransack_name.to_s }.any?
16
+ has_attribute_directly = @clazz.attribute_names.select { |name| name.to_s == @ransack_name.to_s }.any?
17
17
 
18
18
  research unless has_attribute_directly
19
19
  end
@@ -25,9 +25,11 @@ class SimpleFormRansack::AttributeInspector
25
25
 
26
26
  # The last part should be the attribute name.
27
27
  if index == @name_parts.length - 1
28
- if attribute_result = attribute_by_builtup
29
- puts "Attribute was: #{attribute_result[:name]}" if @debug
30
- @attribute = attribute_result[:name]
28
+ 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)
31
33
  break
32
34
  else
33
35
  puts "Not found: #{@name_builtup.join("_")}" if @debug
@@ -36,11 +38,12 @@ class SimpleFormRansack::AttributeInspector
36
38
  end
37
39
 
38
40
  # Try next - maybe next key need to be added? (which is common!)
39
- next unless reflection_result = reflection_by_builtup
41
+ reflection_result = reflection_by_builtup
42
+ next unless reflection_result
40
43
 
41
44
  @name_builtup = []
42
- name = reflection_result[:name]
43
- reflection = reflection_result[:reflection]
45
+ name = reflection_result.fetch(:name)
46
+ reflection = reflection_result.fetch(:reflection)
44
47
 
45
48
  puts "Name: #{name}" if @debug
46
49
  puts "Reflection: #{reflection}" if @debug
@@ -53,7 +56,7 @@ class SimpleFormRansack::AttributeInspector
53
56
  # Returns true if a complicated label could be generated and simple form should not do this itself.
54
57
  def generated_label?
55
58
  return true if @attribute && @generated_name_classes.any?
56
- return false
59
+ false
57
60
  end
58
61
 
59
62
  # Generates the complicated label and returns it.
@@ -61,7 +64,9 @@ class SimpleFormRansack::AttributeInspector
61
64
  name = ""
62
65
 
63
66
  if @generated_name_classes.last
64
- clazz, reflection = @generated_name_classes.last[:clazz], @generated_name_classes.last[:reflection]
67
+ clazz = @generated_name_classes.last.fetch(:clazz)
68
+ reflection = @generated_name_classes.last.fetch(:reflection)
69
+
65
70
  if reflection.collection?
66
71
  name << clazz.model_name.human(count: 2)
67
72
  else
@@ -72,22 +77,22 @@ class SimpleFormRansack::AttributeInspector
72
77
  name << " " unless name.empty?
73
78
  name << @current_clazz.human_attribute_name(@attribute).to_s.downcase
74
79
 
75
- return name
80
+ name
76
81
  end
77
82
 
78
83
  private
79
84
 
80
85
  def reflection_by_builtup
81
86
  total_name = @name_builtup.join("_")
82
- result = @current_clazz.reflections.select { |name, reflection| name.to_s == total_name }.first
87
+ result = @current_clazz.reflections.find { |name, _reflection| name.to_s == total_name }
83
88
  return {name: result[0], reflection: result[1]} if result
84
- return false
89
+ false
85
90
  end
86
91
 
87
92
  def attribute_by_builtup
88
93
  total_name = @name_builtup.join("_")
89
- result = @current_clazz.attribute_names.select { |name| name.to_s == total_name }.first
94
+ result = @current_clazz.attribute_names.find { |name| name.to_s == total_name }
90
95
  return {name: result} if result
91
- return false
96
+ false
92
97
  end
93
98
  end
@@ -1,10 +1,24 @@
1
1
  class SimpleFormRansack::FormProxy
2
+ def self.predicates_regex
3
+ unless @predicates_regex
4
+ predicates = Ransack::Configuration
5
+ .predicates
6
+ .map(&:first)
7
+ .map { |predicate| Regexp.escape(predicate) }
8
+ .join("|")
9
+
10
+ @predicates_regex = /^(.+)_(#{predicates})$/
11
+ end
12
+
13
+ @predicates_regex
14
+ end
15
+
2
16
  def initialize(args)
3
- @resource = args[:resource]
17
+ @resource = args.fetch(:resource)
4
18
  @object = @resource.object
5
19
  @class = @resource.klass
6
- @params = args[:params]
7
- @form = args[:form]
20
+ @params = args.fetch(:params)
21
+ @form = args.fetch(:form)
8
22
 
9
23
  raise "No params given in arguments: #{args.keys}" unless @params
10
24
  end
@@ -16,7 +30,7 @@ class SimpleFormRansack::FormProxy
16
30
  opts = {}
17
31
  end
18
32
 
19
- attribute_name = real_name(name, opts)
33
+ attribute_name = real_name(name)
20
34
  as = as_from_opts(attribute_name, opts)
21
35
  input_html = opts.delete(:input_html) || {}
22
36
  set_value(as, name, opts, input_html)
@@ -26,7 +40,7 @@ class SimpleFormRansack::FormProxy
26
40
  opts[:required] = false unless opts.key?(:required)
27
41
  opts[:input_html] = input_html
28
42
  args << opts
29
- return @form.input(attribute_name, *args)
43
+ @form.input(attribute_name, *args)
30
44
  end
31
45
 
32
46
  def method_missing(method_name, *args, &blk)
@@ -35,13 +49,14 @@ class SimpleFormRansack::FormProxy
35
49
 
36
50
  private
37
51
 
38
- def set_label(attribute_name, opts, input_html)
39
- if !opts.key?(:label)
52
+ def set_label(attribute_name, opts, _input_html)
53
+ unless opts.key?(:label)
40
54
  attribute_inspector = ::SimpleFormRansack::AttributeInspector.new(
41
55
  name: attribute_name,
42
56
  instance: @object,
43
57
  clazz: @class
44
58
  )
59
+
45
60
  if attribute_inspector.generated_label?
46
61
  opts[:label] = attribute_inspector.generated_label
47
62
  end
@@ -49,15 +64,15 @@ private
49
64
  end
50
65
 
51
66
  def set_name(as, name, input_html)
52
- unless input_html.key?(:name)
53
- input_html[:name] = "q[#{name}]"
54
- input_html[:name] << "[]" if as == "check_boxes"
55
- end
67
+ return if input_html.key?(:name)
68
+
69
+ input_html[:name] = "q[#{name}]"
70
+ input_html[:name] << "[]" if as == "check_boxes"
56
71
  end
57
72
 
58
73
  def set_value(as, name, opts, input_html)
59
74
  if as == "select"
60
- if !opts.key?(:selected)
75
+ unless opts.key?(:selected)
61
76
  if @params[name]
62
77
  opts[:selected] = @params[name]
63
78
  else
@@ -65,7 +80,7 @@ private
65
80
  end
66
81
  end
67
82
  elsif as == "check_boxes" || as == "radio_buttons"
68
- if !opts.key?(:checked)
83
+ unless opts.key?(:checked)
69
84
  if @params[name]
70
85
  opts[:checked] = @params[name]
71
86
  else
@@ -73,15 +88,11 @@ private
73
88
  end
74
89
  end
75
90
  elsif as == "boolean"
76
- if !input_html.key?(:checked)
77
- if @params[name] == "1"
78
- input_html[:checked] = "checked"
79
- else
80
- input_html[:checked] = nil
81
- end
91
+ unless input_html.key?(:checked)
92
+ input_html[:checked] = ("checked" if @params[name] == "1")
82
93
  end
83
94
  else
84
- if !input_html.key?(:value)
95
+ unless input_html.key?(:value)
85
96
  if @params[name]
86
97
  input_html[:value] = @params[name]
87
98
  else
@@ -92,33 +103,26 @@ private
92
103
  end
93
104
 
94
105
  def as_list?(opts)
95
- if as_from_opts(opts) == "select"
96
- return true
97
- else
98
- return false
99
- end
106
+ return true if as_from_opts(opts) == "select"
107
+ false
100
108
  end
101
109
 
102
110
  def as_from_opts(attribute_name, opts)
103
111
  if opts[:as].present?
104
- return opts[:as].to_s
105
- elsif opts[:collection] || attribute_name.end_with?('country')
112
+ return opts.fetch(:as).to_s
113
+ elsif opts[:collection] || attribute_name.end_with?("country")
106
114
  return "select"
107
115
  end
108
116
 
109
- if column = @class.columns_hash[attribute_name]
110
- if column.type == :boolean
111
- return "boolean"
112
- end
113
- end
117
+ column = @class.columns_hash[attribute_name]
118
+ return "boolean" if column && column.type == :boolean
114
119
 
115
- return "text"
120
+ "text"
116
121
  end
117
122
 
118
- def real_name(name, opts)
119
- predicates = Ransack::Configuration.predicates.map(&:first).
120
- map { |predicate| Regexp.escape(predicate) }.join("|")
121
- match = name.to_s.match(/^(.+)_(#{predicates})$/)
123
+ def real_name(name)
124
+ match = name.to_s.match(SimpleFormRansack::FormProxy.predicates_regex)
125
+
122
126
  if match
123
127
  return match[1]
124
128
  else
@@ -1,3 +1,3 @@
1
1
  module SimpleFormRansack
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.11"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_form_ransack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Johansen
@@ -150,6 +150,20 @@ dependencies:
150
150
  - - ">="
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
153
167
  description: Makes it easy to use SimpleForm::FormBuilder with Ransack without constantly
154
168
  having to supply labels and other pains.
155
169
  email: