Sha256: 2755e57fea7917c0e43310e123d1bc870d84a592cfe1459680cab935c6d83a50

Contents?: true

Size: 1.89 KB

Versions: 12

Compression:

Stored size: 1.89 KB

Contents

module Hubstats
  class Comment < ActiveRecord::Base
    scope :pull_reviews_count, lambda {
      select("hubstats_comments.user_id")
      .select("COUNT(DISTINCT hubstats_pull_requests.id) as total")
      .joins("LEFT JOIN hubstats_pull_requests ON hubstats_pull_requests.id = hubstats_comments.pull_request_id")
      .where("hubstats_pull_requests.user_id != hubstats_comments.user_id")
      .group("hubstats_comments.user_id")
    }

    scope :created_since, lambda {|time| where("created_at > ?", time) }
    scope :belonging_to_pull_request, lambda {|pull_request_id| where(pull_request_id: pull_request_id)}
    scope :belonging_to_user, lambda {|user_id| where(user_id: user_id)}
    scope :belonging_to_repo, lambda {|repo_id| where(repo_id: repo_id)}

    attr_accessible :id, :html_url, :url, :pull_request_url, :diff_hunk, :path,
      :position, :original_position, :line, :commit_id, :original_commit_id,
      :body, :created_at, :updated_at, :user_id, :pull_request_id, :repo_id,
      :kind
  
    belongs_to :user
    belongs_to :pull_request
    belongs_to :repo
   
    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

  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
hubstats-0.3.13 app/models/hubstats/comment.rb
hubstats-0.3.12 app/models/hubstats/comment.rb
hubstats-0.3.11 app/models/hubstats/comment.rb
hubstats-0.3.10 app/models/hubstats/comment.rb
hubstats-0.3.9 app/models/hubstats/comment.rb
hubstats-0.3.8 app/models/hubstats/comment.rb
hubstats-0.3.7 app/models/hubstats/comment.rb
hubstats-0.3.6 app/models/hubstats/comment.rb
hubstats-0.3.5 app/models/hubstats/comment.rb
hubstats-0.3.4 app/models/hubstats/comment.rb
hubstats-0.3.3 app/models/hubstats/comment.rb
hubstats-0.3.2 app/models/hubstats/comment.rb