Class: Version

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

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Object



61
62
63
64
65
66
67
# File 'app/models/version.rb', line 61

def <=>(other)
  if parsed_number.is_a?(String) || other.parsed_number.is_a?(String)
    other.published_at <=> published_at
  else
    other.parsed_number <=> parsed_number
  end
end

#any_outdated_dependencies?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'app/models/version.rb', line 107

def any_outdated_dependencies?
  dependencies.any?(&:outdated?)
end

#as_json(options = nil) ⇒ Object



13
14
15
# File 'app/models/version.rb', line 13

def as_json(options = nil)
  super({ only: [:number, :published_at] }.merge(options || {}))
end

#follows_semver?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'app/models/version.rb', line 103

def follows_semver?
  valid_number? && follows_semver_for_dependency_requirements?
end

#follows_semver_for_dependency_requirements?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'app/models/version.rb', line 99

def follows_semver_for_dependency_requirements?
  dependencies.all?(&:valid_requirements?)
end

#greater_than_1?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
# File 'app/models/version.rb', line 111

def greater_than_1?
  return nil unless follows_semver?
  begin
    SemanticRange.gte(number, '1.0.0')
  rescue
    false
  end
end

#notify_firehoseObject



32
33
34
# File 'app/models/version.rb', line 32

def notify_firehose
  Firehose.new_version(project, project.platform, self)
end

#notify_subscribersObject



21
22
23
24
25
26
27
28
29
30
# File 'app/models/version.rb', line 21

def notify_subscribers
  subscriptions = project.subscriptions
  subscriptions = subscriptions.include_prereleases if prerelease?

  subscriptions.group_by(&:notification_user).each do |user, subscriptions|
    next if user.nil?
    next if user.muted?(project)
    VersionsMailer.new_version(user, project, self).deliver_later
  end
end

#notify_web_hooksObject



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

def notify_web_hooks
  repos = project.subscriptions.map(&:github_repository).compact.uniq
  repos.each do |repo|
    requirements = repo.repository_dependencies.select{|rd| rd.project == project }.map(&:requirements)
    repo.web_hooks.each do |web_hook|
      web_hook.send_new_version(project, project.platform, self, requirements)
    end
  end
end

#parsed_numberObject



69
70
71
# File 'app/models/version.rb', line 69

def parsed_number
  semantic_version || number
end

#platformObject



17
18
19
# File 'app/models/version.rb', line 17

def platform
  project.try(:platform)
end

#prerelease?Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
# File 'app/models/version.rb', line 85

def prerelease?
  if semantic_version
    !!semantic_version.pre
  elsif platform.try(:downcase) == 'rubygems'
    !!(number =~ /[a-zA-Z]/)
  else
    false
  end
end

#published_atObject



57
58
59
# File 'app/models/version.rb', line 57

def published_at
  read_attribute(:published_at).presence || created_at
end

#semantic_versionObject



73
74
75
76
77
78
79
# File 'app/models/version.rb', line 73

def semantic_version
  @semantic_version ||= begin
    Semantic::Version.new(number)
  rescue ArgumentError
    nil
  end
end

#send_notificationsObject



51
52
53
54
55
# File 'app/models/version.rb', line 51

def send_notifications
  notify_subscribers
  notify_firehose
  notify_web_hooks
end

#send_notifications_asyncObject



46
47
48
49
# File 'app/models/version.rb', line 46

def send_notifications_async
  return if published_at && published_at < 1.week.ago
  VersionNotificationsWorker.perform_async(self.id)
end

#stable?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'app/models/version.rb', line 81

def stable?
  !prerelease?
end

#to_paramObject



120
121
122
# File 'app/models/version.rb', line 120

def to_param
  project.to_param.merge(number: number)
end

#to_sObject



124
125
126
# File 'app/models/version.rb', line 124

def to_s
  number
end

#valid_number?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'app/models/version.rb', line 95

def valid_number?
  !!semantic_version
end