Sha256: 091828309791f5d5395034ebb005acd777fb8d392f10afe01021b9b4600aa3a4

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

require 'rails_admin/config/fields'
require 'rails_admin/config/fields/types'
require 'rails_admin/config/fields/types/belongs_to_association'

RailsAdmin::Config::Fields.register_factory do |parent, properties, fields|
  if association = parent.abstract_model.associations.find {|a| a[:foreign_key] == properties[:name] }
    field = RailsAdmin::Config::Fields::Types.load("#{association[:polymorphic] ? :polymorphic : :belongs_to}_association").new(parent, association[:name], association)
    fields << field

    child_columns = []
    id_column = parent.abstract_model.properties.find {|p| p[:name].to_s == association[:foreign_key].to_s }
    child_columns << RailsAdmin::Config::Fields.default_factory.call(parent, id_column, fields)

    if association[:polymorphic]
      type_colum = parent.abstract_model.properties.find {|p| p[:name].to_s == association[:foreign_type].to_s }
      child_columns << RailsAdmin::Config::Fields.default_factory.call(parent, type_colum, fields)
    end

    child_columns.each do |child_column|
      child_column.hide
      child_column.filterable(false)
    end

    field.children_fields child_columns.map(&:name)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
obitum-rails_admin-0.0.3 lib/rails_admin/config/fields/factories/belongs_to_association.rb