2015-12-11 15:26:51 -05:00
|
|
|
# controllers/subcreddits_controller.rb
|
2015-07-13 02:14:35 -04:00
|
|
|
class SubcredditsController < ApplicationController
|
|
|
|
before_filter :set_subcreddit, only: [:show, :edit, :update]
|
2015-12-14 13:28:39 -05:00
|
|
|
after_action :verify_authorized
|
2015-07-13 02:14:35 -04:00
|
|
|
|
|
|
|
def index
|
|
|
|
@subcreddits = Subcreddit.all
|
2015-12-14 13:28:39 -05:00
|
|
|
|
|
|
|
authorize Subcreddit
|
2015-07-13 02:14:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2015-12-23 17:01:23 -05:00
|
|
|
@posts = @subcreddit.posts.includes(:user)
|
2015-12-14 13:28:39 -05:00
|
|
|
|
|
|
|
authorize @subcreddit
|
2015-07-13 02:14:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@subcreddit = Subcreddit.new
|
2015-12-14 13:28:39 -05:00
|
|
|
|
|
|
|
authorize @subcreddit
|
2015-07-13 02:14:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@subcreddit = Subcreddit.new(create_subcreddit_params)
|
|
|
|
@subcreddit.owner = current_user
|
|
|
|
|
2015-12-14 13:28:39 -05:00
|
|
|
authorize @subcreddit
|
|
|
|
|
2015-07-13 02:14:35 -04:00
|
|
|
if @subcreddit.save
|
|
|
|
redirect_to @subcreddit, notice: 'Subcreddit was created!'
|
|
|
|
else
|
|
|
|
render :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
2015-12-14 13:28:39 -05:00
|
|
|
authorize @subcreddit
|
2015-07-13 02:14:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2015-12-14 13:28:39 -05:00
|
|
|
authorize @subcreddit
|
|
|
|
|
2015-07-13 02:14:35 -04:00
|
|
|
if @subcreddit.update(update_subcreddit_params)
|
|
|
|
redirect_to @subcreddit, notice: 'Subcreddit was updated!'
|
|
|
|
else
|
|
|
|
render :edit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_subcreddit_params
|
|
|
|
params.require(:subcreddit).permit(:name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_subcreddit_params
|
|
|
|
params.require(:subcreddit).permit(:closed)
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_subcreddit
|
2015-07-16 00:51:15 -04:00
|
|
|
@subcreddit = Subcreddit.friendly.find(params[:id])
|
2015-07-13 02:14:35 -04:00
|
|
|
end
|
|
|
|
end
|