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

fix(line-item): refactor code #136

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
26 changes: 15 additions & 11 deletions app/models/spree/line_item_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@ module Spree::LineItemDecorator
# chosen for the product. This is mainly for compatibility with spree_sale_products
#
# Assumption here is that the volume price currency is the same as the product currency

def self.prepended(base)
old_copy_price = base.instance_method(:copy_price)

define_method(:copy_price) do
old_copy_price.bind(self).call
return unless variant
def copy_price
return unless variant

if changed? && changes.keys.include?('quantity')
vprice = variant.volume_price(quantity, order.user)
if price.present? && vprice <= variant.price
self.price = vprice and return
end
if changed? && changes.keys.include?('quantity')
vprice = variant.volume_price(quantity, order.user)
if price.present? && vprice <= variant.price
self.price = vprice and return
end

self.price = variant.price if price.nil?
end
end

def update_price
vprice = variant.volume_price(quantity, order.user)
if price.present? && vprice <= variant.price
self.price = variant.volume_price(quantity, order.user) and return
end
self.price = variant.price if price.nil?
end

end

Spree::LineItem.prepend Spree::LineItemDecorator
20 changes: 5 additions & 15 deletions app/models/spree/variant_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,11 @@ def self.prepended(base)
def join_volume_prices(user = nil)
table = Spree::VolumePrice.arel_table

if user
Spree::VolumePrice.where(
(table[:variant_id].eq(id)
.or(table[:volume_price_model_id].in(volume_price_models.ids)))
.and(table[:role_id].eq(user.resolve_role.try(:id)))
)
.order(position: :asc)
else
Spree::VolumePrice.where(
(table[:variant_id]
.eq(id)
.or(table[:volume_price_model_id].in(volume_price_models.ids)))
.and(table[:role_id].eq(nil))
).order(position: :asc)
end
Spree::VolumePrice.where(
(table[:variant_id]
.eq(id)
.or(table[:volume_price_model_id].in(volume_price_models.ids)))
).order(position: :asc)
end

# calculates the price based on quantity
Expand Down
2 changes: 1 addition & 1 deletion app/views/spree/admin/variants/_edit_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<th><%= Spree.t(:range) %></th>
<th><%= Spree.t(:amount) %></th>
<th><%= Spree.t(:position) %></th>
<th><%= Spree.t(:role) %></th>
<!-- <th><%#= Spree.t(:role) %></th> -->
<th><%= Spree.t(:action) %></th>
</tr>
</thead>
Expand Down
2 changes: 1 addition & 1 deletion app/views/spree/admin/volume_prices/_edit_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<th><%= Spree.t(:range) %></th>
<th><%= Spree.t(:amount) %></th>
<th><%= Spree.t(:position) %></th>
<th><%= Spree.t(:role) %></th>
<!-- <th><%#= Spree.t(:role) %></th> -->
<th><%= Spree.t(:action) %></th>
</tr>
</thead>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
<%= error_message_on(f.object, :position) %>
<%= f.text_field :position, size: 3, class: 'form-control' %>
</td>
<td>
<%= error_message_on(f.object, :role_id) %>
<%= f.collection_select(:role_id, Spree::Role.all, :id, :name, { include_blank: Spree.t('match_choices.none') }, { class: 'select2' }) %>
</td>
<!-- <td>
<%#= error_message_on(f.object, :role_id) %>
<%#= f.collection_select(:role_id, Spree::Role.all, :id, :name, { include_blank: Spree.t('match_choices.none') }, { class: 'select2' }) %>
</td> -->
<td class="actions">
<%= link_to_icon_remove_fields f %>
</td>
Expand Down