Class: SimpleFormRansack::AttributeInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_form_ransack/attribute_inspector.rb

Overview

Inpect the model namespace for labels based on Ransack-names in order to generate labels for inputs.

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ AttributeInspector

Returns a new instance of AttributeInspector.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/simple_form_ransack/attribute_inspector.rb', line 3

def initialize(args)
  @args = args
  @ransack_name = args[:name]
  @name_parts = @ransack_name.to_s.split("_")
  @instance = args[:instance]
  @clazz = args[:clazz]
  @debug = args[:debug]

  @generated_name_classes = []
  @models = []
  @current_clazz = @clazz
  @name_builtup = []

  has_attribute_directly = @clazz.attribute_names.select { |name| name.to_s == @ransack_name.to_s }.any?

  research unless has_attribute_directly
end

Instance Method Details

#generated_labelObject

Generates the complicated label and returns it.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/simple_form_ransack/attribute_inspector.rb', line 63

def generated_label
  name = ""

  if @generated_name_classes.last
    clazz = @generated_name_classes.last.fetch(:clazz)
    reflection = @generated_name_classes.last.fetch(:reflection)

    if reflection.collection?
      name << clazz.model_name.human(count: 2)
    else
      name << clazz.model_name.human
    end
  end

  name << " " unless name.empty?
  name << @current_clazz.human_attribute_name(@attribute).to_s.downcase

  name
end

#generated_label?Boolean

Returns true if a complicated label could be generated and simple form should not do this itself.

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/simple_form_ransack/attribute_inspector.rb', line 57

def generated_label?
  return true if @attribute && @generated_name_classes.any?
  false
end

#researchObject

Loop through the name parts and inspectors reflections with it.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/simple_form_ransack/attribute_inspector.rb', line 22

def research
  @name_parts.each_with_index do |name_part, index|
    @name_builtup << name_part

    # The last part should be the attribute name.
    if index == @name_parts.length - 1
      attribute_result = attribute_by_builtup

      if attribute_result
        puts "Attribute was: #{attribute_result.fetch(:name)}" if @debug
        @attribute = attribute_result.fetch(:name)
        break
      else
        puts "Not found: #{@name_builtup.join("_")}" if @debug
        next
      end
    end

    # Try next - maybe next key need to be added? (which is common!)
    reflection_result = reflection_by_builtup
    next unless reflection_result

    @name_builtup = []
    name = reflection_result.fetch(:name)
    reflection = reflection_result.fetch(:reflection)

    puts "Name: #{name}" if @debug
    puts "Reflection: #{reflection}" if @debug

    @current_clazz = reflection.klass
    @generated_name_classes << {clazz: @current_clazz, reflection: reflection}
  end
end