osem-fcy/app/models/comment.rb
AEtherC0r3 efaf07178f Upgrade to Rails 5
Update config with rails app:update
Update schema.rb rails db:migrate
Add puma
Make jobs and models inherit from ApplicationJob and ApplicationRecord
Update acts_as_list to 0.9.7 in order to fix
"undefined method `sanitize_sql_hash_for_conditions'" error
Update web-console to 2.3.0 to fix a 500 internal server error
Replace before_filter with before_action
Add rails-controller-testing gem
Add prepend: :true to protect_from_forgery in ApplicationController to
avoid ActionController::InvalidAuthenticityToken exceptions
Remove activeuuid
Update formtastic to 3.1.5 to fix deprecation warnings and issues
with the Input class
Update ahoy_matey to 1.6.0
Update cancancan to 2.0.0 to fix issues with malformed sql queries
Fix program spec
Fix issue with the picture being nil in admin/Organizations#new and #edit
and Organizations#show
Fix ActiveRecord::Base.raise_in_transactional_callbacks= deprecation
warning by removing an unnecessary line in application.rb
Fix failing versions specs
2017-12-11 20:58:04 +02:00

67 lines
1.9 KiB
Ruby

class Comment < ApplicationRecord
acts_as_nested_set scope: %i(commentable_id commentable_type)
validates :body, presence: true
validates :user, presence: true
after_create :send_notification
# NOTE: install the acts_as_votable plugin if you
# want user to vote on the quality of comments.
#acts_as_votable
belongs_to :commentable, counter_cache: true, polymorphic: true
# NOTE: Comments belong to a user
belongs_to :user
has_paper_trail on: %i(create destroy), meta: { conference_id: :conference_id }
# Helper class method that allows you to build a comment
# by passing a commentable object, a user_id, and comment text
# example in readme
def self.build_from(obj, user_id, comment)
new \
commentable: obj,
body: comment,
user_id: user_id
end
#helper method to check if a comment has children
def has_children?
children.any?
end
# Helper class method to lookup all comments assigned
# to all commentable types for a given user.
scope :find_comments_by_user, lambda { |user|
where(user_id: user.id).order('created_at DESC')
}
# Helper class method to look up all comments for
# commentable class name and commentable id.
scope :find_comments_for_commentable, lambda { |commentable_str, commentable_id|
where(commentable_type: commentable_str.to_s, commentable_id: commentable_id).order('created_at DESC')
}
scope :find_since_last_login, lambda { |user|
if user.last_sign_in_at
where(created_at: (user.last_sign_in_at..Time.now)).order(created_at: :desc)
else
none
end
}
# Helper class method to look up a commentable object
# given the commentable class name and id
def self.find_commentable(commentable_str, commentable_id)
commentable_str.constantize.find(commentable_id)
end
private
def send_notification
EventCommentMailJob.perform_later(self)
end
def conference_id
commentable.program.conference_id
end
end