Sha256: 9c9a4896e67c0a837add3c5cf70901c7a4ba4fbe7b9bedc4dba67fd8c40afaa3

Contents?: true

Size: 1.23 KB

Versions: 13

Compression:

Stored size: 1.23 KB

Contents

module ActiveAdmin

  class Resource
    module Attributes

      def default_attributes
        resource_class.columns.each_with_object({}) do |c, attrs|
          unless reject_col?(c)
            name = c.name.to_sym
            attrs[name] = (method_for_column(name) || name)
          end
        end
      end

      def method_for_column(c)
        resource_class.respond_to?(:reflect_on_all_associations) && foreign_methods.has_key?(c) && foreign_methods[c].name.to_sym
      end

      def foreign_methods
        @foreign_methods ||= resource_class.reflect_on_all_associations.
          select { |r| r.macro == :belongs_to }.
          reject { |r| r.chain.length > 2 && !r.options[:polymorphic] }.
          index_by { |r| r.foreign_key.to_sym }
      end

      def reject_col?(c)
        primary_col?(c) || sti_col?(c) || counter_cache_col?(c) || filtered_col?(c)
      end

      def primary_col?(c)
        c.name == resource_class.primary_key
      end

      def sti_col?(c)
        c.name == resource_class.inheritance_column
      end

      def counter_cache_col?(c)
        c.name.end_with?('_count')
      end

      def filtered_col?(c)
        ActiveAdmin.application.filter_attributes.include?(c.name.to_sym)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
activeadmin-2.8.0 lib/active_admin/resource/attributes.rb
activeadmin-2.7.0 lib/active_admin/resource/attributes.rb
activeadmin-2.6.1 lib/active_admin/resource/attributes.rb
activeadmin-2.6.0 lib/active_admin/resource/attributes.rb
activeadmin-2.5.0 lib/active_admin/resource/attributes.rb
activeadmin-2.4.0 lib/active_admin/resource/attributes.rb
activeadmin-2.3.1 lib/active_admin/resource/attributes.rb
activeadmin-2.3.0 lib/active_admin/resource/attributes.rb
activeadmin-2.2.0 lib/active_admin/resource/attributes.rb
activeadmin-2.1.0 lib/active_admin/resource/attributes.rb
activeadmin-2.0.0 lib/active_admin/resource/attributes.rb
activeadmin-2.0.0.rc2 lib/active_admin/resource/attributes.rb
activeadmin-2.0.0.rc1 lib/active_admin/resource/attributes.rb