simple_form_ransack 0.0.3 → 0.0.4

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: 908f1e7b0caf9a74d9afd4ace8b8eb2bd59f4662
4
- data.tar.gz: 733e44844b4490f02a83c9796763a55bc897abf7
3
+ metadata.gz: c46e179d53d0bffde988759fa5b1b374a2d486bc
4
+ data.tar.gz: 13ec406b32985efe8f16a6351b599693f719df11
5
5
  SHA512:
6
- metadata.gz: 97128aa38fa68a4dfed2bc346ea2f6496b5f48e5bc9f74a0977704469a6badb9711f87aeb51fbb6f800154e4988f754bf03a90db56ca69b29636a9408e3928a3
7
- data.tar.gz: 0cf66c6a840b84598afab7d486da5c44e9269bc168929ff4e024a53af3afbe9d9b1e57673b1a418e9467205cde9fde63171e3b7fc86ae23f76446432fa3b6c79
6
+ metadata.gz: 397ece886b2a4f2321218301f1e2d53b40fd2b003148257754d23db5861a0bdd7d36f0c3734436bbd0370aa9704be5eb158076998407cef48293ec4fe7cf57cf
7
+ data.tar.gz: f6a1db27fe68971c6dc2abba089b5815fe13c2c52c00151bc7baebed17377ff70894c0c474e5106ad048ec8e7610f4286fa0c44bbbc69213261032dfc6ba058f
@@ -1,4 +1,5 @@
1
1
  module SimpleFormRansack
2
+ autoload :AttributeInspector, "simple_form_ransack/attribute_inspector"
2
3
  end
3
4
 
4
5
  require_relative "../app/helpers/simple_form_ransack_helper"
@@ -0,0 +1,93 @@
1
+ # Inpect the model namespace for labels based on Ransack-names in order to generate labels for inputs.
2
+ class SimpleFormRansack::AttributeInspector
3
+ def initialize(args)
4
+ @args = args
5
+ @ransack_name = args[:name]
6
+ @name_parts = @ransack_name.to_s.split("_")
7
+ @instance = args[:instance]
8
+ @clazz = args[:clazz]
9
+ @debug = args[:debug]
10
+
11
+ @generated_name_classes = []
12
+ @models = []
13
+ @current_clazz = @clazz
14
+ @name_builtup = []
15
+
16
+ has_attribute_directly = @clazz.attribute_names.select{ |name| name.to_s == @ransack_name.to_s }.any?
17
+
18
+ research unless has_attribute_directly
19
+ end
20
+
21
+ # Loop through the name parts and inspectors reflections with it.
22
+ def research
23
+ @name_parts.each_with_index do |name_part, index|
24
+ @name_builtup << name_part
25
+
26
+ # The last part should be the attribute name.
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]
31
+ break
32
+ else
33
+ puts "Not found: #{@name_builtup.join("_")}" if @debug
34
+ next
35
+ end
36
+ end
37
+
38
+ # Try next - maybe next key need to be added? (which is common!)
39
+ next unless reflection_result = reflection_by_builtup
40
+
41
+ @name_builtup = []
42
+ name = reflection_result[:name]
43
+ reflection = reflection_result[:reflection]
44
+
45
+ puts "Name: #{name}" if @debug
46
+ puts "Reflection: #{reflection}" if @debug
47
+
48
+ @current_clazz = reflection.klass
49
+ @generated_name_classes << {:clazz => @current_clazz, :reflection => reflection}
50
+ end
51
+ end
52
+
53
+ # Returns true if a complicated label could be generated and simple form should not do this itself.
54
+ def generated_label?
55
+ return true if @attribute && @generated_name_classes.any?
56
+ return false
57
+ end
58
+
59
+ # Generates the complicated label and returns it.
60
+ def generated_label
61
+ name = ""
62
+
63
+ if @generated_name_classes.last
64
+ clazz, reflection = @generated_name_classes.last[:clazz], @generated_name_classes.last[:reflection]
65
+ if reflection.collection?
66
+ name << clazz.model_name.human(:count => 2)
67
+ else
68
+ name << clazz.model_name.human
69
+ end
70
+ end
71
+
72
+ name << " " unless name.empty?
73
+ name << @current_clazz.human_attribute_name(@attribute).to_s.downcase
74
+
75
+ return name
76
+ end
77
+
78
+ private
79
+
80
+ def reflection_by_builtup
81
+ total_name = @name_builtup.join("_")
82
+ result = @current_clazz.reflections.select{ |name, reflection| name.to_s == total_name }.first
83
+ return {:name => result[0], :reflection => result[1]} if result
84
+ return false
85
+ end
86
+
87
+ def attribute_by_builtup
88
+ total_name = @name_builtup.join("_")
89
+ result = @current_clazz.attribute_names.select{ |name| name.to_s == total_name }.first
90
+ return {:name => result} if result
91
+ return false
92
+ end
93
+ end
@@ -18,19 +18,13 @@ class SimpleFormRansack::FormProxy
18
18
 
19
19
  attribute_name = real_name(name, opts)
20
20
  as = as_from_opts(opts)
21
-
22
21
  input_html = opts.delete(:input_html) || {}
23
- input_html[:name] = "q[#{name}]" unless input_html.key?(:name)
24
-
25
- if as == "select"
26
- opts[:selected] = @params[name] if !input_html.key?(:selected) && @params[name]
27
- else
28
- input_html[:value] = @params[name] if !input_html.key?(:value) && @params[name]
29
- end
22
+ set_value(as, name, opts, input_html)
23
+ set_name(as, name, input_html)
24
+ set_label(attribute_name, opts, input_html)
30
25
 
31
- opts[:input_html] = input_html
32
26
  opts[:required] = false unless opts.key?(:required)
33
-
27
+ opts[:input_html] = input_html
34
28
  args << opts
35
29
  return @form.input(attribute_name, *args)
36
30
  end
@@ -41,6 +35,62 @@ class SimpleFormRansack::FormProxy
41
35
 
42
36
  private
43
37
 
38
+ def set_label(attribute_name, opts, input_html)
39
+ if !opts.key?(:label)
40
+ attribute_inspector = ::SimpleFormRansack::AttributeInspector.new(
41
+ :name => attribute_name,
42
+ :instance => @object,
43
+ :clazz => @class
44
+ )
45
+ if attribute_inspector.generated_label?
46
+ opts[:label] = attribute_inspector.generated_label
47
+ end
48
+ end
49
+ end
50
+
51
+ 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
56
+ end
57
+
58
+ def set_value(as, name, opts, input_html)
59
+ if as == "select"
60
+ if !opts.key?(:selected)
61
+ if @params[name]
62
+ opts[:selected] = @params[name]
63
+ else
64
+ opts[:selected] = ""
65
+ end
66
+ end
67
+ elsif as == "check_boxes" || as == "radio_buttons"
68
+ if !opts.key?(:checked)
69
+ if @params[name]
70
+ opts[:checked] = @params[name]
71
+ else
72
+ opts[:checked] = ""
73
+ end
74
+ end
75
+ else
76
+ if !input_html.key?(:value)
77
+ if @params[name]
78
+ input_html[:value] = @params[name]
79
+ else
80
+ input_html[:value] = ""
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ def as_list?(opts)
87
+ if as_from_opts(opts) == "select"
88
+ return true
89
+ else
90
+ return false
91
+ end
92
+ end
93
+
44
94
  def as_from_opts(opts)
45
95
  if opts[:as].present?
46
96
  return opts[:as].to_s
@@ -52,7 +102,7 @@ private
52
102
  end
53
103
 
54
104
  def real_name(name, opts)
55
- match = name.to_s.match(/^(.+)_(eq|cont|eq_any)$/)
105
+ match = name.to_s.match(/^(.+)_(eq|cont|eq_any|gteq|lteq|gt|lt)$/)
56
106
  if match
57
107
  return match[1]
58
108
  else
@@ -1,3 +1,3 @@
1
1
  module SimpleFormRansack
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Johansen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-21 00:00:00.000000000 Z
11
+ date: 2014-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -134,6 +134,7 @@ files:
134
134
  - lib/simple_form_ransack.rb
135
135
  - lib/tasks/simple_form_ransack_tasks.rake
136
136
  - lib/simple_form_ransack/version.rb
137
+ - lib/simple_form_ransack/attribute_inspector.rb
137
138
  - lib/simple_form_ransack/form_proxy.rb
138
139
  - MIT-LICENSE
139
140
  - Rakefile