Class: Adminable::Attributes::Collection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Collection

Returns a new instance of Collection.



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

def initialize(model)
  @model = model
end

Instance Attribute Details

#formObject



15
16
17
# File 'lib/adminable/attributes/collection.rb', line 15

def form
  @form ||= all.except(:id, :created_at, :updated_at)
end

#indexObject



11
12
13
# File 'lib/adminable/attributes/collection.rb', line 11

def index
  @index ||= all.except(:created_at, :updated_at)
end

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/adminable/attributes/collection.rb', line 4

def model
  @model
end

Instance Method Details

#allObject



24
25
26
# File 'lib/adminable/attributes/collection.rb', line 24

def all
  [columns, belongs_to, has_many].inject(&:merge)
end

#associationObject



28
29
30
# File 'lib/adminable/attributes/collection.rb', line 28

def association
  [belongs_to, has_many].inject(&:merge)
end

#belongs_toObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/adminable/attributes/collection.rb', line 44

def belongs_to
  @belongs_to ||= {}.tap do |attribute|
    @model.reflect_on_all_associations(:belongs_to).each do |association|
      attribute[association.name] = Adminable::Attributes::Types::BelongsTo.new(
        association.name,
        required: attribute_required?(association.name),
        association: association
      )
    end
  end.symbolize_keys
end

#columnsObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/adminable/attributes/collection.rb', line 32

def columns
  @columns ||= {}.tap do |attribute|
    @model.columns.reject { |a| a.name.match(/_id$/) }.each do |column|
      attribute[column.name] = "adminable/attributes/types/#{column.type}"
        .classify.constantize.new(
          column.name,
          required: attribute_required?(column.name)
        )
    end
  end.symbolize_keys
end

#has_manyObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/adminable/attributes/collection.rb', line 56

def has_many
  @has_many ||= {}.tap do |attribute|
    @model.reflect_on_all_associations(:has_many).each do |association|
      attribute[association.name] = Adminable::Attributes::Types::HasMany.new(
        association.name,
        required: attribute_required?(association.name),
        association: association
      )
    end
  end.symbolize_keys
end

#ransackObject



19
20
21
22
# File 'lib/adminable/attributes/collection.rb', line 19

def ransack
  @ransack ||= columns.except(:id, :created_at, :updated_at)
                      .select { |k, v| %w(string text).include?(v.type) }
end