Class: Comment

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
app/models/comment.rb

Overview

Comment is a polymorphic class. Any ‘commentable’ can have comments.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) build_and_validate_comment(commentable, form_params)



35
36
37
38
39
40
41
42
43
# File 'app/models/comment.rb', line 35

def self.build_and_validate_comment(commentable, form_params)
  comment = commentable.comments.build(form_params)
  if comment.valid?
    comment.save
  else
    errors =  comment.errors.full_messages.join("<br/>").html_safe
  end
  [comment, errors]
end

Instance Method Details

- (Object) commentable_url



57
58
59
60
61
62
63
64
65
66
# File 'app/models/comment.rb', line 57

def commentable_url
  path = "http://#{DEFAULT_URL}/"
  case self.commentable_type
  when Posting
    path += "blogs/#{self.commentable.blog_id.to_s}/postings/#{self.commentable_id}"
  else
    path += "#{self.commentable.class.to_s.pluralize.downcase}/#{self.commentable_id}"
  end
  path
end

- (Object) time_left_to_edit

Calculate the time left a user can edit a comment



26
27
28
29
30
31
32
# File 'app/models/comment.rb', line 26

def time_left_to_edit
  unless self.new_record?
    @time_left_to_edit ||= CONSTANTS['max_time_to_edit_new_comments'].to_i - ( (Time.now()-self.updated_at )/1.minute ).to_i
  else
    -1
  end
end

- (Object) update_session_comments(comments)



45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/comment.rb', line 45

def update_session_comments(comments)
  begin
    remove_old_comments(comments) << [self.id.to_s,self.updated_at.to_i]
  rescue => e
    Rails.logger.warn(
      "***WARNING*** #{e.inspect} *** "+ "RESET SESSION COMMENTS "+
      "#{__FILE__}:#{__LINE__}"
    )
    [self.id.to_s,self.updated_at.to_i]
  end
end

- (Object) user



68
69
70
# File 'app/models/comment.rb', line 68

def user
  User.where( :name => self.name, :email => self.).first
end