Skip to content

Commit ffc7d1b

Browse files
author
Vesa Marttila
committed
Feeds can be liked.
1 parent 7269169 commit ffc7d1b

File tree

9 files changed

+68
-2
lines changed

9 files changed

+68
-2
lines changed

app/controllers/newsfeeds_controller.rb

+7
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@ class NewsfeedsController < ApplicationController
22
def index
33
@newsfeed = Newsfeed.find_latest(15)
44
end
5+
6+
def update
7+
newsfeed = Newsfeed.find(params[:id])
8+
Newsfeed.create_liking(current_user, newsfeed)
9+
flash[:notice] = "You just liked something!"
10+
redirect_to newsfeeds_url
11+
end
512
end

app/models/like.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Like < ActiveRecord::Base
2+
belongs_to :user
3+
belongs_to :newsfeed
4+
end

app/models/newsfeed.rb

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
class Newsfeed < ActiveRecord::Base
2+
has_many :likes
3+
has_many :liking_users, :through => :likes, :source => :user
4+
5+
def self.create_liking(user, newsfeed)
6+
newsfeed.liking_users << user
7+
end
8+
29
def self.user_registered(user)
310
create :message => "User #{user.username} registered."
411
end

app/models/user.rb

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class User < ActiveRecord::Base
2121
has_many :courses_faileds
2222
has_many :failed_courses, :through => :courses_faileds, :source =>
2323
:course
24+
has_many :likes
25+
has_many :liked_feeds, :through => :likes, :source => :newsfeed
2426

2527
after_create :update_newsfeed
2628

app/views/newsfeeds/index.html.erb

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
<h2>Newsfeed</h2>
22

33
<% @newsfeed.each do |item| %>
4-
<p><%= item.message %> <%= time_ago_in_words(item.created_at) %> ago</p>
4+
<p><%= item.message %> <%= time_ago_in_words(item.created_at) %> ago
5+
<% unless item.liking_users.include?(current_user) %>
6+
<%= link_to 'Like', newsfeed_url(item), :method => :put %>
7+
<% end %>
8+
<% unless item.liking_users.empty? %>
9+
Liked by:
10+
<% item.liking_users.each do |u| %>
11+
<%= u.username %>
12+
<% end %>
13+
<% end %>
14+
</p>
515
<% end %>
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class CreateLikes < ActiveRecord::Migration
2+
def self.up
3+
create_table :likes do |t|
4+
t.integer :user_id
5+
t.integer :newsfeed_id
6+
7+
t.timestamps
8+
end
9+
end
10+
11+
def self.down
12+
drop_table :likes
13+
end
14+
end

db/schema.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#
1010
# It's strongly recommended to check this file into your version control system.
1111

12-
ActiveRecord::Schema.define(:version => 20091210231225) do
12+
ActiveRecord::Schema.define(:version => 20091211000141) do
1313

1414
create_table "categories", :force => true do |t|
1515
t.string "name"
@@ -63,6 +63,13 @@
6363
t.datetime "updated_at"
6464
end
6565

66+
create_table "likes", :force => true do |t|
67+
t.integer "user_id"
68+
t.integer "newsfeed_id"
69+
t.datetime "created_at"
70+
t.datetime "updated_at"
71+
end
72+
6673
create_table "newsfeeds", :force => true do |t|
6774
t.text "message"
6875
t.datetime "created_at"

test/fixtures/likes.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2+
3+
# one:
4+
# column: value
5+
#
6+
# two:
7+
# column: value

test/unit/like_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'test_helper'
2+
3+
class LikeTest < ActiveSupport::TestCase
4+
# Replace this with your real tests.
5+
test "the truth" do
6+
assert true
7+
end
8+
end

0 commit comments

Comments
 (0)