0
\$\begingroup\$

I've created a Marketplace application on ruby on rails and would like to request a review of my code (Order Model) to check if I am in the right track or not...please kindly understand that I am a super webdev rookie and would really appreciate the kindness of the tech community to get useful advice and/or guidance. Many thanks.

class Order < ApplicationRecord
  before_validation :ensure_login_has_a_value

  validates :name, presence: true
  validates :last_name, presence: true
  validates :mobile_number, presence: true
  validates :address, presence: true
  validates :total, presence: true,  numericality: {greater_than_or_equal_to: 1.50}

  after_validation :create_order_profile

  belongs_to :user
  has_many :order_articles, dependent: :destroy
  has_many :articles, through: 'order_articles'
  enum :status, %i[:complete, :incomplete]

  private

  def ensure_login_has_a_value
    if login.nil?
      self.login = email unless email.blank?
    end
  end

  def add_article(product, quantity)
    order_article = order_articles.find(article: article_description)
    order_article.update(quantity: order_article.quantity + quantity, price: article.price)
  end

  def subtotal
    order_article.sum(&:subtotal)
  end

  def tax_amount
    subtotal * .10
  end

  def total_amount
    total = subtotal + tax_amount
  end

  def complete_order
    update!(status: :complete)
    order_articles.each(&:complete_order_article)
  end

  def incomplete_order
    update!(status: :incomplete)
    order_articles.each(&:incomplete_order_article)
  end


end
\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.