Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Import count_on_hand methods for Spree 2 #2417

Closed
wants to merge 1 commit into from
Closed
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
70 changes: 70 additions & 0 deletions app/models/spree/variant_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,54 @@ def self.indexed
]
end

# Overriding `Spree::Variant.on_hand` in old Spree versions.
# It doesn't exist in newer Spree versions.
def on_hand
if respond_to? :total_on_hand
# This is Spree 2.0
total_on_hand
elsif Spree::Config[:track_inventory_levels] && !on_demand
count_on_hand
else
Float::INFINITY
end
end

# Overriding `Spree::Variant.on_hand=` in old Spree versions.
# It doesn't exist in newer Spree versions.
def on_hand=(new_level)
error = 'Cannot set on_hand value when Spree::Config[:track_inventory_levels] is false'
raise error unless Spree::Config[:track_inventory_levels]

self.count_on_hand = new_level unless on_demand
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ this is directly affected by openfoodfoundation/spree#8

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's where I saw this code before! Thank you. So this pull request would override Frank's change.

end

# Overriding Spree::Variant.count_on_hand in old Spree versions.
# It doesn't exist in newer Spree versions.
def count_on_hand
if respond_to? :total_on_hand
# This is Spree 2.0
total_on_hand
else
# We assume old Spree and call ActiveRecord's method.
# We can't call the same method on Variant, because we are overwriting it.
super
end
end

# Overriding `Spree::Variant.count_on_hand=` in old Spree versions.
# It doesn't exist in newer Spree versions.
def count_on_hand=(new_level)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these methods necessary? Spree::Variant doesn't have count_on_hand or count_on_hand=methods, it uses the on_hand methods as getters and setters.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is this related to Variant Overrides?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Spree 1.3 the variants table has a count_on_hand column. So ActiveRecord adds these methods automatically and we have a few calls to these methods in our code base. In order to keep the old API alive, we need these methods.

if respond_to? :total_on_hand
# This is Spree 2.0
overwrite_stock_levels new_level
else
# We assume old Spree and call ActiveRecord's method.
# We can't call the same method on Variant, because we are overwriting it.
super unless on_demand
end
end

def price_with_fees(distributor, order_cycle)
price + fees_for(distributor, order_cycle)
end
Expand Down Expand Up @@ -119,6 +167,28 @@ def refresh_products_cache

private

# Spree 2 creates this location in:
#
# core/db/migrate/20130213191427_create_default_stock.rb
#
# This is the only location we are using at the moment. So everything stays
# the same, each variant has only one stock level (at the default location).
def self.default_stock_location
Spree::StockLocation.find_by_name("default")
end

# Temporary, backwards compatible setting of stock levels in Spree 2.0.
# It would be better to use `Spree::StockItem.adjust_count_on_hand` which
# takes a value to add to the current stock level and uses proper locking.
def overwrite_stock_levels(new_level)
stock_items.first.send :count_on_hand, new_level

# There shouldn't be any other stock items, because we should have only one
# stock location. But in case there are, the total should be new_level,
# so all others need to be zero.
stock_items[1..-1].send :count_on_hand, 0
end

def update_weight_from_unit_value
self.weight = weight_from_unit_value if self.product.variant_unit == 'weight' && unit_value.present?
end
Expand Down