Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

.idea/*
/log/*
/tmp/*
18 changes: 18 additions & 0 deletions app/admin/order_products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

ActiveAdmin.register OrderProduct do
# See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
#
# Uncomment all parameters which should be permitted for assignment
#
# permit_params :order_id, :product_id, :quantity
#
# or
#
permit_params do
permitted = %i[order_id product_id quantity each_item_price_in_cents]
# permitted << :other if params[:action] == 'create' && current_user.admin?
permitted
end
end
18 changes: 18 additions & 0 deletions app/admin/orders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

ActiveAdmin.register Order do
# See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
#
# Uncomment all parameters which should be permitted for assignment
#
# permit_params :shipping_date
#
# or
#
permit_params do
permitted = [:shipping_date]
# permitted << :other if params[:action] == 'create' && current_user.admin?
permitted
end
end
18 changes: 18 additions & 0 deletions app/admin/products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

ActiveAdmin.register Product do
# See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
#
# Uncomment all parameters which should be permitted for assignment
#
# permit_params :name, :price_in_cents, :sku_id
#
# or
#
permit_params do
permitted = %i[name price_in_cents sku_id]
# permitted << :other if params[:action] == 'create' && current_user.admin?
permitted
end
end
18 changes: 18 additions & 0 deletions app/admin/skus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

ActiveAdmin.register Sku do
# See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
#
# Uncomment all parameters which should be permitted for assignment
#
# permit_params :product_code
#
# or
#
permit_params do
permitted = [:product_code]
# permitted << :other if params[:action] == 'create' && current_user.admin?
permitted
end
end
2 changes: 2 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

import './active_admin'
6 changes: 6 additions & 0 deletions app/models/discount.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Discount < ApplicationRecord
validates :order, presence: true
validates :amount, presence: true

belongs_to :order
end
10 changes: 9 additions & 1 deletion app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class Order < ApplicationRecord
# has many
has_many :order_products, dependent: :destroy
has_many :discounts

# accepts nested attributes
accepts_nested_attributes_for :order_products, allow_destroy: true
Expand All @@ -15,7 +16,14 @@ class Order < ApplicationRecord

# methods
def total
order_products.sum(&:subtotal)
(order_products.sum(&:subtotal) / total_discounts) * 100
end

def total_discounts
total_discount = discounts.sum(:amount)
return 100 if total_discount > 100

total_discount
end

# class methods
Expand Down
12 changes: 9 additions & 3 deletions app/models/order_product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,25 @@ class OrderProduct < ApplicationRecord

# validations
validates :quantity, presence: true

validates :each_item_price_in_cents, presence: true
# attr_readonly :each_item_price_in_cents TODO to be enabled after the migration has been executed on production
#
# Calculates the subtotal for the given order product
#
# @return [Integer] the subtotal
#

before_create do
self.each_item_price_in_cents = product.price_in_cents
end

def subtotal
quantity * product.price_in_cents
quantity * each_item_price_in_cents
end

# class methods
def self.ransackable_attributes(_auth_object = nil)
%w[created_at id id_value order_id product_id quantity updated_at]
%w[created_at id id_value order_id product_id quantity each_item_price_in_cents updated_at]
end

def self.ransackable_associations(_auth_object = nil)
Expand Down
9 changes: 9 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class Product < ApplicationRecord
validates :name, presence: true
validates :price_in_cents, presence: true

has_many :product_price_histories

before_update :check_if_price_changed
# class methods
def self.ransackable_attributes(_auth_object = nil)
%w[created_at id id_value name price_in_cents sku_id updated_at]
Expand All @@ -19,4 +22,10 @@ def self.ransackable_attributes(_auth_object = nil)
def self.ransackable_associations(_auth_object = nil)
['sku']
end

def check_if_price_changed
return unless price_in_cents_changed?

ProductPriceHistory.create!(product: self, previous_price: price_in_cents_was, current_price: price_in_cents)
end
end
13 changes: 13 additions & 0 deletions app/models/product_price_history.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

#
# Represents a single product
#
class ProductPriceHistory < ApplicationRecord
belongs_to :product

# validations
validates :product, presence: true
validates :previous_price, presence: true
validates :current_price, presence: true
end
1 change: 1 addition & 0 deletions config/initializers/content_security_policy.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

# Be sure to restart your server when you modify this file.

# Define an application-wide content security policy.
Expand Down
1 change: 1 addition & 0 deletions config/initializers/inflections.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

# Be sure to restart your server when you modify this file.

# Add new inflection rules using the following format. Inflections
Expand Down
1 change: 1 addition & 0 deletions config/initializers/permissions_policy.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

# Be sure to restart your server when you modify this file.

# Define an application-wide HTTP permissions policy. For further
Expand Down
Loading