diff options
Diffstat (limited to 'app/controllers/quotes_controller.rb')
| -rw-r--r-- | app/controllers/quotes_controller.rb | 97 |
1 files changed, 97 insertions, 0 deletions
| diff --git a/app/controllers/quotes_controller.rb b/app/controllers/quotes_controller.rb new file mode 100644 index 0000000..fb5e33c --- /dev/null +++ b/app/controllers/quotes_controller.rb | |||
| @@ -0,0 +1,97 @@ | |||
| 1 | class QuotesController < ApplicationController | ||
| 2 | def index | ||
| 3 | @quote = Quote.find(310) | ||
| 4 | @qnumber = Quote.published.count | ||
| 5 | @mnumber = Quote.pending.count | ||
| 6 | end | ||
| 7 | |||
| 8 | def latest | ||
| 9 | @quotes = Quote.published.order(id: :desc).paginate(page: params[:page], per_page: 10) | ||
| 10 | |||
| 11 | respond_to do |format| | ||
| 12 | format.html { render :list } | ||
| 13 | format.json { render :json => @quotes } | ||
| 14 | format.xml { render :xml => @quotes } | ||
| 15 | format.atom { render :atom => @quotes } | ||
| 16 | end | ||
| 17 | end | ||
| 18 | |||
| 19 | def top | ||
| 20 | @quotes = Quote.published.order(Arel.sql("(upvotes - downvotes) DESC")).paginate(page: params[:page], per_page: 10) | ||
| 21 | |||
| 22 | respond_to do |format| | ||
| 23 | format.html { render :list } | ||
| 24 | format.json { render :json => @quotes } | ||
| 25 | format.xml { render :xml => @quotes } | ||
| 26 | end | ||
| 27 | end | ||
| 28 | |||
| 29 | def tags | ||
| 30 | @tags = Quote.published.tag_counts_on(:tags) | ||
| 31 | end | ||
| 32 | |||
| 33 | def tag | ||
| 34 | @quotes = Quote.published.tagged_with(params[:id]).order(id: :desc).paginate(page: params[:page], per_page: 10) | ||
| 35 | |||
| 36 | respond_to do |format| | ||
| 37 | format.html { render :list } | ||
| 38 | format.json { render :json => @quotes } | ||
| 39 | format.xml { render :xml => @quotes } | ||
| 40 | end | ||
| 41 | end | ||
| 42 | |||
| 43 | def show | ||
| 44 | @quote = Quote.published.find(params[:id]) | ||
| 45 | |||
| 46 | respond_to do |format| | ||
| 47 | format.html | ||
| 48 | format.json { render :json => @quote } | ||
| 49 | format.xml { render :xml => @quote } | ||
| 50 | end | ||
| 51 | end | ||
| 52 | |||
| 53 | def new | ||
| 54 | end | ||
| 55 | |||
| 56 | def upvote | ||
| 57 | @quote = Quote.published.find(params[:id]) | ||
| 58 | |||
| 59 | respond_to do |format| | ||
| 60 | if @quote.upvote! request.remote_ip | ||
| 61 | format.html do | ||
| 62 | flash[:notice] = "You have upvoted Quote \"#{@quote.id}\"." | ||
| 63 | redirect_to @quote | ||
| 64 | end | ||
| 65 | format.js { render "voted" } | ||
| 66 | format.xml { head :ok } | ||
| 67 | else | ||
| 68 | format.html do | ||
| 69 | flash[:notice] = "You have already voted on Quote \"#{@quote.id}\"." | ||
| 70 | redirect_to @quote | ||
| 71 | end | ||
| 72 | format.xml { render :xml => { :error => "Someone from your IP address has already voted on this quote."} } | ||
| 73 | end | ||
| 74 | end | ||
| 75 | end | ||
| 76 | |||
| 77 | def downvote | ||
| 78 | @quote = Quote.published.find(params[:id]) | ||
| 79 | |||
| 80 | respond_to do |format| | ||
| 81 | if @quote.downvote! request.remote_ip | ||
| 82 | format.html do | ||
| 83 | flash[:notice] = "You have downvoted Quote \"#{@quote.id}\"." | ||
| 84 | redirect_to @quote | ||
| 85 | end | ||
| 86 | format.js { render "voted" } | ||
| 87 | format.xml { head :ok } | ||
| 88 | else | ||
| 89 | format.html do | ||
| 90 | flash[:notice] = "You have already voted on Quote \"#{@quote.id}\"." | ||
| 91 | redirect_to @quote | ||
| 92 | end | ||
| 93 | format.xml { render :xml => { :error => "Someone from your IP address has already voted on this quote."} } | ||
| 94 | end | ||
| 95 | end | ||
| 96 | end | ||
| 97 | end | ||
