Class: VoteFu::VotesController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- ApplicationController
- VoteFu::VotesController
- Defined in:
- app/controllers/vote_fu/votes_controller.rb
Instance Method Summary collapse
-
#create ⇒ Object
POST /vote_fu/votes Creates or updates a vote.
-
#destroy ⇒ Object
DELETE /vote_fu/votes/:id Removes a vote.
-
#toggle ⇒ Object
POST /vote_fu/votes/toggle Toggles between upvote/remove or downvote/remove.
-
#update ⇒ Object
PATCH/PUT /vote_fu/votes/:id Updates an existing vote's value.
Instance Method Details
#create ⇒ Object
POST /vote_fu/votes Creates or updates a vote
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/controllers/vote_fu/votes_controller.rb', line 10 def create value = vote_value_from_params if VoteFu.configuration.allow_recast @vote = current_voter.vote_on(@voteable, value: value, scope: vote_scope) else existing = find_existing_vote if existing respond_with_error("You have already voted on this item") return end @vote = current_voter.vote_on(@voteable, value: value, scope: vote_scope) end respond_with_vote(:created) rescue StandardError => e respond_with_error(e.) end |
#destroy ⇒ Object
DELETE /vote_fu/votes/:id Removes a vote
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/controllers/vote_fu/votes_controller.rb', line 55 def destroy @vote = if params[:id].present? && params[:id] != "remove" Vote.find_by(id: params[:id], voter: current_voter) else find_existing_vote end unless @vote respond_with_error("Vote not found") return end @vote.destroy! @voteable.reload respond_to do |format| format.turbo_stream { render_vote_turbo_stream(:removed) } format.html { redirect_back fallback_location: main_app.root_path } format.json { render json: vote_json_response(:removed), status: :ok } end rescue StandardError => e respond_with_error(e.) end |
#toggle ⇒ Object
POST /vote_fu/votes/toggle Toggles between upvote/remove or downvote/remove
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'app/controllers/vote_fu/votes_controller.rb', line 81 def toggle direction = params[:direction]&.to_sym || :up existing = find_existing_vote if existing if (direction == :up && existing.up?) || (direction == :down && existing.down?) # Same direction - remove the vote existing.destroy! @voteable.reload @vote = nil respond_to do |format| format.turbo_stream { render_vote_turbo_stream(:removed) } format.html { redirect_back fallback_location: main_app.root_path } format.json { render json: vote_json_response(:removed), status: :ok } end else # Different direction - change the vote (if allowed) if VoteFu.configuration.allow_recast value = direction == :up ? 1 : -1 existing.update!(value: value) @vote = existing @voteable.reload respond_with_vote(:ok) else respond_with_error("Vote recasting is disabled") end end else # No existing vote - create one value = direction == :up ? 1 : -1 @vote = current_voter.vote_on(@voteable, value: value, scope: vote_scope) respond_with_vote(:created) end rescue StandardError => e respond_with_error(e.) end |
#update ⇒ Object
PATCH/PUT /vote_fu/votes/:id Updates an existing vote's value
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/controllers/vote_fu/votes_controller.rb', line 31 def update @vote = Vote.find_by(id: params[:id], voter: current_voter) unless @vote respond_with_error("Vote not found") return end unless VoteFu.configuration.allow_recast respond_with_error("Vote recasting is disabled") return end value = vote_value_from_params @vote.update!(value: value) @voteable.reload respond_with_vote(:ok) rescue StandardError => e respond_with_error(e.) end |