Class: Hubstats::PullRequest

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_or_update(github_pull) ⇒ Object



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

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

  user = Hubstats::User.create_or_update(github_pull[:user])
  github_pull[:user_id] = user.id

  github_pull[:repository][:updated_at] = github_pull[:updated_at]
  repo = Hubstats::Repo.create_or_update(github_pull[:repository])
  github_pull[:repo_id] = repo.id

  pull_data = github_pull.slice(*column_names.map(&:to_sym))

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

.group_by(group) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'app/models/hubstats/pull_request.rb', line 59

def self.group_by(group)
  if group == 'user'
    with_user_name.order("user_name ASC")
  elsif group == 'repo'
    with_repo_name.order("repo_name asc")
  else
    scoped
  end
end

.state_based_order(timespan, state, order) ⇒ Object



50
51
52
53
54
55
56
57
# File 'app/models/hubstats/pull_request.rb', line 50

def self.state_based_order(timespan,state,order)
  order = ["ASC","DESC"].detect{|order_type| order_type.to_s == order.to_s.upcase } || "DESC"
  if state == "closed"
    with_state(state).updated_since(timespan).order("hubstats_pull_requests.closed_at #{order}")
  else
    with_state(state).updated_since(timespan).order("hubstats_pull_requests.created_at #{order}")
  end
end

Instance Method Details

#add_labels(labels) ⇒ Object



45
46
47
48
# File 'app/models/hubstats/pull_request.rb', line 45

def add_labels(labels)
  labels.map!{ |label| Hubstats::Label.first_or_create(label) }
  self.labels = labels
end