Back to Repositories

Testing Mongoid Field Types and File Attachments in rails_admin

This test file implements a comprehensive Mongoid document model for testing various field types and file attachments in Rails Admin. It validates the integration of multiple file upload solutions and complex field relationships.

Test Coverage Overview

The test suite provides extensive coverage for Mongoid document fields and attachments integration.

Key areas tested include:
  • Multiple field types (String, Array, BigDecimal, Boolean, Date, etc.)
  • File attachment systems (Paperclip, Shrine, Dragonfly, Carrierwave)
  • Nested relationships and embedded documents
  • Field validations and callbacks

Implementation Analysis

The testing approach implements a comprehensive field test model using Mongoid’s document-oriented architecture.

Notable patterns include:
  • Multiple file upload integrations
  • Nested attribute handling
  • Custom field type definitions
  • Polymorphic associations

Technical Details

Testing infrastructure includes:
  • Mongoid Document module
  • Multiple file upload libraries (Paperclip, Shrine, Dragonfly, Carrierwave)
  • ActiveModel validations
  • BSON data types
  • Custom field mappings

Best Practices Demonstrated

The test implementation showcases several best practices in MongoDB testing:

  • Comprehensive field type coverage
  • Proper relationship modeling
  • Multiple upload solution integration
  • Validation and callback implementation
  • Clean separation of concerns

railsadminteam/rails_admin

spec/dummy_app/app/mongoid/field_test.rb

            
# frozen_string_literal: true

require 'rails_admin/adapters/mongoid'

class FieldTest
  include Mongoid::Document
  include Mongoid::Paperclip
  include ActiveModel::ForbiddenAttributesProtection
  extend Dragonfly::Model

  field :name, type: String
  field :title, type: String
  field :subject, type: String
  field :short_text, type: String

  field :array_field, type: Array
  field :big_decimal_field, type: BigDecimal
  field :boolean_field, type: Boolean
  field :bson_object_id_field, type: RailsAdmin::Adapters::Mongoid::Bson::OBJECT_ID
  field :bson_binary_field, type: BSON::Binary
  field :date_field, type: Date
  field :datetime_field, type: DateTime
  field :time_with_zone_field, type: ActiveSupport::TimeWithZone
  field :default_field
  field :float_field, type: Float
  field :hash_field, type: Hash
  field :integer_field, type: Integer
  field :object_field, type: Object
  field :range_field, type: Range
  field :string_field, type: String
  field :symbol_field, type: Symbol
  field :text_field, type: String
  field :time_field, type: Time

  field :format, type: String
  field :open, type: Boolean
  field :restricted_field, type: String
  field :protected_field, type: String
  field :al, as: :aliased_field, type: String
  has_mongoid_attached_file :paperclip_asset, styles: {thumb: '100x100>'}

  field :shrine_asset_data, type: String
  include ShrineUploader::Attachment.new(:shrine_asset)
  field :shrine_versioning_asset_data, type: String
  include ShrineVersioningUploader::Attachment.new(:shrine_versioning_asset)

  has_many :nested_field_tests, dependent: :destroy, inverse_of: :field_test, autosave: true
  accepts_nested_attributes_for :nested_field_tests, allow_destroy: true

  # on creation, comment is not saved without autosave: true
  has_one :comment, as: :commentable, autosave: true
  accepts_nested_attributes_for :comment, allow_destroy: true

  embeds_many :embeds
  accepts_nested_attributes_for :embeds, allow_destroy: true

  attr_accessor :delete_paperclip_asset

  before_validation { self.paperclip_asset = nil if delete_paperclip_asset == '1' }

  field :dragonfly_asset_name
  field :dragonfly_asset_uid
  dragonfly_accessor :dragonfly_asset

  mount_uploader :carrierwave_asset, CarrierwaveUploader
  # carrierwave-mongoid does not support mount_uploaders yet:
  #   https://github.com/carrierwaveuploader/carrierwave-mongoid/issues/138
  mount_uploaders :carrierwave_assets, CarrierwaveUploader

  validates :short_text, length: {maximum: 255}
end