Sha256: e16e96c530d9048eed52237a63e44504827538d55abe5bc7aaec6022dc12bbc6

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

class Admin::VariantsController < Admin::BaseController
  resource_controller
  belongs_to :product

  new_action.response do |wants|
    wants.html {render :action => :new, :layout => !request.xhr?}
  end

  create.before :create_before

  # redirect to index (instead of r_c default of show view)
  create.response do |wants|
    wants.html {redirect_to collection_url}
  end

  # redirect to index (instead of r_c default of show view)
  update.response do |wants|
    wants.html {redirect_to collection_url}
  end

  # override the destory method to set deleted_at value
  # instead of actually deleting the product.
  def destroy
    @variant = Variant.find(params[:id])

    @variant.deleted_at = Time.now()
    if @variant.save
      flash.notice = I18n.t("notice_messages.variant_deleted")
    else
      flash.notice = I18n.t("notice_messages.variant_not_deleted")
    end

    respond_to do |format|
      format.html { redirect_to admin_product_variants_url(params[:product_id]) }
      format.js  { render_js_for_destroy }
    end
  end
  
  def update_positions
    params[:positions].each do |id, index|
      Variant.update_all(['position=?', index], ['id=?', id])
    end
    
    respond_to do |format|
      format.html { redirect_to admin_product_variants_url(params[:product_id]) }
      format.js  { render :text => 'Ok' }
    end
  end

  private
  def create_before
    option_values = params[:new_variant]
    option_values.each_value {|id| @object.option_values << OptionValue.find(id)}
    @object.save
  end

  def collection
    @deleted =  (params.key?(:deleted)  && params[:deleted] == "on") ? "checked" : ""

    if @deleted.blank?
      @collection ||= end_of_association_chain
    else
      @collection ||= Variant.where(:product_id => @product.id).deleted
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spree_core-0.50.4 app/controllers/admin/variants_controller.rb
spree_core-0.50.3 app/controllers/admin/variants_controller.rb
spree_core-0.50.2 app/controllers/admin/variants_controller.rb
spree_core-0.50.1 app/controllers/admin/variants_controller.rb
spree_core-0.50.0 app/controllers/admin/variants_controller.rb