From 4c5cf4a54217cc2f10796f81a01be901be2b0a2b Mon Sep 17 00:00:00 2001 From: joshua stein Date: Mon, 2 May 2016 17:57:19 -0500 Subject: [PATCH] require a minimum karma to suggest story title/tag suggestions --- app/controllers/stories_controller.rb | 10 ++++++++++ app/models/story.rb | 6 +----- app/models/user.rb | 7 +++++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index 7560661..ace3949 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -171,6 +171,11 @@ class StoriesController < ApplicationController end def suggest + if !@story.can_have_suggestions_from_user?(@user) + flash[:error] = "You are not allowed to offer suggestions on that story." + return redirect_to @story.comments_path + end + if (st = @story.suggested_taggings.where(:user_id => @user.id)).any? @story.tags_a = st.map{|st| st.tag.tag } end @@ -180,6 +185,11 @@ class StoriesController < ApplicationController end def submit_suggestions + if !@story.can_have_suggestions_from_user?(@user) + flash[:error] = "You are not allowed to offer suggestions on that story." + return redirect_to @story.comments_path + end + ostory = @story.dup @story.title = params[:story][:title] diff --git a/app/models/story.rb b/app/models/story.rb index d61d0d7..424809e 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -219,11 +219,7 @@ class Story < ActiveRecord::Base end def can_have_suggestions_from_user?(user) - if !user - return false - end - - if user.id == self.user_id + if !user || (user.id == self.user_id) || !user.can_offer_suggestions? return false end diff --git a/app/models/user.rb b/app/models/user.rb index 5e58fdd..aa882de 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -59,6 +59,9 @@ class User < ActiveRecord::Base # days old accounts are considered new for NEW_USER_DAYS = 7 + # minimum karma required to be able to offer title/tag suggestions + MIN_KARMA_TO_SUGGEST = 10 + def self.recalculate_all_karmas! User.all.each do |u| u.karma = u.stories.map(&:score).sum + u.comments.map(&:score).sum @@ -146,6 +149,10 @@ class User < ActiveRecord::Base false end + def can_offer_suggestions? + !self.is_new? && (self.karma >= MIN_KARMA_TO_SUGGEST) + end + def check_session_token if self.session_token.blank? self.session_token = Utils.random_str(60)