Commit a7aaad96 authored by Alessio Caiazza's avatar Alessio Caiazza Committed by Shinya Maeda

ReleasesFinder will always return a relation

parent b9aac409
...@@ -6,11 +6,9 @@ class ReleasesFinder ...@@ -6,11 +6,9 @@ class ReleasesFinder
@current_user = current_user @current_user = current_user
end end
# rubocop: disable CodeReuse/ActiveRecord
def execute def execute
return [] unless Ability.allowed?(@current_user, :read_release, @project) return Release.none unless Ability.allowed?(@current_user, :read_release, @project)
@project.releases.order('created_at DESC') @project.releases.sorted
end end
# rubocop: enable CodeReuse/ActiveRecord
end end
...@@ -11,6 +11,8 @@ class Release < ActiveRecord::Base ...@@ -11,6 +11,8 @@ class Release < ActiveRecord::Base
validates :description, :project, :tag, presence: true validates :description, :project, :tag, presence: true
scope :sorted, -> { order(created_at: :desc) }
delegate :repository, to: :project delegate :repository, to: :project
def commit def commit
......
...@@ -21,7 +21,7 @@ module API ...@@ -21,7 +21,7 @@ module API
use :pagination use :pagination
end end
get ':id/releases' do get ':id/releases' do
releases = ::Kaminari.paginate_array(::ReleasesFinder.new(user_project, current_user).execute) releases = ::ReleasesFinder.new(user_project, current_user).execute
present paginate(releases), with: Entities::Release present paginate(releases), with: Entities::Release
end end
......
...@@ -6,15 +6,14 @@ describe ReleasesFinder do ...@@ -6,15 +6,14 @@ describe ReleasesFinder do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:repository) { project.repository } let(:repository) { project.repository }
let(:v1_0_0) { create(:release, project: project, tag: 'v1.0.0')} let(:v1_0_0) { create(:release, project: project, tag: 'v1.0.0') }
let(:v1_1_0) { create(:release, project: project, tag: 'v1.1.0')} let(:v1_1_0) { create(:release, project: project, tag: 'v1.1.0') }
subject { described_class.new(project, user)} subject { described_class.new(project, user)}
before do before do
now = Time.now v1_0_0.update_attribute(:created_at, 2.days.ago)
v1_0_0.update_attribute(:created_at, now - 2.days) v1_1_0.update_attribute(:created_at, 1.day.ago)
v1_1_0.update_attribute(:created_at, now - 1.day)
end end
describe '#execute' do describe '#execute' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment