main
rb 28 lines 990 Bytes
Raw
1 # Toggles a star (a "like" for models, a star for code repos) for the signed-in
2 # user on a repository. One endpoint flips the state in both directions.
3 class StarsController < ApplicationController
4 before_action :require_sign_in!
5 before_action :load_repository
6
7 def create
8 star = current_user.stars.find_or_initialize_by(repository: @repository)
9 if star.persisted?
10 star.destroy
11 else
12 star.save
13 end
14 redirect_back fallback_location: repository_path(@repository.user.username, @repository.name)
15 end
16
17 private
18
19 def load_repository
20 owner = User.find_by!(username: params[:username])
21 @repository = owner.repositories.find_by!(name: params[:repository])
22 unless !@repository.is_private || current_user == owner
23 render file: Rails.public_path.join("404.html"), status: :not_found, layout: false
24 end
25 rescue ActiveRecord::RecordNotFound
26 render file: Rails.public_path.join("404.html"), status: :not_found, layout: false
27 end
28 end