The belongs_to_creator macro was used to initialize the creator_id field to the CurrentUser. This made tests complicated because it meant you had to create and set the current user every time you wanted to create an object, when lead to the current user being set over and over again. It also meant you had to constantly be aware of what the CurrentUser was in many different contexts, which was often confusing. Setting creators explicitly simplifies everything greatly.
45 lines
1.0 KiB
Ruby
45 lines
1.0 KiB
Ruby
class NewsUpdatesController < ApplicationController
|
|
before_action :admin_only
|
|
respond_to :html
|
|
|
|
def index
|
|
@news_updates = NewsUpdate.order("id desc").paginate(params[:page], :limit => params[:limit])
|
|
respond_with(@news_updates)
|
|
end
|
|
|
|
def edit
|
|
@news_update = NewsUpdate.find(params[:id])
|
|
respond_with(@news_update)
|
|
end
|
|
|
|
def update
|
|
@news_update = NewsUpdate.find(params[:id])
|
|
@news_update.update(news_update_params)
|
|
respond_with(@news_update, :location => news_updates_path)
|
|
end
|
|
|
|
def new
|
|
@news_update = NewsUpdate.new
|
|
respond_with(@news_update)
|
|
end
|
|
|
|
def create
|
|
@news_update = NewsUpdate.create(news_update_params.merge(creator: CurrentUser.user))
|
|
respond_with(@news_update, :location => news_updates_path)
|
|
end
|
|
|
|
def destroy
|
|
@news_update = NewsUpdate.find(params[:id])
|
|
@news_update.destroy
|
|
respond_with(@news_update) do |format|
|
|
format.js
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def news_update_params
|
|
params.require(:news_update).permit([:message])
|
|
end
|
|
end
|