Skip to content

Instantly share code, notes, and snippets.

@eri-b
Last active September 17, 2019 22:44
Show Gist options
  • Select an option

  • Save eri-b/ae67048407078f709dd98d9a36d8d8b7 to your computer and use it in GitHub Desktop.

Select an option

Save eri-b/ae67048407078f709dd98d9a36d8d8b7 to your computer and use it in GitHub Desktop.
class SitesController < ApplicationController
before_action :find_site, only: [:show, :add_password, :remove_password]
before_action :unlocked?, only: [:add_password, :remove_password]
before_action :have_posts?, only: [:add_password]
def show
...
end
def add_password
if @site.update_attributes(pass_params)
lock_site
redirect_to site_pass_path(@site.name), notice: "Updated password"
else
redirect_to site_pass_path(@site.name), alert: "Update failed"
end
end
private
def pass_params
params.require(:site).permit(:password, :password_confirmation)
end
def find_site
@slug = params[:id].downcase
@site = Site.find_by(name: @slug)
if @site.present?
expired?
end
end
def lock_site
@site.update_attributes(locked: true)
end
def unlocked?
if private?
redirect_to main_path(@site.name), notice: 'Site is locked.'
end
end
def have_posts?
if @site.posts.count == 0
redirect_to main_path(@site.name), notice: 'Cannot add password to empty site.'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment