Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class PhrasesController < ApplicationController
def index
set_cookies
@phrases_left = avaible_ids(cookies[:showed_ph]).count
@phrases = Phrase.find(cookies_to_array(cookies[:showed_ph]))
end
def random_phrase
all_ids = avaible_ids(cookies[:showed_ph])
@phrases_left = all_ids.count - 1
respond_to do |format|
if all_ids.count != 0
id = all_ids.sample
cookies[:showed_ph] += "#{id} "
@phrase = Phrase.find(id)
format.js {}
else
format.js { render js: 'alert("no more phrases in db");' }
end
end
end
def clear_phrases
cookies.delete(:showed_ph)
set_cookies
@phrases_left = Phrase.count
respond_to { |f| f.js }
end
private
def avaible_ids(showed_phrases)
(1..Phrase.count).to_a - cookies_to_array(showed_phrases)
end
def cookies_to_array(cookie)
cookie.split(' ').map(&:to_i)
end
def set_cookies
cookies.permanent[:showed_ph] ||= ""
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.