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



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
80
81
82
83
84
85
# File 'lib/spotlight_search/exportable_columns_v2.rb', line 45

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

#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
34
35
36
37
38
39
40
41
42
43
# File 'lib/spotlight_search/exportable_columns_v2.rb', line 31

def export_columns(*record_fields, **associated_fields)
  ActiveRecord::Base.connection.migration_context.needs_migration? && return

  # Check that all record fields are valid accessible. Error if it doesn't.
  # for each association, check that if its a valid association, and take the recursive step with that association
  # End result is setting up in self, enabled columns and enabled associated columns
  columns_hash = _model_exportable_columns(self, *record_fields, **associated_fields)

  raise SpotlightSearch::Exceptions::InvalidColumns, columns_hash[:invalid_columns] if columns_hash[:invalid_columns].size.positive?
  self.enabled_columns = [*record_fields, **associated_fields]
rescue ActiveRecord::NoDatabaseError
  Rails.logger.info("No database error")
end

#validate_exportable_columns(columns) ⇒ Object

Validates whether the selected columns are allowed for export



88
89
90
91
92
93
94
# File 'lib/spotlight_search/exportable_columns_v2.rb', line 88

def validate_exportable_columns(columns)
  unless columns.is_a?(Array)
    raise SpotlightSearch::Exceptions::InvalidValue, 'Expected Array. Invalid type received'
  end

  true
end