diff --git a/app/controllers/api/v1/maintainers_controller.rb b/app/controllers/api/v1/maintainers_controller.rb new file mode 100644 index 000000000..cda1761f1 --- /dev/null +++ b/app/controllers/api/v1/maintainers_controller.rb @@ -0,0 +1,11 @@ +class Api::V1::MaintainersController < Api::V1::BaseController + before_filter :authenticate_user! unless APP_CONFIG['anonymous_access'] + load_and_authorize_resource :platform + + def index + @maintainers = BuildList::Package.includes(:project) + .actual.by_platform(@platform) + .like_name(params[:filter].try(:[], :package_name)) + .paginate(paginate_params) + end +end diff --git a/app/controllers/platforms/maintainers_controller.rb b/app/controllers/platforms/maintainers_controller.rb index 386e48ae4..9abd417de 100644 --- a/app/controllers/platforms/maintainers_controller.rb +++ b/app/controllers/platforms/maintainers_controller.rb @@ -5,11 +5,9 @@ class Platforms::MaintainersController < ApplicationController load_and_authorize_resource :platform def index - @maintainers = BuildList::Package.actual.by_platform(@platform) - .order('lower(name) ASC, length(name) ASC') - .includes(:project) - @maintainers = @maintainers.where('name ILIKE ?', "%#{params[:q]}%") if params[:q].present? - @maintainers = @maintainers.paginate(:page => params[:page]) + @maintainers = BuildList::Package.includes(:project) + .actual.by_platform(@platform) + .like_name(params[:q]) + .paginate(:page => params[:page]) end - end diff --git a/app/models/build_list/package.rb b/app/models/build_list/package.rb index ce894412b..891fe239b 100644 --- a/app/models/build_list/package.rb +++ b/app/models/build_list/package.rb @@ -12,11 +12,14 @@ class BuildList::Package < ActiveRecord::Base :presence => true validates :package_type, :inclusion => PACKAGE_TYPES + default_scope order('lower(name) ASC, length(name) ASC') + # Fetches only actual (last publised) packages. scope :actual, where(:actual => true) scope :by_platform, lambda {|platform| where(:platform_id => platform) } scope :by_name, lambda {|name| where(:name => name) } scope :by_package_type, lambda {|type| where(:package_type => type) } + scope :like_name, lambda {|name| where('name ILIKE ?', "%#{name}%") if name.present?} def assignee project.maintainer diff --git a/app/views/api/v1/maintainers/_maintainer.json.jbuilder b/app/views/api/v1/maintainers/_maintainer.json.jbuilder new file mode 100644 index 000000000..ee03a5c24 --- /dev/null +++ b/app/views/api/v1/maintainers/_maintainer.json.jbuilder @@ -0,0 +1,2 @@ +json.(maintainer, :id, :uname, :name, :email) +json.url api_v1_user_path(maintainer.id) diff --git a/app/views/api/v1/maintainers/_package.json.jbuilder b/app/views/api/v1/maintainers/_package.json.jbuilder new file mode 100644 index 000000000..a6d452013 --- /dev/null +++ b/app/views/api/v1/maintainers/_package.json.jbuilder @@ -0,0 +1,3 @@ +json.(package, :id, :name, :version, :release) +json.type package.package_type +json.updated_at package.updated_at.to_i diff --git a/app/views/api/v1/maintainers/index.json.jbuilder b/app/views/api/v1/maintainers/index.json.jbuilder new file mode 100644 index 000000000..3113b84af --- /dev/null +++ b/app/views/api/v1/maintainers/index.json.jbuilder @@ -0,0 +1,15 @@ +json.maintainers @maintainers do |json, maintainer| + json.project do |json_project| + json_project.partial! 'api/v1/projects/project', :project => maintainer.project, :json => json + end + + json.package do |json_package| + json_package.partial! 'package', :package => maintainer, :json => json + end + + json.maintainer do |json_maintainer| + if user = maintainer.try(:assignee) + json_maintainer.partial! 'maintainer', :maintainer => user, :json => json + end + end +end diff --git a/config/routes.rb b/config/routes.rb index a3a65c2a2..1e6c9a32f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -33,6 +33,7 @@ Rosa::Application.routes.draw do post :clone put :clear } + resources :maintainers, :only => [ :index ] end resources :repositories, :only => [:show, :update, :destroy] do member { diff --git a/spec/controllers/api/v1/maintainers_controller_spec.rb b/spec/controllers/api/v1/maintainers_controller_spec.rb new file mode 100644 index 000000000..e98420f20 --- /dev/null +++ b/spec/controllers/api/v1/maintainers_controller_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe Api::V1::MaintainersController do + before do + stub_symlink_methods + end + + let(:package) { FactoryGirl.create(:build_list_package) } + + context 'for guest' do + it "should be able to perform index action", :anonymous_access => true do + get :index, :platform_id => package.platform_id, :format => :json + should render_template(:index) + end + + it 'should be able to perform get_id action', :anonymous_access => false do + get :index, :platform_id => package.platform_id, :format => :json + response.status.should == 401 + end + end + + context 'for simple user' do + before do + stub_symlink_methods + http_login(FactoryGirl.create(:user)) + end + + it "should be able to perform index action" do + get :index, :platform_id => package.platform_id, :format => :json + should render_template(:index) + end + end +end