class QuotesController < ApplicationController protect_from_forgery with: :null_session, if: -> { request.format.json? or request.format.xml? } def index @quote = Quote.find(310) @qnumber = Quote.published.count @mnumber = Quote.pending.count end def latest @quotes = Quote.published.order(id: :desc).paginate(page: params[:page], per_page: 10) respond_to do |format| format.html { render :list } format.json { render :json => @quotes } format.xml { render :xml => @quotes } format.atom { render :atom => @quotes } end end def top @quotes = Quote.published.order(Arel.sql("(upvotes - downvotes) DESC")).paginate(page: params[:page], per_page: 10) respond_to do |format| format.html { render :list } format.json { render :json => @quotes } format.xml { render :xml => @quotes } end end def random picked = Quote.ids.sample redirect_to quote_url(picked) end def tags @tags = Quote.published.tag_counts_on(:tags) end def tag @quotes = Quote.published.tagged_with(params[:id]).order(id: :desc).paginate(page: params[:page], per_page: 10) respond_to do |format| format.html { render :list } format.json { render :json => @quotes } format.xml { render :xml => @quotes } end end def show @quote = Quote.published.find(params[:id]) set_meta_tags(og: { title: "Quote \##{@quote.id}", type: "article", description: (@quote.content.length <= 300 ? @quote.content : @quote.content[0..299]), url: quote_url(@quote, host: "www.fourisland.com"), article: { published_time: @quote.created_at.iso8601, modified_time: @quote.updated_at.iso8601 } }) respond_to do |format| format.html format.json { render :json => @quote } format.xml { render :xml => @quote } end end def new @quote = Quote.new end def create @quote = Quote.new(quote_params) @quote.submitter = nil if @quote.submitter.empty? unless @quote.valid? flash.alert = "Error submitting quote." respond_to do |format| format.html { render :new } format.json { render json: { error: "Error submitting quote." }, status: :bad_request } format.xml { render xml: { error: "Error submitting quote." }, status: :bad_request } end return end if user_signed_in? @quote.state = :published @quote.save! flash[:notice] = "Thank you for submitting your quote!" respond_to do |format| format.html { redirect_to @quote } format.json { render json: @quote } format.xml { render xml: @quote } end else @quote.state = :pending @quote.save! QuoteMailer.with(quote: @quote).pending_quote_email.deliver_later flash[:notice] = "Your quote has been submitted and is pending moderation." respond_to do |format| format.html { redirect_to new_quote_url } format.json { render json: @quote } format.xml { render xml: @quote } end end end def upvote @quote = Quote.published.find(params[:id]) respond_to do |format| if @quote.upvote! request.remote_ip format.html do flash[:notice] = "You have upvoted Quote \"#{@quote.id}\"." redirect_to @quote end format.js { render "voted" } format.xml { head :ok } else format.html do flash[:notice] = "You have already voted on Quote \"#{@quote.id}\"." redirect_to @quote end format.xml { render :xml => { :error => "Someone from your IP address has already voted on this quote."} } end end end def downvote @quote = Quote.published.find(params[:id]) respond_to do |format| if @quote.downvote! request.remote_ip format.html do flash[:notice] = "You have downvoted Quote \"#{@quote.id}\"." redirect_to @quote end format.js { render "voted" } format.xml { head :ok } else format.html do flash[:notice] = "You have already voted on Quote \"#{@quote.id}\"." redirect_to @quote end format.xml { render :xml => { :error => "Someone from your IP address has already voted on this quote."} } end end end private def quote_params params.require(:quote).permit(:content, :notes, :submitter, :tag_list) end end