Sha256: 4c1a86ed81d3b2e4905d111848b38073418b18fbecc168745d368ada058c308c

Contents?: true

Size: 1.95 KB

Versions: 10

Compression:

Stored size: 1.95 KB

Contents

class Admin::VariantsController < Admin::ResourceController
  belongs_to :product, :find_by => :permalink
  create.before :create_before
  new_action.before :new_before

  def index
    respond_with(collection) do |format|
      format.html
      format.json { render :json => json_data }
    end
  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_with(@variant) 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_with(@variant) do |format|
      format.html { redirect_to admin_product_variants_url(params[:product_id]) }
      format.js  { render :text => 'Ok' }
    end
  end

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


  def new_before
    @object.attributes = @object.product.master.attributes.except('id', 'created_at', 'deleted_at',
                                                                  'sku', 'is_master', 'count_on_hand')
  end

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

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

  def json_data
    ( parent.variants.presence || [parent.master] ).map do |v|
      { :label => v.options_text.presence || v.name, :id => v.id }
    end
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
spree_core-0.70.7 app/controllers/admin/variants_controller.rb
spree_core-0.70.6 app/controllers/admin/variants_controller.rb
spree_core-0.70.5 app/controllers/admin/variants_controller.rb
spree_core-0.70.4 app/controllers/admin/variants_controller.rb
spree_core-0.70.3 app/controllers/admin/variants_controller.rb
spree_core-0.70.2 app/controllers/admin/variants_controller.rb
spree_core-0.70.1 app/controllers/admin/variants_controller.rb
spree_core-0.70.0 app/controllers/admin/variants_controller.rb
spree_core-0.70.0.rc2 app/controllers/admin/variants_controller.rb
spree_core-0.70.RC1 app/controllers/admin/variants_controller.rb