Class: SimpleFormRansack::AttributeInspector
- Inherits:
-
Object
- Object
- SimpleFormRansack::AttributeInspector
- 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
-
#generated_label ⇒ Object
Generates the complicated label and returns it.
-
#generated_label? ⇒ Boolean
Returns true if a complicated label could be generated and simple form should not do this itself.
-
#initialize(args) ⇒ AttributeInspector
constructor
A new instance of AttributeInspector.
-
#research ⇒ Object
Loop through the name parts and inspectors reflections with it.
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 |
# File 'lib/simple_form_ransack/attribute_inspector.rb', line 3 def initialize(args) @args = args @ransack_name = args.fetch(:name) @name_parts = @ransack_name.to_s.split("_") @instance = args.fetch(:instance) @clazz = args.fetch(:clazz) @as = args.fetch(:as) @debug = args[:debug] @generated_name_classes = [] @models = [] @current_clazz = @clazz @name_builtup = [] research unless attribute_directly? end |
Instance Method Details
#generated_label ⇒ Object
Generates the complicated label and returns it.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/simple_form_ransack/attribute_inspector.rb', line 52 def generated_label @generated_label = "" if @generated_name_classes.last clazz = @generated_name_classes.last.fetch(:clazz) reflection = @generated_name_classes.last.fetch(:reflection) if reflection.collection? @generated_label << clazz.model_name.human(count: 2) else @generated_label << clazz.model_name.human end end add_id_to_generated_label @generated_label end |
#generated_label? ⇒ Boolean
Returns true if a complicated label could be generated and simple form should not do this itself.
46 47 48 49 |
# File 'lib/simple_form_ransack/attribute_inspector.rb', line 46 def generated_label? return true if @attribute && @generated_name_classes.any? false end |
#research ⇒ Object
Loop through the name parts and inspectors reflections with it.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/simple_form_ransack/attribute_inspector.rb', line 21 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 next unless attribute_result @attribute = attribute_result.fetch(:name) break end # Try next - maybe next key need to be added? (which is common!) reflection_result = reflection_by_builtup next unless reflection_result @name_builtup = [] reflection = reflection_result.fetch(:reflection) @current_clazz = reflection.klass @generated_name_classes << {clazz: @current_clazz, reflection: reflection} end end |