Sha256: ac9a848ec3a2a0889935baa55c9e377e715602145997eeb599491c4d5b9af786

Contents?: true

Size: 936 Bytes

Versions: 1

Compression:

Stored size: 936 Bytes

Contents

# encoding: UTF-8

# A dummy model used for general testing.
class CrudTestModel < ActiveRecord::Base #:nodoc:

  belongs_to :companion, class_name: 'CrudTestModel'
  has_and_belongs_to_many :others, class_name: 'OtherCrudTestModel'
  has_many :mores, class_name: 'OtherCrudTestModel',
                   foreign_key: :more_id

  before_destroy :protect_if_companion

  validates :name, presence: true
  validates :rating, inclusion: { in: 1..10 }

  def to_s
    name
  end

  def chatty
    remarks.size
  end

  private

  def protect_if_companion
    if companion.present?
      errors.add(:base, 'Cannot destroy model with companion')
      false
    end
  end

end

# Second dummy model to test associations.
class OtherCrudTestModel < ActiveRecord::Base #:nodoc:

  has_and_belongs_to_many :others, class_name: 'CrudTestModel'
  belongs_to :more, foreign_key: :more_id, class_name: 'CrudTestModel'

  def to_s
    name
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry_crud-3.0.0 lib/generators/dry_crud/templates/test/support/crud_test_model.rb