docbrown/app/models/badge_achievement.rb
Andy Zhao 0a8f9ca654 Fix URL Validations and Update Admin Dashboard (#1392)
* Validate for trailing slash instead of strict validation

* Remove unused sign_in_count field

* Add profile URLs to edit view

* Add profile URLs to admin controller params

* Add profile URLs to edit view

* Clean up index and search views for user

* Move around user fields to make more sense

* Display more informative label

* Remove name_of_user field in favor of new display label

* Fix typo

* Remove duplication for website, employer, and Mastodon URLs

* Add score to comment params

* Remove unused name_of_user field views

* Fix typo for medium url
2018-12-26 18:00:02 -05:00

37 lines
1.1 KiB
Ruby

class BadgeAchievement < ApplicationRecord
belongs_to :user
belongs_to :badge
belongs_to :rewarder, class_name: "User", optional: true
counter_culture :user, column_name: "badge_achievements_count"
validates :badge_id, uniqueness: { scope: :user_id }
after_create :notify_recipient
after_create :send_email_notification
before_validation :render_rewarding_context_message_html
def render_rewarding_context_message_html
return if rewarding_context_message_markdown.blank?
parsed_markdown = MarkdownParser.new(rewarding_context_message_markdown)
html = parsed_markdown.finalize
final_html = ActionController::Base.helpers.sanitize html,
tags: %w(strong em i b u a code),
attributes: %w(href name)
self.rewarding_context_message = final_html
end
private
def notify_recipient
Notification.send_new_badge_notification(self)
end
def send_email_notification
if user.class.name == "User" && user.email.present? && user.email_badge_notifications
NotifyMailer.new_badge_email(self).deliver
end
end
handle_asynchronously :send_email_notification
end