Sha256: 387aa64913a0a5f645364270ff67077dfbbaa79ff73a63f9c5a187985b97da3e

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

class SolidusSubscriptions::Api::V1::SubscriptionsController < Spree::Api::BaseController
  before_action :load_subscription, only: [:cancel, :update, :skip]

  def update
    if @subscription.update(subscription_params)
      render json: @subscription.to_json(include: [:line_items, :shipping_address])
    else
      render json: @subscription.errors.to_json, status: 422
    end
  end

  def skip
    if @subscription.skip
      render json: @subscription.to_json
    else
      render json: @subscription.errors.to_json, status: 422
    end
  end

  def cancel
    if @subscription.cancel
      render json: @subscription.to_json
    else
      render json: @subscription.errors.to_json, status: 422
    end
  end

  private

  def load_subscription
    @subscription = current_api_user.subscriptions.find(params[:id])
  end

  def subscription_params
    params.require(:subscription).permit(
      line_items_attributes: line_item_attributes,
      shipping_address_attributes: Spree::PermittedAttributes.address_attributes
    )
  end

  def line_item_attributes
    SolidusSubscriptions::Config.subscription_line_item_attributes - [:subscribable_id] + [:id]
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
solidus_subscriptions-alpha-0.0.5 app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb
solidus_subscriptions-alpha-0.0.3 app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb
solidus_subscriptions-alpha-0.0.2 app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb
solidus_subscriptions-alpha-0.0.1 app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb