diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb index 1184256..f143c17 100644 --- a/app/controllers/bookmarks_controller.rb +++ b/app/controllers/bookmarks_controller.rb @@ -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 diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index 2284422..27a9d42 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -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 diff --git a/app/models/like_activity.rb b/app/models/like_activity.rb new file mode 100644 index 0000000..cd9c5e9 --- /dev/null +++ b/app/models/like_activity.rb @@ -0,0 +1,7 @@ +class LikeActivity < UserActivity + delegate :title, to: :recipe, prefix: true + + def recipe + target + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 63dddd0..b5a7031 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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) diff --git a/app/services/recipe_liking.rb b/app/services/recipe_liking.rb new file mode 100644 index 0000000..9bc55cf --- /dev/null +++ b/app/services/recipe_liking.rb @@ -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 diff --git a/app/services/user_activity_publisher.rb b/app/services/user_activity_publisher.rb index f97f2ad..8e4bdb5 100644 --- a/app/services/user_activity_publisher.rb +++ b/app/services/user_activity_publisher.rb @@ -24,7 +24,8 @@ def run private ACTIVITY_MAP = { - bookmark: BookmarkActivity + bookmark: BookmarkActivity, + like: LikeActivity }.freeze private_constant :ACTIVITY_MAP diff --git a/app/views/like_activities/_like_activity.html.erb b/app/views/like_activities/_like_activity.html.erb new file mode 100644 index 0000000..e29ae3f --- /dev/null +++ b/app/views/like_activities/_like_activity.html.erb @@ -0,0 +1,2 @@ +<%= t(".liked") %> +<%= link_to like_activity.recipe_title, like_activity.recipe %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 92f8c08..b7a7bdf 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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"