Module: Reports::NestedAttributeOptions
- Defined in:
- app/commands/reports/nested_attribute_options.rb
Class Method Summary collapse
- .expand_on ⇒ Object
- .generate_filter_options(klass, **options) ⇒ Object
-
.generate_select_options(klass, **_options) ⇒ Object
Generate select options (attributes) for all associations with nested attributes for the given class.
- .is_expanded?(resource) ⇒ Boolean
-
.is_nested?(attr) ⇒ Boolean
Check if the attribute is nested (i.e. sales_manager_1_name).
Class Method Details
.expand_on ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/commands/reports/nested_attribute_options.rb', line 117 def self. return @expand_on if @expand_on_memoized @expand_on_memoized = true @expand_on = @expand_on.call if @expand_on.is_a?(Proc) # Fallback only if @expand_on was never set if @expand_on.nil? && klass @expand_on = klass..keys end @expand_on = Array(@expand_on).map do |expansion| expansion.is_a?(Hash) ? expansion : { value: expansion, column_name: "type" } end end |
.generate_filter_options(klass, **options) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/commands/reports/nested_attribute_options.rb', line 59 def self.(klass, **) [].tap do |results| flattener_klass = ::Flattener.for(klass).pivot_or_self associations = flattener_klass.associations.lazy. select(&:is_nested_attributes?) associations. reject { |association| association.flattener.class.is_pivot? }. reject { |association| association.merge_into_parent }. select { |association| association.count >= 0 }. each do |association| resource = Flattener::Resource.new( flattener: association.flattener, association: association, parent: Flattener::Resource.new(flattener: Flattener.for(klass).new) ) association_root_name = association.name.to_s.singularize expansions = Array(association.klass).map do || .respond_to?(:name) ? .name : .to_s end expansions.each do |_expansion| resource.attributes.each do |attribute| child_column_name = (attribute.column.alias_name || attribute.column_name || attribute.column.name).to_s column_name = [association_root_name, *child_column_name].join("_").underscore column_type = ::Flattener::Column::Types.sql_type_to_type(attribute.column.sql_type) next if column_type == ::Flattener::Column::Types::JSON # skip json filter_option = ::Reports::FilterOptionGenerator.for(column_type). new(flattener_attr: attribute, attr: column_name, name: ::Reports::Generator.build_option_name(column_name, preserve_id: true), **). generate. tap do |o| o[:nestedResourceCount] = (association.count if association.count > 0) o[:associationName] = association.name.to_s if [::Flattener::Column::Types::DATE, ::Flattener::Column::Types::DATETIME].include?(column_type) o.merge!( filterOptionType: "BelongsTo", columnType: nil, operationType: nil, partialType: nil, ) end end results << filter_option end end end end end |
.generate_select_options(klass, **_options) ⇒ Object
Generate select options (attributes) for all associations with nested attributes for the given class.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 55 56 57 |
# File 'app/commands/reports/nested_attribute_options.rb', line 8 def self.(klass, **) [].tap do |results| flattener_klass = ::Flattener.for(klass).pivot_or_self associations = flattener_klass.associations.lazy.select(&:is_nested_attributes?) associations. reject { |association| association.klass.nil? }. reject { |association| association.flattener.class.is_pivot? }. reject { |association| association.merge_into_parent }. select { |association| association.count >= 0 }. each do |association| resource = Flattener::Resource.new( flattener: association.flattener, association: association, parent: Flattener::Resource.new(flattener: Flattener.for(klass).new) ) association_root_name = association.name.to_s.singularize expansions = Array(association.klass).map do || .respond_to?(:name) ? .name : .to_s end expansions.each do |_expansion| resource.attributes.each do |attribute| child_column_name = (attribute.column.alias_name || attribute.column_name || attribute.column.name).to_s column_name = [association_root_name, *child_column_name].join("_").underscore column_type = ::Flattener::Column::Types.sql_type_to_type(attribute.column.sql_type) next if column_type == ::Flattener::Column::Types::JSON # skip json result = { name: ::Reports::Generator.build_option_name(column_name, preserve_id: true), columnType: column_type.to_s, nestedResourceCount: (association.count if association.count > 0), associationName: association.name.to_s, associationColumnName: column_name, } if association. result[:associationColumnName] = [association_root_name, *child_column_name].join("_").underscore end if [::Flattener::Column::Types::DATE, ::Flattener::Column::Types::DATETIME].include?(column_type) result[:selectOptionType] = "BelongsTo" end results << result end end end end end |
.is_expanded?(resource) ⇒ Boolean
112 113 114 115 |
# File 'app/commands/reports/nested_attribute_options.rb', line 112 def self.(resource) return false unless resource.try :association resource.association.count > 0 || (resource.parent) end |
.is_nested?(attr) ⇒ Boolean
Check if the attribute is nested (i.e. sales_manager_1_name)
108 109 110 |
# File 'app/commands/reports/nested_attribute_options.rb', line 108 def self.is_nested?(attr) (attr.resource) end |