Commit 14e80d14 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Move npm packages lookup into own finder

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 0425cbf5
# frozen_string_literal: true
class Packages::NpmPackagesFinder
attr_reader :project, :package_name
def initialize(project, package_name)
@project = project
@package_name = package_name
end
def execute
packages
end
private
def packages
project.packages
.only_npm
.with_name(package_name)
.last_of_each_version
.preload_files
end
end
...@@ -15,6 +15,7 @@ class Packages::Package < ActiveRecord::Base ...@@ -15,6 +15,7 @@ class Packages::Package < ActiveRecord::Base
enum package_type: { maven: 1, npm: 2 } enum package_type: { maven: 1, npm: 2 }
scope :with_name, ->(name) { where(name: name) } scope :with_name, ->(name) { where(name: name) }
scope :only_npm, -> { where(package_type: 'npm') }
scope :preload_files, -> { preload(:package_files) } scope :preload_files, -> { preload(:package_files) }
def self.for_projects(projects) def self.for_projects(projects)
......
...@@ -41,10 +41,8 @@ module API ...@@ -41,10 +41,8 @@ module API
authorize!(:read_package, project) authorize!(:read_package, project)
forbidden! unless project.feature_available?(:packages) forbidden! unless project.feature_available?(:packages)
packages = project.packages packages = ::Packages::NpmPackagesFinder
.with_name(package_name) .new(project, package_name).execute
.last_of_each_version
.preload_files
present NpmPackagePresenter.new(project, package_name, packages), present NpmPackagePresenter.new(project, package_name, packages),
with: EE::API::Entities::NpmPackage with: EE::API::Entities::NpmPackage
...@@ -69,7 +67,7 @@ module API ...@@ -69,7 +67,7 @@ module API
get ':id/packages/npm/*package_name/-/*file_name', format: false do get ':id/packages/npm/*package_name/-/*file_name', format: false do
authorize_download_package! authorize_download_package!
package = user_project.packages package = user_project.packages.only_npm
.by_name_and_file_name(params[:package_name], params[:file_name]) .by_name_and_file_name(params[:package_name], params[:file_name])
package_file = ::Packages::PackageFileFinder package_file = ::Packages::PackageFileFinder
......
# frozen_string_literal: true
require 'spec_helper'
describe Packages::NpmPackagesFinder do
let(:package) { create(:npm_package) }
let(:project) { package.project }
describe '#execute!' do
it 'returns project packages' do
finder = described_class.new(project, package.name)
expect(finder.execute).to eq([package])
end
it 'returns an empty collection' do
finder = described_class.new(project, 'baz')
expect(finder.execute).to be_empty
end
end
end
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