module EmailNormalization
extend ActiveSupport::Concern
included do
attr_accessor :soft_warnings
class SlugValidator < ActiveModel::Validator
SLUG_REGEX = /\A[a-z0-9]+(?:-[a-z0-9]+)*\z/
def validate(record)
slug = record.send(options[:attribute] || :slug)
# app/validators/order_validator.rb
class OrderValidator < ActiveModel::Validator
def validate(record)
validate_order_total(record)
validate_items_availability(record)
validate_shipping_address(record)
class AtLeastOneOfValidator < ActiveModel::Validator
def validate(record)
fields = Array(options[:fields])
raise ArgumentError, "provide :fields" if fields.empty?
return if fields.any? { |field| filled?(record.public_send(field)) }
class Category < ApplicationRecord
has_many :products, dependent: :restrict_with_error
has_many :subcategories,
class_name: "Category",
foreign_key: :parent_id,
dependent: :restrict_with_error
class Order < ApplicationRecord
has_many :line_items, dependent: :destroy, inverse_of: :order
accepts_nested_attributes_for :line_items,
reject_if: :all_blank,
allow_destroy: true
class Order < ApplicationRecord
has_many :line_items, inverse_of: :order, dependent: :destroy
accepts_nested_attributes_for :line_items,
allow_destroy: true,
reject_if: ->(attrs) { attrs["product_id"].blank? && attrs["quantity"].blank? }
class Order < ApplicationRecord
class InvalidTransition < StandardError; end
enum status: { pending: 0, paid: 1, shipped: 2, cancelled: 3 }
has_many :order_transitions, -> { order(:created_at) }, dependent: :destroy