Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DRY Madness
class NotesController < ApplicationController
def index
notes = requested_notes
respond_to do |format|
format.html do
render :index, locals: { notes: notes }
end
format.json do
render :index, locals: { notes: notes } if stale? notes
end
end
end
def show
note = requested_note
respond_to do |format|
format.html do
render :show, locals: { note: note }
end
format.json do
render :show, locals: { note: note } if stale? note
end
end
end
def new
respond_to do |format|
format.html do
render :new, locals: { note: Note.new(note_params) }
end
end
end
def create
note = Note.new(note_params)
if note.save
respond_to do |format|
format.html do
redirect_to note
end
format.json do
head :created
end
end
else
respond_to do |format|
format.html do
render :new, locals: { note: note }
end
format.json do
render :errors, locals: { note: note }
end
end
end
end
def edit
note = requested_note
respond_to do |format|
format.html do
render :errors, locals: { errors: note.errors }
end
end
end
def update
note = requested_note
if note.update(note_params)
respond_to do |format|
format.html do
redirect_to note
end
format.json do
head :no_content
end
end
else
respond_to do |format|
format.html do
render :edit, locals: { note: note }
end
format.json do
render :errors, locals: { errors: note.errors }
end
end
end
end
def destroy
note = requested_note
if note.destroy
respond_to do |format|
format.html do
redirect_to notes_url
end
format.json do
head :no_content
end
end
else
respond_to do |format|
format.html do
render :show, locals: { note: note }
end
format.json do
render :errors, locals: { errors: note.errors }
end
end
end
end
private
def requested_notes
note_scope.page(params[:page])
end
def requested_note
note_scope.find(params[:id])
end
def note_params
params.require(:note).permit(:title, :text, :published)
end
def note_scope
if current_user.admin?
Note.all
else
current_user.notes
end
end
end
# Shallow nested resource
class NotesController < ApplicationController
# Alternative index which uses search filters and a nest route
# This controller likely would not have a `requested_notes` helper
def index
# Ensure we have an authorized request before we do anything
category = requested_category
filter = persisted_search_filter.merge(requested_search_filter)
notes = NotesQuery.new(
category,
note_scope.includes(:tags, :revisors),
filter,
)
persist_search_filter filter
respond_to do |format|
format.html do
render :index, locals: { notes: notes }
end
format.json do
render :index, locals: { notes: notes } if stale? notes
end
end
end
def show
note = requested_note
respond_to do |format|
format.html do
render :show, locals: { note: note }
end
format.json do
render :show, locals: { note: note } if stale? note
end
end
end
def new
respond_to do |format|
format.html do
render :new, locals: { note: Note.new(note_params) }
end
end
end
def create
note = Note.new(create_note_params))
if note.save
respond_to do |format|
format.html do
redirect_to note
end
format.json do
head :created
end
end
else
respond_to do |format|
format.html do
render :new, locals: { note: note }
end
format.json do
render :errors, locals: { note: note }
end
end
end
end
def edit
note = requested_note
respond_to do |format|
format.html do
render :errors, locals: { errors: note.errors }
end
end
end
def update
note = requested_note
if note.update(note_params)
respond_to do |format|
format.html do
redirect_to note
end
format.json do
head :no_content
end
end
else
respond_to do |format|
format.html do
render :edit, locals: { note: note }
end
format.json do
render :errors, locals: { errors: note.errors }
end
end
end
end
def destroy
note = requested_note
if note.destroy
respond_to do |format|
format.html do
redirect_to notes_url
end
format.json do
head :no_content
end
end
else
respond_to do |format|
format.html do
render :show, locals: { note: note }
end
format.json do
render :errors, locals: { errors: note.errors }
end
end
end
end
private
def requested_category
scope = if current_user.admin?
Category.all
else
current_user.categories
end
scope.find(params.require(:category_id))
end
def requested_search_filter
{
page: params[:page],
tags: params[:tag].try(:split, ','),
query_terms: params[:q].try(:split),
}
end
def requested_note
note_scope.find(params[:id])
end
def note_params
munge_legacy_params
params.require(:note).permit(:title, :text, :published, :locale)
end
def munage_legacy_params
# b/c one-line ||= if statements are confusing
if old_locale = params.delete(:localized_web)
params[:locale] ||= old_locale
end
end
def create_note_params
create_by_params = {
category: requested_category,
creator: current_user,
}
base_params = note_params
verify_required_params base_params
base_params.merge!(create_by_params)
end
def verify_required_params(submitted_params)
submitted_params.require(:locale)
end
def note_scope
if current_user.admin?
Note.all
else
Note.where(category: current_user.categories)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.