diff options
Diffstat (limited to 'app/controllers/quotes_controller.rb')
| -rw-r--r-- | app/controllers/quotes_controller.rb | 260 |
1 files changed, 260 insertions, 0 deletions
| diff --git a/app/controllers/quotes_controller.rb b/app/controllers/quotes_controller.rb new file mode 100644 index 0000000..468f6ea --- /dev/null +++ b/app/controllers/quotes_controller.rb | |||
| @@ -0,0 +1,260 @@ | |||
| 1 | class QuotesController < ApplicationController | ||
| 2 | protect_from_forgery with: :null_session, if: -> { request.format.json? or request.format.xml? } | ||
| 3 | |||
| 4 | def index | ||
| 5 | @quote = Quote.find(310) | ||
| 6 | @qnumber = Quote.published.count | ||
| 7 | @mnumber = Quote.pending.count | ||
| 8 | end | ||
| 9 | |||
| 10 | def latest | ||
| 11 | @quotes = Quote.published.order(id: :desc).paginate(page: params[:page], per_page: 10) | ||
| 12 | |||
| 13 | respond_to do |format| | ||
| 14 | format.html { render :list } | ||
| 15 | format.json { render :json => @quotes } | ||
| 16 | format.xml { render :xml => @quotes } | ||
| 17 | format.atom { render :atom => @quotes } | ||
| 18 | end | ||
| 19 | end | ||
| 20 | |||
| 21 | def top | ||
| 22 | @quotes = Quote.published | ||
| 23 | @quotes = @quotes.where("created_at > ?", Time.new(2015,1,1)) if params[:modern] | ||
| 24 | @quotes = @quotes.order(Arel.sql("(upvotes - downvotes) DESC, id ASC")).paginate(page: params[:page], per_page: 10) | ||
| 25 | |||
| 26 | respond_to do |format| | ||
| 27 | format.html { render :list } | ||
| 28 | format.json { render :json => @quotes } | ||
| 29 | format.xml { render :xml => @quotes } | ||
| 30 | end | ||
| 31 | end | ||
| 32 | |||
| 33 | def search_form | ||
| 34 | @q = Quote.published.ransack | ||
| 35 | end | ||
| 36 | |||
| 37 | def search | ||
| 38 | @quotes = Quote.published.ransack(params[:q]).result(distinct: true).paginate(page: params[:page], per_page: 10) | ||
| 39 | |||
| 40 | respond_to do |format| | ||
| 41 | format.html { render :list } | ||
| 42 | format.json { render :json => @quotes } | ||
| 43 | format.xml { render :xml => @quotes } | ||
| 44 | end | ||
| 45 | end | ||
| 46 | |||
| 47 | def random | ||
| 48 | picked = Quote.where(state: :published).ids.sample | ||
| 49 | redirect_to quote_url(picked) | ||
| 50 | end | ||
| 51 | |||
| 52 | def tags | ||
| 53 | @tags = Quote.published.tag_counts_on(:tags) | ||
| 54 | end | ||
| 55 | |||
| 56 | def tag | ||
| 57 | @quotes = Quote.published.tagged_with(params[:id]).order(id: :desc).paginate(page: params[:page], per_page: 10) | ||
| 58 | |||
| 59 | respond_to do |format| | ||
| 60 | format.html { render :list } | ||
| 61 | format.json { render :json => @quotes } | ||
| 62 | format.xml { render :xml => @quotes } | ||
| 63 | end | ||
| 64 | end | ||
| 65 | |||
| 66 | def stats | ||
| 67 | @months = [] | ||
| 68 | 0.upto(11) do |i| | ||
| 69 | month = i.months.ago.month | ||
| 70 | year = i.months.ago.year | ||
| 71 | beginning = i.months.ago.beginning_of_month.beginning_of_day | ||
| 72 | endtime = i.months.ago.end_of_month.end_of_day | ||
| 73 | quotes = Quote.published.where("created_at >= :start_date AND created_at <= :end_date", { :start_date => beginning, :end_date => endtime }).all | ||
| 74 | @months[11-i] = { :name => Date::MONTHNAMES[month], :number => quotes.count } | ||
| 75 | end | ||
| 76 | |||
| 77 | #@months2 = [] | ||
| 78 | #i = 0 | ||
| 79 | #while true | ||
| 80 | # month = i.months.ago.month | ||
| 81 | # year = i.months.ago.year | ||
| 82 | # beginning = i.months.ago.beginning_of_month.beginning_of_day | ||
| 83 | # endtime = i.months.ago.end_of_month.end_of_day | ||
| 84 | # quotes = Quote.where("created_at <= :end_date", { :end_date => endtime }).all | ||
| 85 | # @months2[i] = { :name => i.months.ago.strftime("%Y-%m"), :number => quotes.count } | ||
| 86 | # | ||
| 87 | # break if month == 4 and year == 2008 | ||
| 88 | # | ||
| 89 | # i=i+1 | ||
| 90 | #end | ||
| 91 | # | ||
| 92 | #@months2.reverse! | ||
| 93 | # | ||
| 94 | #@upvotes = Vote.where(upvote: 1, votable_type: :quote).count | ||
| 95 | #@downvotes = Vote.where(upvote: 0, votable_type: :quote).count | ||
| 96 | |||
| 97 | hardcoded_aliases = { | ||
| 98 | "hatkirby": "Hatkirby", | ||
| 99 | "Gryphic": "Drifty", | ||
| 100 | "DriftyBeyond": "Drifty", | ||
| 101 | "Starla": "Hatkirby", | ||
| 102 | "BleuM937": "Bluemonkey", | ||
| 103 | "BlueM937": "Bluemonkey", | ||
| 104 | "Drifty1": "Drifty", | ||
| 105 | "Starla Insigna": "Hatkirby", | ||
| 106 | "Starla Alice Insigna": "Hatkirby", | ||
| 107 | "tamasys": "Tamasys", | ||
| 108 | "Student 2": "Student", | ||
| 109 | "Старла Эппрет": "Hatkirby", | ||
| 110 | "Girl Without Slipper": "Hatkirby", | ||
| 111 | "Actias": "Drifty", | ||
| 112 | "Salaboy123": "Tamasys", | ||
| 113 | "TaMACsys": "Tamasys", | ||
| 114 | "RealityCheck": "Drifty", | ||
| 115 | "Student 3": "Student", | ||
| 116 | "toothpastecanyon": "ToothPasteCanyon", | ||
| 117 | "eka-caesium": "eka_caesium", | ||
| 118 | } | ||
| 119 | |||
| 120 | person_count = {} | ||
| 121 | Quote.published.each do |quote| | ||
| 122 | person_in_quote = {} | ||
| 123 | |||
| 124 | quote.content.split("\n").each do |line| | ||
| 125 | check = line.chomp | ||
| 126 | hi = /([\(\[][^\]\)]*[\]\)] )?([^:]*): /.match(check) | ||
| 127 | if hi and hi[2] then | ||
| 128 | person = hi[2] | ||
| 129 | if hardcoded_aliases.has_key? person.intern | ||
| 130 | person = hardcoded_aliases[person.intern] | ||
| 131 | end | ||
| 132 | unless person_in_quote.has_key? person | ||
| 133 | person_count[person] ||= 0 | ||
| 134 | person_count[person] += 1 | ||
| 135 | person_in_quote[person] = 1 | ||
| 136 | end | ||
| 137 | end | ||
| 138 | end | ||
| 139 | end | ||
| 140 | |||
| 141 | @by_speaker = person_count.to_a.sort_by {|hi| hi[1]}.reverse.take(20) | ||
| 142 | end | ||
| 143 | |||
| 144 | def show | ||
| 145 | @quote = Quote.published.find(params[:id]) | ||
| 146 | |||
| 147 | set_meta_tags(og: { | ||
| 148 | title: "Quote \##{@quote.id}", | ||
| 149 | type: "article", | ||
| 150 | description: (@quote.content.length <= 300 ? @quote.content : @quote.content[0..299]), | ||
| 151 | url: quote_url(@quote, host: "www.fourisland.com"), | ||
| 152 | article: { | ||
| 153 | published_time: @quote.created_at.iso8601, | ||
| 154 | modified_time: @quote.updated_at.iso8601 | ||
| 155 | } | ||
| 156 | }) | ||
| 157 | |||
| 158 | respond_to do |format| | ||
| 159 | format.html | ||
| 160 | format.json { render :json => @quote } | ||
| 161 | format.xml { render :xml => @quote } | ||
| 162 | end | ||
| 163 | end | ||
| 164 | |||
| 165 | def new | ||
| 166 | @quote = Quote.new | ||
| 167 | end | ||
| 168 | |||
| 169 | def create | ||
| 170 | @quote = Quote.new(quote_params) | ||
| 171 | @quote.submitter = nil if @quote.submitter.empty? | ||
| 172 | |||
| 173 | unless @quote.valid? | ||
| 174 | flash.alert = "Error submitting quote." | ||
| 175 | |||
| 176 | respond_to do |format| | ||
| 177 | format.html { render :new } | ||
| 178 | format.json { render json: { error: "Error submitting quote." }, status: :bad_request } | ||
| 179 | format.xml { render xml: { error: "Error submitting quote." }, status: :bad_request } | ||
| 180 | end | ||
| 181 | |||
| 182 | return | ||
| 183 | end | ||
| 184 | |||
| 185 | if user_signed_in? | ||
| 186 | @quote.state = :published | ||
| 187 | @quote.save! | ||
| 188 | |||
| 189 | flash[:notice] = "Thank you for submitting your quote!" | ||
| 190 | respond_to do |format| | ||
| 191 | format.html { redirect_to @quote } | ||
| 192 | format.json { render json: @quote } | ||
| 193 | format.xml { render xml: @quote } | ||
| 194 | end | ||
| 195 | else | ||
| 196 | @quote.state = :pending | ||
| 197 | @quote.save! | ||
| 198 | |||
| 199 | # hacky little thing to reduce the number of emails I get | ||
| 200 | unless @quote.submitter == "hatkirby" | ||
| 201 | QuoteMailer.with(quote: @quote).pending_quote_email.deliver_later | ||
| 202 | end | ||
| 203 | |||
| 204 | flash[:notice] = "Your quote has been submitted and is pending moderation." | ||
| 205 | respond_to do |format| | ||
| 206 | format.html { redirect_to new_quote_url } | ||
| 207 | format.json { render json: @quote } | ||
| 208 | format.xml { render xml: @quote } | ||
| 209 | end | ||
| 210 | end | ||
| 211 | end | ||
| 212 | |||
| 213 | def upvote | ||
| 214 | @quote = Quote.published.find(params[:id]) | ||
| 215 | |||
| 216 | respond_to do |format| | ||
| 217 | if @quote.upvote! request.remote_ip | ||
| 218 | format.html do | ||
| 219 | flash[:notice] = "You have upvoted Quote \"#{@quote.id}\"." | ||
| 220 | redirect_to @quote | ||
| 221 | end | ||
| 222 | format.js { render "voted" } | ||
| 223 | format.xml { head :ok } | ||
| 224 | else | ||
| 225 | format.html do | ||
| 226 | flash[:notice] = "You have already voted on Quote \"#{@quote.id}\"." | ||
| 227 | redirect_to @quote | ||
| 228 | end | ||
| 229 | format.xml { render :xml => { :error => "Someone from your IP address has already voted on this quote."} } | ||
| 230 | end | ||
| 231 | end | ||
| 232 | end | ||
| 233 | |||
| 234 | def downvote | ||
| 235 | @quote = Quote.published.find(params[:id]) | ||
| 236 | |||
| 237 | respond_to do |format| | ||
| 238 | if @quote.downvote! request.remote_ip | ||
| 239 | format.html do | ||
| 240 | flash[:notice] = "You have downvoted Quote \"#{@quote.id}\"." | ||
| 241 | redirect_to @quote | ||
| 242 | end | ||
| 243 | format.js { render "voted" } | ||
| 244 | format.xml { head :ok } | ||
| 245 | else | ||
| 246 | format.html do | ||
| 247 | flash[:notice] = "You have already voted on Quote \"#{@quote.id}\"." | ||
| 248 | redirect_to @quote | ||
| 249 | end | ||
| 250 | format.xml { render :xml => { :error => "Someone from your IP address has already voted on this quote."} } | ||
| 251 | end | ||
| 252 | end | ||
| 253 | end | ||
| 254 | |||
| 255 | private | ||
| 256 | |||
| 257 | def quote_params | ||
| 258 | params.require(:quote).permit(:content, :notes, :submitter, :tag_list) | ||
| 259 | end | ||
| 260 | end | ||
