Class: Lentil::ThisorthatController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- Lentil::ThisorthatController
- Defined in:
- app/controllers/lentil/thisorthat_controller.rb
Instance Method Summary collapse
- #battle ⇒ Object
- #battle_leaders ⇒ Object
- #get_images ⇒ Object
- #result ⇒ Object
- #test_urls(images, quantity) ⇒ Object
Instance Method Details
#battle ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/controllers/lentil/thisorthat_controller.rb', line 39 def battle @title = "Battle" @images = get_images @prev_winner = session[:prev_winner] || nil prev_images = session[:prev_images] || [] unless prev_images.empty? begin @prev_images = Image.find(session[:prev_images]).sort_by{ |i| i.id } rescue @prev_images = [] end else @prev_images = [] end if @images.length < 2 Rails.logger.error "Battle Image Failure: possible service outage" render "/lentil/thisorthat/battle_error" end end |
#battle_leaders ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'app/controllers/lentil/thisorthat_controller.rb', line 18 def battle_leaders @title = "Battle Leaders" @images = Image.approved.where("win_pct IS NOT NULL").select{ |i| i.battles_count > 4} @images = @images.sort_by{ |i| i.win_pct}.reverse.first(25) render :action => "../images/index" end |
#get_images ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/controllers/lentil/thisorthat_controller.rb', line 27 def get_images images = Image.approved.order_by_rand.limit(20).sort_by{|i| i.battles_count} images = test_urls(images, 2).sort_by{ |i| i.id } session[:battle_images] = [] images.each do |image| session[:battle_images] << image.id end return images end |
#result ⇒ Object
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 |
# File 'app/controllers/lentil/thisorthat_controller.rb', line 62 def result viewed_ids = session[:battle_images] || [] submitted_ids = [] params['image'].each do |image| submitted_ids << image[0].to_i end winner_id = params['vote'].to_i cond_one = submitted_ids.length == 2 cond_two = (submitted_ids - viewed_ids).size == 0 cond_three = submitted_ids.include? winner_id if cond_one && cond_two && cond_three loser_id = submitted_ids.reject{|id| id == winner_id}.first begin winner = Image.find(winner_id) loser = Image.find(loser_id) rescue => e = "Battle not saved: " << e. Rails.logger.error else winner.won_battles.create(:loser => loser) end else = "Battle not saved" << ": submitted_ids length is not equal to 2" unless cond_one << ": submitted_ids not in viewed_ids" unless cond_two << ": submitted_ids do not include winner_id" unless cond_three Rails.logger.error end respond_with do |format| format.html do if request.xhr? @images = get_images if @images.length < 2 render :partial => "/layouts/lentil/error", :locals => {:error_message => "Sorry, there was a problem loading images."} else session[:prev_images] = submitted_ids session[:prev_winner] = winner_id @prev_images = Lentil::Image.find(submitted_ids).sort_by{ |i| i.id } @prev_winner = session[:prev_winner] render :partial => "/lentil/thisorthat/battle_form", :locals => { :images => @images, :prev_images => @prev_images, :prev_winner => @prev_winner }, :layout => false, :status => :created end else session[:prev_images] = submitted_ids session[:prev_winner] = winner_id redirect_to :back end end end end |
#test_urls(images, quantity) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'app/controllers/lentil/thisorthat_controller.rb', line 7 def test_urls(images, quantity) image = images.selecting{ |i| begin res = OEmbed::Providers::Instagram.get(i.url) true rescue false end }.first(quantity) end |