Skip to content

Commit

Permalink
like activity
Browse files Browse the repository at this point in the history
  • Loading branch information
aqeelvn committed Jun 8, 2017
1 parent bf52985 commit 2a382ef
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 7 deletions.
3 changes: 1 addition & 2 deletions app/controllers/bookmarks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ class BookmarksController < ApplicationController
before_action :require_login

def create
bookmark = RecipeBookmarking.new(user: current_user, recipe:recipe).run
current_user.bookmark(recipe)
RecipeBookmarking.new(user: current_user, recipe:recipe).run
redirect_to recipe
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/likes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class LikesController < ApplicationController
before_action :require_login

def create
current_user.like(recipe)
RecipeLiking.new(user: current_user, recipe:recipe).run
redirect_to recipe
end

Expand Down
7 changes: 7 additions & 0 deletions app/models/like_activity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class LikeActivity < UserActivity
delegate :title, to: :recipe, prefix: true

def recipe
target
end
end
4 changes: 3 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def unfollow(user)
end

def like(recipe)
liked_recipes << recipe
likes.find_or_create_by(recipe:recipe)
rescue ActiveRecord::RecordNotUnique
retry
end

def liked?(recipe)
Expand Down
26 changes: 26 additions & 0 deletions app/services/recipe_liking.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class RecipeLiking
def initialize(user:, recipe:)
@user = user
@recipe = recipe
end

def run
like = create_like

if like.persisted?
publish_activity
end
end

private

attr_reader :user, :recipe

def create_like
user.like(recipe)
end

def publish_activity
UserActivityPublisher.new(user:user, target: recipe, action: "like").run
end
end
3 changes: 2 additions & 1 deletion app/services/user_activity_publisher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def run
private

ACTIVITY_MAP = {
bookmark: BookmarkActivity
bookmark: BookmarkActivity,
like: LikeActivity
}.freeze

private_constant :ACTIVITY_MAP
Expand Down
2 changes: 2 additions & 0 deletions app/views/like_activities/_like_activity.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= t(".liked") %>
<%= link_to like_activity.recipe_title, like_activity.recipe %>
14 changes: 12 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,37 @@ en:
bookmark: "Bookmark"
remove_bookmark: "Remove bookmark"
home: "Home"
user_activities:

bookmark_activities:
bookmark_activity:
bookmarked: "Bookmarked"
bookmarked: "bookmarked"

like_activities:
like_activity:
liked: "liked"

my:
recipes:
form:
add_ingredient: "Add ingredient"

recipes:
comments:
remove_comment: "Delete"
controls:
confirm_delete: "Are you sure you want to delete the recipe?"

users:
show:
users_recipes: "%{username}'s recipes"

helpers:
submit:
recipe:
create: "Create recipe"
comment:
create: "Submit"

homes:
show:
create_recipe: "Create recipe"

0 comments on commit 2a382ef

Please sign in to comment.