Skip to content

Instantly share code, notes, and snippets.

@bplexico
Created May 26, 2013 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bplexico/5653972 to your computer and use it in GitHub Desktop.
Save bplexico/5653972 to your computer and use it in GitHub Desktop.
final post refactoring
# Ensure maximum amount of mentions in a post
def max_mention_validator
if acting_user_is_trusted?
add_error_if_count_exceeded(:too_many_mentions, raw_mentions.size, SiteSetting.max_mentions_per_post)
else
add_error_if_count_exceeded(:too_many_mentions_newuser, raw_mentions.size, SiteSetting.newuser_max_mentions_per_post)
end
end
# Ensure new users can not put too many images in a post
def max_images_validator
add_error_if_count_exceeded(:too_many_images, image_count, SiteSetting.newuser_max_images) unless acting_user_is_trusted?
end
# Ensure new users can not put too many links in a post
def max_links_validator
add_error_if_count_exceeded(:too_many_links, link_count, SiteSetting.newuser_max_links) unless acting_user_is_trusted?
end
...
private
def acting_user_is_trusted?
acting_user.present? && acting_user.has_trust_level?(:basic)
end
def add_error_if_count_exceeded(key_for_translation, current_count, max_count)
errors.add(:base, I18n.t(key_for_translation, count: max_count)) if current_count > max_count
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment