Sha256: a0e32ef4d65850f676445f49bc88980fd17f630daf8829c5125a14b02d550bee

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

module ResourcesController::RestActions
  extend ActiveSupport::Concern

  included do
    include ActionController::MimeResponds

    respond_to :html
    responders :flash

    if respond_to?(:before_action)
      before_action :load_collection, only: [:index]
      before_action :load_resource, only: [:show, :edit, :destroy, :update]
      before_action :initialize_resource, only: [:new]
      before_action :initialize_resource_for_create, only: [:create]
    else
      before_filter :load_collection, only: [:index]
      before_filter :load_resource, only: [:show, :edit, :destroy, :update]
      before_filter :initialize_resource, only: [:new]
      before_filter :initialize_resource_for_create, only: [:create]
    end
  end

  def index; end
  def new; end
  def show; end
  def edit; end

  def update
    if Rails::VERSION::MAJOR < 4
      @resource.update_attributes(permitted_params)
    else
      @resource.update(permitted_params)
    end
    respond_with @resource
  end

  def destroy
    @resource.destroy
    respond_with @resource
  end

  def create
    @resource.save
    respond_with @resource
  end

  private

  def after_create_location
    ->(controller) { resource_path(@resource) }
  end

  def load_collection_scope
    resource_class
      end
      def load_collection
        @collection = load_collection_scope.all
      end

      def load_resource_scope
        resource_class
      end
      def load_resource
        @resource = load_resource_scope.find(params[:id])
      end

      def initialize_resource
        @resource = resource_class.new
      end

      def initialize_resource_for_create
        @resource = resource_class.new(permitted_params)
      end

      def permitted_params
        raise "not implemented"
      end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails-add_ons-1.0.0 app/concerns/resources_controller/rest_actions.rb