Class: Adminable::FieldCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/adminable/field_collector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ FieldCollector

Returns a new instance of FieldCollector.

Parameters:

  • model (ActiveRecord::Base)

    activerecord model class



13
14
15
16
# File 'lib/adminable/field_collector.rb', line 13

def initialize(model)
  @model = model
  @all ||= columns + associations
end

Instance Attribute Details

#allArray (readonly)

Returns fields from activerecord model.

Returns:

  • (Array)

    fields from activerecord model



10
11
12
# File 'lib/adminable/field_collector.rb', line 10

def all
  @all
end

#modelActiveRecord::Base (readonly)

Returns activerecord model class.

Examples:

Adminable::Fields::Collection.new(User).model
# => User(id: integer, email: string, password_hash: string)

Returns:

  • (ActiveRecord::Base)

    activerecord model class



7
8
9
# File 'lib/adminable/field_collector.rb', line 7

def model
  @model
end

Instance Method Details

#associationsArray

Collects fields from model associations

Returns:

  • (Array)


30
31
32
33
34
35
36
# File 'lib/adminable/field_collector.rb', line 30

def associations
  @associations ||= [].tap do |fields|
    @model.reflect_on_all_associations.each do |association|
      fields << resolve(association.macro, association.name)
    end
  end
end

#columnsArray

Collects fields from model columns

Returns:

  • (Array)


20
21
22
23
24
25
26
# File 'lib/adminable/field_collector.rb', line 20

def columns
  @columns ||= [].tap do |fields|
    @model.columns.reject { |a| a.name.match(/_id$/) }.each do |column|
      fields << resolve(column.type, column.name)
    end
  end
end