app/controllers/spree/admin/taxons_controller.rb in spree_backend-2.0.13 vs app/controllers/spree/admin/taxons_controller.rb in spree_backend-2.1.0
- old
+ new
@@ -6,11 +6,11 @@
def search
if params[:ids]
@taxons = Spree::Taxon.where(:id => params[:ids].split(','))
else
- @taxons = Spree::Taxon.limit(20).search(:name_cont => params[:q]).result
+ @taxons = Spree::Taxon.limit(20).ransack(:name_cont => params[:q]).result
end
end
def create
@taxonomy = Taxonomy.find(params[:taxonomy_id])
@@ -37,37 +37,54 @@
@taxonomy = Taxonomy.find(params[:taxonomy_id])
@taxon = @taxonomy.taxons.find(params[:id])
parent_id = params[:taxon][:parent_id]
new_position = params[:taxon][:position]
- if parent_id
- @taxon.parent = Taxon.find(parent_id.to_i)
- end
+ if parent_id || new_position #taxon is being moved
+ new_parent = parent_id.nil? ? @taxon.parent : Taxon.find(parent_id.to_i)
+ new_position = new_position.nil? ? -1 : new_position.to_i
- if new_position
- @taxon.child_index = new_position.to_i
- end
+ # Bellow is a very complicated way of finding where in nested set we
+ # should actually move the taxon to achieve sane results,
+ # JS is giving us the desired position, which was awesome for previous setup,
+ # but now it's quite complicated to find where we should put it as we have
+ # to differenciate between moving to the same branch, up down and into
+ # first position.
+ new_siblings = new_parent.children
+ if new_position <= 0 && new_siblings.empty?
+ @taxon.move_to_child_of(new_parent)
+ elsif new_parent.id != @taxon.parent_id
+ if new_position == 0
+ @taxon.move_to_left_of(new_siblings.first)
+ else
+ @taxon.move_to_right_of(new_siblings[new_position-1])
+ end
+ elsif new_position < new_siblings.index(@taxon)
+ @taxon.move_to_left_of(new_siblings[new_position]) # we move up
+ else
+ @taxon.move_to_right_of(new_siblings[new_position-1]) # we move down
+ end
+ # Reset legacy position, if any extensions still rely on it
+ new_parent.children.reload.each{|t| t.update_column(:position, t.position)}
- @taxon.save!
-
- # regenerate permalink
- if parent_id
- @taxon.reload
- @taxon.set_permalink
- @taxon.save!
- @update_children = true
+ if parent_id
+ @taxon.reload
+ @taxon.set_permalink
+ @taxon.save!
+ @update_children = true
+ end
end
if params.key? "permalink_part"
parent_permalink = @taxon.permalink.split("/")[0...-1].join("/")
parent_permalink += "/" unless parent_permalink.blank?
params[:taxon][:permalink] = parent_permalink + params[:permalink_part]
end
#check if we need to rename child taxons if parent name or permalink changes
@update_children = true if params[:taxon][:name] != @taxon.name || params[:taxon][:permalink] != @taxon.permalink
- if @taxon.update_attributes(params[:taxon])
+ if @taxon.update_attributes(taxon_params)
flash[:success] = flash_message_for(@taxon, :successfully_updated)
end
#rename child taxons
if @update_children
@@ -88,8 +105,17 @@
@taxon = Taxon.find(params[:id])
@taxon.destroy
respond_with(@taxon) { |format| format.json { render :json => '' } }
end
+ private
+ def taxon_params
+ params.require(:taxon).permit(permitted_params)
+ end
+
+ def permitted_params
+ [:name, :parent_id, :position, :icon, :description, :permalink,
+ :taxonomy_id, :meta_description, :meta_keywords, :meta_title]
+ end
end
end
end