Class: Hubstats::Comment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/hubstats/comment.rb

Class Method Summary collapse

Class Method Details

.create_or_update(github_comment) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/hubstats/comment.rb', line 25

def self.create_or_update(github_comment)
  github_comment = github_comment.to_h.with_indifferent_access if github_comment.respond_to? :to_h

  user = Hubstats::User.create_or_update(github_comment[:user])
  github_comment[:user_id] = user.id
  
  if github_comment[:pull_number]
    pull_request = Hubstats::PullRequest.belonging_to_repo(github_comment[:repo_id]).where(number: github_comment[:pull_number]).first
    if pull_request
      github_comment[:pull_request_id] = pull_request.id
    end
  end

  comment_data = github_comment.slice(*Hubstats::Comment.column_names.map(&:to_sym))

  comment = where(:id => comment_data[:id]).first_or_create(comment_data)
  return comment if comment.update_attributes(comment_data)
  Rails.logger.warn comment.errors.inspect
end