Skip to content

Commit f21fc50

Browse files
committed
the cookies
1 parent c3cac42 commit f21fc50

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

app/controllers/quotes_controller.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ def random
2121
end
2222
end
2323

24+
def read
25+
@quotes = Quote.where(id: cookies[:quotes_read].split(/,\s*/)) rescue []
26+
respond_to do |format|
27+
format.text do
28+
render text: @quotes.map(&:as_text).join("\n")
29+
end
30+
format.json { render json: @quotes }
31+
end
32+
end
33+
2434
# GET /quotes
2535
# GET /quotes.json
2636
def index
@@ -33,6 +43,10 @@ def index
3343
@quotes ||= Quote.where(language: request.env["HTTP_ACCEPT_LANGUAGE"])
3444
end
3545

46+
if params[:unread].present? && !cookies[:quotes_read].blank?
47+
@quotes = (@quotes || Quote).where("id NOT IN (?)", cookies[:quotes_read].split(/,\s*/))
48+
end
49+
3650
@quotes ||= Quote.all
3751

3852
respond_to do |format|
@@ -49,6 +63,10 @@ def index
4963
def show
5064
@quote = Quote.find_by_id(params[:id])
5165

66+
quotes_read = cookies[:quotes_read].split(/,\s*/) rescue []
67+
quotes_read << @quote.id.to_s
68+
cookies[:quotes_read] = quotes_read.join(",")
69+
5270
respond_to do |format|
5371
format.html # show.html.erb
5472
format.json do

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
root :to => redirect('/quotes')
33
resources :quotes do
44
get :random, on: :collection
5+
get :read, on: :collection
56
end
67

78
# The priority is based upon order of creation:

0 commit comments

Comments
 (0)