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

Change make_permalink behaviour #2341

Merged
merged 4 commits into from
Nov 23, 2017
Merged
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
2 changes: 1 addition & 1 deletion backend/spec/features/admin/orders/order_details_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@

context 'A shipment has shipped' do
it 'should not show or let me back to the cart page, nor show the shipment edit buttons' do
order = create(:order, state: 'payment', number: "R100")
order = create(:order, state: 'payment')
order.shipments.create!(stock_location_id: stock_location.id, state: 'shipped')

visit spree.cart_admin_order_path(order)
Expand Down
8 changes: 7 additions & 1 deletion core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ def states
validates :number, presence: true, uniqueness: { allow_blank: true }
validates :store_id, presence: true

make_permalink field: :number
def self.find_by_param(value)
find_by number: value
end

def self.find_by_param!(value)
find_by! number: value
end

delegate :update_totals, :persist_totals, to: :updater
delegate :firstname, :lastname, to: :bill_address, prefix: true, allow_nil: true
Expand Down
22 changes: 9 additions & 13 deletions core/lib/spree/core/permalinks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,20 @@ def generate_permalink
end

def save_permalink(permalink_value = to_param)
with_lock do
permalink_value ||= generate_permalink
permalink_value ||= generate_permalink
permalink_field = self.class.permalink_field

field = self.class.permalink_field
# Do other links exist with this permalink?
other = self.class.where("#{self.class.table_name}.#{field} LIKE ?", "#{permalink_value}%")
if other.any?
# Find the existing permalink with the highest number, and increment that number.
# (If none of the existing permalinks have a number, this will evaluate to 1.)
number = other.map { |o| o.send(field)[/-(\d+)$/, 1].to_i }.max + 1
permalink_value += "-#{number}"
end
write_attribute(field, permalink_value)
loop do
other = self.class.where(permalink_field => permalink_value)
break unless other.exists?

# Try again with a new value
permalink_value = generate_permalink
end
write_attribute(permalink_field, permalink_value)
end
end
end
end

ActiveRecord::Base.send :include, Spree::Core::Permalinks
ActiveRecord::Relation.send :include, Spree::Core::Permalinks