docbrown/app/controllers/html_variants_controller.rb
2018-10-23 16:23:48 -04:00

67 lines
1.9 KiB
Ruby

class HtmlVariantsController < ApplicationController
after_action :verify_authorized
def index
authorize HtmlVariant
@html_variants = if params[:state] == "mine"
current_user.html_variants.order("created_at DESC").includes(:user)
elsif params[:state] == "admin"
HtmlVariant.where(published: true).order("created_at DESC").includes(:user)
else
HtmlVariant.where(published: true, approved: true).order("success_rate DESC").includes(:user)
end
end
def new
authorize HtmlVariant
@html_variant = HtmlVariant.new
if params[:fork_id]
@fork = HtmlVariant.find(params[:fork_id])
@html_variant.name = @fork.name + " FORK-#{rand(10000)}"
@html_variant.html = @fork.html
end
end
def show
@story_show = true
@@article_show = true
@html_variant = HtmlVariant.find(params[:id])
authorize @html_variant
render layout: false
end
def edit
@html_variant = HtmlVariant.find(params[:id])
authorize @html_variant
end
def create
authorize HtmlVariant
@html_variant = HtmlVariant.new(html_variant_params)
@html_variant.group = "article_show_sidebar_cta"
@html_variant.user_id = current_user.id
if @html_variant.save
flash[:success] = "HTML Variant Created"
redirect_to "/html_variants/#{@html_variant.id}/edit"
else
render :new
end
end
def update
@html_variant = HtmlVariant.find(params[:id])
authorize @html_variant
if @html_variant.update(html_variant_params)
flash[:success] = "HTML Variant Updated"
redirect_to "/html_variants/#{@html_variant.id}/edit"
else
render :edit
end
end
private
def html_variant_params
params.require(:html_variant).permit(policy(HtmlVariant).permitted_attributes)
end
end