about summary refs log tree commit diff stats
path: root/app/controllers/quotes_controller.rb
blob: 58af7bb3e2c4481b2d3baf611b1a20efb09fc1e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
class QuotesController < ApplicationController
  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])

    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?

    if user_signed_in?
      @quote.state = :published
      @quote.save!

      flash[:notice] = "Thank you for submitting your quote!"
      redirect_to @quote
    else
      flash[:error] = "There was an error."
      render :new
    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