Sha256: 030b7a1f332c67b338608375d48c9c811f9973d111b50fc81c2e8f2b705dcb58

Contents?: true

Size: 1.79 KB

Versions: 11

Compression:

Stored size: 1.79 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(respond_with_namespace, @resource)
  end

  def destroy
    @resource.destroy
    respond_with(respond_with_namespace, @resource)
  end

  def create
    @resource.save
    respond_with(respond_with_namespace, @resource)
  end

  private

  def respond_with_namespace
    nil
  end

  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

11 entries across 11 versions & 1 rubygems

Version Path
rails-add_ons-1.5.2 app/concerns/resources_controller/rest_actions.rb
rails-add_ons-1.5.1 app/concerns/resources_controller/rest_actions.rb
rails-add_ons-1.5.0 app/concerns/resources_controller/rest_actions.rb
rails-add_ons-1.4.1 app/concerns/resources_controller/rest_actions.rb
rails-add_ons-1.4.0 app/concerns/resources_controller/rest_actions.rb
rails-add_ons-1.3.3 app/concerns/resources_controller/rest_actions.rb
rails-add_ons-1.3.2 app/concerns/resources_controller/rest_actions.rb
rails-add_ons-1.3.1 app/concerns/resources_controller/rest_actions.rb
rails-add_ons-1.3.0 app/concerns/resources_controller/rest_actions.rb
rails-add_ons-1.2.0 app/concerns/resources_controller/rest_actions.rb
rails-add_ons-1.1.0 app/concerns/resources_controller/rest_actions.rb