Module: SpotlightSearch::ExportableColumnsV2::ClassMethods

Defined in:
lib/spotlight_search/exportable_columns_v2.rb

Instance Method Summary collapse

Instance Method Details

#_model_exportable_columns(klass, *record_fields, **associated_fields) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/spotlight_search/exportable_columns_v2.rb', line 39

def _model_exportable_columns(klass, *record_fields, **associated_fields)
  # Gets all the valid columns of a model
  # If any column is invalid, it also returns it
  raise SpotlightSearch::Exceptions::InvalidValue, "Expected ActiveRecord::Base, Received #{klass}" unless klass < ActiveRecord::Base

  valid_columns = []
  invalid_columns = []

  # Base case: verify all the columns that belong to this record
  record_fields.each do |field|
    klass.new.respond_to?(field) ? valid_columns << field : invalid_columns << field
  end

  # Recursive case: check all associations and verify that they are all valid too
  associated_fields.each do |association, association_record_fields|
    reflection = klass.reflect_on_association(association)
    invalid_columns << association && next unless reflection # Add whole association to invalid columns if it doesn't exist

    case reflection
    when ActiveRecord::Reflection::BelongsToReflection, ActiveRecord::Reflection::HasOneReflection
      if reflection.polymorphic?
        # We cannot process them further, so we'll assume it works and call it a day
        valid_columns << { association => association_record_fields }
      else
        columns_hash = _model_exportable_columns(reflection.klass, *association_record_fields)
        valid_columns << { association => columns_hash[:valid_columns] }
        invalid_columns << { association => columns_hash[:invalid_columns] } if columns_hash[:invalid_columns].size.positive?
      end
    else
      # one to many relationshops cannot be supported
      invalid_columns << association
      next
    end
  end

  # return all the valid and invalid columns in a hash
  {
    valid_columns: valid_columns,
    invalid_columns: invalid_columns
  }
end

#default_scopes_for_export(*filter_scopes) ⇒ Object



35
36
37
# File 'lib/spotlight_search/exportable_columns_v2.rb', line 35

def default_scopes_for_export(*filter_scopes)
  self.default_filters = filter_scopes
end

#export_columns(*record_fields, **associated_fields) ⇒ Object

Enables or disables export and specifies which all columns can be exported. For enabling export for all columns in all models

class ApplicationRecord < ActiveRecord::Base
  export_columns enabled: true
end

For disabling export for only specific models

class Person < ActiveRecord::Base
  export_columns enabled: false
end

For allowing export for only specific columns in a model

class Person < ActiveRecord::Base
  export_columns enabled: true, only: [:created_at, :updated_at]
end

For excluding only specific columns and allowing all others

class Person < ActiveRecord::Base
  export_columns enabled: true, except: [:created_at, :updated_at]
end


31
32
33
# File 'lib/spotlight_search/exportable_columns_v2.rb', line 31

def export_columns(*record_fields, **associated_fields)
  self.enabled_columns = [*record_fields, **associated_fields]
end

#validate_exportable_columns(columns) ⇒ Object

Validates whether the selected columns are allowed for export



82
83
84
85
86
87
# File 'lib/spotlight_search/exportable_columns_v2.rb', line 82

def validate_exportable_columns(columns)
  unless columns.is_a? Array
    raise SpotlightSearch::Exceptions::InvalidValue, 'Expected Array. Invalid type received'
  end
  (columns & SpotlightSearch::Utils.serialize_csv_columns(*enabled_columns)).size == columns.size # returns true if all columns are valid
end