Sha256: 547cfa497abdf6cfefcfed8acc8315d1baefe666acb8d206f2a33ca00f43f259

Contents?: true

Size: 1.28 KB

Versions: 13

Compression:

Stored size: 1.28 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] && [:belongs_to, :has_and_belongs_to_many].include?(a[:type]) }
    field = RailsAdmin::Config::Fields::Types.load("#{association[:polymorphic] ? :polymorphic : association[:type]}_association").new(parent, association[:name], association)
    fields << field

    child_columns = []
    possible_field_names =
      if association[:polymorphic]
        [:foreign_key, :foreign_type, :foreign_inverse_of]
      else
        [:foreign_key]
      end.map{|k| association[k] }.compact

    parent.abstract_model.properties.select{|p| possible_field_names.include? p[:name] }.each do |column|
      unless child_field = fields.find{|f| f.name.to_s == column[:name].to_s }
        child_field = RailsAdmin::Config::Fields.default_factory.call(parent, column, fields)
      end
      child_columns << child_field
    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

13 entries across 13 versions & 2 rubygems

Version Path
rails_admin-0.6.0 lib/rails_admin/config/fields/factories/association.rb
rails_admin-0.5.0 lib/rails_admin/config/fields/factories/association.rb
rails_admin-0.4.9 lib/rails_admin/config/fields/factories/association.rb
rails_admin-0.4.8 lib/rails_admin/config/fields/factories/association.rb
rails_admin-0.4.7 lib/rails_admin/config/fields/factories/association.rb
rails_admin-0.4.6 lib/rails_admin/config/fields/factories/association.rb
rails_admin-0.4.5 lib/rails_admin/config/fields/factories/association.rb
rails_admin-0.4.4 lib/rails_admin/config/fields/factories/association.rb
rails_admin-lbgraham-0.4.2 lib/rails_admin/config/fields/factories/association.rb
rails_admin-0.4.3 lib/rails_admin/config/fields/factories/association.rb
rails_admin-0.4.2 lib/rails_admin/config/fields/factories/association.rb
rails_admin-0.4.1 lib/rails_admin/config/fields/factories/association.rb
rails_admin-0.4.0 lib/rails_admin/config/fields/factories/association.rb