Class: Nazar::Formatter::ActiveRecordCollection

Inherits:
Object
  • Object
show all
Includes:
ActiveRecordInterface
Defined in:
lib/nazar/formatter/active_record_collection.rb

Instance Attribute Summary

Attributes included from ActiveRecordInterface

#attributes, #collection, #klass

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveRecordInterface

#cells, #headers, #valid?

Constructor Details

#initialize(collection) ⇒ ActiveRecordCollection

Returns a new instance of ActiveRecordCollection.



10
11
12
13
14
15
16
# File 'lib/nazar/formatter/active_record_collection.rb', line 10

def initialize(collection)
  @collection = collection
  @collection.load if @collection.respond_to?(:loaded?) && !@collection.loaded?

  @attributes = collection.first&.attributes
  @klass = collection.first&.class
end

Class Method Details

.valid?(data) ⇒ Boolean

rubocop:disable Metrics/CyclomaticComplexity

Returns:

  • (Boolean)


18
19
20
21
22
23
24
# File 'lib/nazar/formatter/active_record_collection.rb', line 18

def self.valid?(data) # rubocop:disable Metrics/CyclomaticComplexity
  return false if data.is_a?(::Struct) || (defined?(OpenStruct) && data.is_a?(OpenStruct))

  data.is_a?(ActiveRecord::Associations::CollectionProxy) ||
    data.is_a?(ActiveRecord::Relation) ||
    (data.is_a?(Enumerable) && !data.is_a?(Range) && data.first.is_a?(ActiveRecord::Base))
end

Instance Method Details

#summaryObject



26
27
28
# File 'lib/nazar/formatter/active_record_collection.rb', line 26

def summary
  collection.size
end