Sha256: cb372649d1bc3985cbbaea5351cb2b0c25bf06140fdd8d8ee9a8ebab67aa8223

Contents?: true

Size: 1.83 KB

Versions: 17

Compression:

Stored size: 1.83 KB

Contents

module Spree
  module Api
    class PropertiesController < Spree::Api::BaseController
      before_action :find_property, only: [:show, :update, :destroy]

      def index
        @properties = Spree::Property.accessible_by(current_ability, :read)

        if params[:ids]
          ids = params[:ids].split(",").flatten
          @properties = @properties.where(id: ids)
        else
          @properties = @properties.ransack(params[:q]).result
        end

        @properties = @properties.page(params[:page]).per(params[:per_page])
        respond_with(@properties)
      end

      def show
        respond_with(@property)
      end

      def new
      end

      def create
        authorize! :create, Property
        @property = Spree::Property.new(property_params)
        if @property.save
          respond_with(@property, status: 201, default_template: :show)
        else
          invalid_resource!(@property)
        end
      end

      def update
        if @property
          authorize! :update, @property
          @property.update_attributes(property_params)
          respond_with(@property, status: 200, default_template: :show)
        else
          invalid_resource!(@property)
        end
      end

      def destroy
        if @property
          authorize! :destroy, @property
          @property.destroy
          respond_with(@property, status: 204)
        else
          invalid_resource!(@property)
        end
      end

      private

      def find_property
        @property = Spree::Property.accessible_by(current_ability, :read).find(params[:id])
      rescue ActiveRecord::RecordNotFound
        @property = Spree::Property.accessible_by(current_ability, :read).find_by!(name: params[:id])
      end

      def property_params
        params.require(:property).permit(permitted_property_attributes)
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
solidus_api-2.0.3 app/controllers/spree/api/properties_controller.rb
solidus_api-1.4.2 app/controllers/spree/api/properties_controller.rb
solidus_api-1.3.2 app/controllers/spree/api/properties_controller.rb
solidus_api-2.0.2 app/controllers/spree/api/properties_controller.rb
solidus_api-1.4.1 app/controllers/spree/api/properties_controller.rb
solidus_api-2.0.1 app/controllers/spree/api/properties_controller.rb
solidus_api-2.0.0 app/controllers/spree/api/properties_controller.rb
solidus_api-1.4.0 app/controllers/spree/api/properties_controller.rb
solidus_api-2.0.0.rc1 app/controllers/spree/api/properties_controller.rb
solidus_api-1.4.0.rc1 app/controllers/spree/api/properties_controller.rb
solidus_api-2.0.0.beta1 app/controllers/spree/api/properties_controller.rb
solidus_api-1.4.0.beta1 app/controllers/spree/api/properties_controller.rb
solidus_api-1.3.1 app/controllers/spree/api/properties_controller.rb
solidus_api-1.3.0 app/controllers/spree/api/properties_controller.rb
solidus_api-1.3.0.rc2 app/controllers/spree/api/properties_controller.rb
solidus_api-1.3.0.rc1 app/controllers/spree/api/properties_controller.rb
solidus_api-1.3.0.beta1 app/controllers/spree/api/properties_controller.rb