Skip to content

Instantly share code, notes, and snippets.

@EdgarOrtegaRamirez
Forked from dhh/documents.rb
Created April 9, 2014 17:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EdgarOrtegaRamirez/10294503 to your computer and use it in GitHub Desktop.
Save EdgarOrtegaRamirez/10294503 to your computer and use it in GitHub Desktop.
# config/routes.rb
resources :documents do
resources :versions, controller: "documents/versions" do
post :restore, on: :member
end
resource :lock, controller: "documents/locks"
end
# app/controllers/documents_controller.rb
class DocumentsController < ApplicationController
include ProjectScoped
def index
@documents = @project.documents
end
def show
@document = @project.documents.find(params[:id])
end
def new
@document = Document.new
end
def create
@document = @project.documents.create! document_params.merge(creator: current_person)
end
end
# app/controllers/documents/locks_controller.rb
module Documents
class LocksController < ApplicationController
include DocumentScoped, ProjectScoped
def update
@document.lock!(current_person)
end
def destroy
@document.unlock!(current_person)
end
end
end
# app/controllers/documents/versions_controller.rb
module Documents
class VersionsController < ApplicationController
include DocumentScoped, ProjectScoped
before_action :set_version
def show
end
def restore
@document.restore!(@version)
end
private
def set_version
@version = @document.versions.find(params[:id])
end
end
end
# app/controllers/concerns/document_scoped.rb
module DocumentScoped
extend ActiveSupport::Concern
included do
before_action :set_document
end
private
def set_document
@document = @project.documents.find(params[:document_id])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment