From c42fcc824ab7a81ac76b208776d8dc3df97d7eb0 Mon Sep 17 00:00:00 2001 From: vladdruzh Date: Mon, 26 Nov 2012 15:32:23 +0400 Subject: [PATCH 1/5] add to api maintainers index method --- .../api/v1/maintainers_controller.rb | 18 ++++++++++++ .../v1/maintainers/_maintainer.json.jbuilder | 2 ++ .../api/v1/maintainers/_package.json.jbuilder | 3 ++ .../api/v1/maintainers/index.json.jbuilder | 15 ++++++++++ config/routes.rb | 1 + .../api/v1/maintainers_controller_spec.rb | 29 +++++++++++++++++++ 6 files changed, 68 insertions(+) create mode 100644 app/controllers/api/v1/maintainers_controller.rb create mode 100644 app/views/api/v1/maintainers/_maintainer.json.jbuilder create mode 100644 app/views/api/v1/maintainers/_package.json.jbuilder create mode 100644 app/views/api/v1/maintainers/index.json.jbuilder create mode 100644 spec/controllers/api/v1/maintainers_controller_spec.rb diff --git a/app/controllers/api/v1/maintainers_controller.rb b/app/controllers/api/v1/maintainers_controller.rb new file mode 100644 index 000000000..7aa77de28 --- /dev/null +++ b/app/controllers/api/v1/maintainers_controller.rb @@ -0,0 +1,18 @@ +class Api::V1::MaintainersController < Api::V1::BaseController + before_filter :authenticate_user! unless APP_CONFIG['anonymous_access'] + before_filter :find_platform + + def index + @maintainers = BuildList::Package.actual.by_platform(@platform) + .order('lower(name) ASC, length(name) ASC') + .includes(:project) + .paginate(paginate_params) + end + + private + + def find_platform + @platform = Platform.find(params[:platform_id]) + end + +end 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 b57307f1b..4916b02da 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..15493e1c2 --- /dev/null +++ b/spec/controllers/api/v1/maintainers_controller_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +describe Api::V1::MaintainersController do + 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 From 0545863642b4cc9dd15597f304bf3debb4378fee Mon Sep 17 00:00:00 2001 From: vladdruzh Date: Wed, 28 Nov 2012 10:04:40 +0400 Subject: [PATCH 2/5] refactor --- app/controllers/api/v1/maintainers_controller.rb | 10 +--------- app/controllers/platforms/maintainers_controller.rb | 1 - app/models/build_list/package.rb | 2 ++ 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/app/controllers/api/v1/maintainers_controller.rb b/app/controllers/api/v1/maintainers_controller.rb index 7aa77de28..625a63134 100644 --- a/app/controllers/api/v1/maintainers_controller.rb +++ b/app/controllers/api/v1/maintainers_controller.rb @@ -1,18 +1,10 @@ class Api::V1::MaintainersController < Api::V1::BaseController before_filter :authenticate_user! unless APP_CONFIG['anonymous_access'] - before_filter :find_platform + load_and_authorize_resource :platform def index @maintainers = BuildList::Package.actual.by_platform(@platform) - .order('lower(name) ASC, length(name) ASC') .includes(:project) .paginate(paginate_params) end - - private - - def find_platform - @platform = Platform.find(params[:platform_id]) - end - end diff --git a/app/controllers/platforms/maintainers_controller.rb b/app/controllers/platforms/maintainers_controller.rb index 386e48ae4..5c3e49a84 100644 --- a/app/controllers/platforms/maintainers_controller.rb +++ b/app/controllers/platforms/maintainers_controller.rb @@ -6,7 +6,6 @@ class Platforms::MaintainersController < ApplicationController 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]) diff --git a/app/models/build_list/package.rb b/app/models/build_list/package.rb index ce894412b..6a3dcb79c 100644 --- a/app/models/build_list/package.rb +++ b/app/models/build_list/package.rb @@ -12,6 +12,8 @@ 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) } From 5899d5f28f858327248570ac65d5cece74454093 Mon Sep 17 00:00:00 2001 From: vladdruzh Date: Thu, 29 Nov 2012 09:32:10 +0400 Subject: [PATCH 3/5] add filters to maintainers list api --- app/controllers/api/v1/maintainers_controller.rb | 5 ++++- app/controllers/platforms/maintainers_controller.rb | 2 +- app/models/build_list/package.rb | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/maintainers_controller.rb b/app/controllers/api/v1/maintainers_controller.rb index 625a63134..a85203e4b 100644 --- a/app/controllers/api/v1/maintainers_controller.rb +++ b/app/controllers/api/v1/maintainers_controller.rb @@ -5,6 +5,9 @@ class Api::V1::MaintainersController < Api::V1::BaseController def index @maintainers = BuildList::Package.actual.by_platform(@platform) .includes(:project) - .paginate(paginate_params) + if name = params[:filter].try(:[], :package_name) + @maintainers = @maintainers.like_name(name) + end + @maintainers = @maintainers.paginate(paginate_params) end end diff --git a/app/controllers/platforms/maintainers_controller.rb b/app/controllers/platforms/maintainers_controller.rb index 5c3e49a84..10e200aa3 100644 --- a/app/controllers/platforms/maintainers_controller.rb +++ b/app/controllers/platforms/maintainers_controller.rb @@ -7,7 +7,7 @@ class Platforms::MaintainersController < ApplicationController def index @maintainers = BuildList::Package.actual.by_platform(@platform) .includes(:project) - @maintainers = @maintainers.where('name ILIKE ?', "%#{params[:q]}%") if params[:q].present? + @maintainers = @maintainers.like_name(params[:q]) if params[:q].present? @maintainers = @maintainers.paginate(:page => params[:page]) end diff --git a/app/models/build_list/package.rb b/app/models/build_list/package.rb index 6a3dcb79c..108f9ea4e 100644 --- a/app/models/build_list/package.rb +++ b/app/models/build_list/package.rb @@ -19,6 +19,7 @@ class BuildList::Package < ActiveRecord::Base 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}%")} def assignee project.maintainer From bfa538e644405861cbec5558d3ee4a23d8ab06e3 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Fri, 30 Nov 2012 23:15:37 +0600 Subject: [PATCH 4/5] [refs #732] small refactoring --- app/controllers/api/v1/maintainers_controller.rb | 10 ++++------ app/controllers/platforms/maintainers_controller.rb | 9 ++++----- app/models/build_list/package.rb | 2 +- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/v1/maintainers_controller.rb b/app/controllers/api/v1/maintainers_controller.rb index a85203e4b..cda1761f1 100644 --- a/app/controllers/api/v1/maintainers_controller.rb +++ b/app/controllers/api/v1/maintainers_controller.rb @@ -3,11 +3,9 @@ class Api::V1::MaintainersController < Api::V1::BaseController load_and_authorize_resource :platform def index - @maintainers = BuildList::Package.actual.by_platform(@platform) - .includes(:project) - if name = params[:filter].try(:[], :package_name) - @maintainers = @maintainers.like_name(name) - end - @maintainers = @maintainers.paginate(paginate_params) + @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 10e200aa3..9abd417de 100644 --- a/app/controllers/platforms/maintainers_controller.rb +++ b/app/controllers/platforms/maintainers_controller.rb @@ -5,10 +5,9 @@ class Platforms::MaintainersController < ApplicationController load_and_authorize_resource :platform def index - @maintainers = BuildList::Package.actual.by_platform(@platform) - .includes(:project) - @maintainers = @maintainers.like_name(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 108f9ea4e..891fe239b 100644 --- a/app/models/build_list/package.rb +++ b/app/models/build_list/package.rb @@ -19,7 +19,7 @@ class BuildList::Package < ActiveRecord::Base 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}%")} + scope :like_name, lambda {|name| where('name ILIKE ?', "%#{name}%") if name.present?} def assignee project.maintainer From cb8b54c6df68f8366e5f487c5b3130d6b36de252 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Fri, 30 Nov 2012 23:16:05 +0600 Subject: [PATCH 5/5] [refs #732] fix index test --- spec/controllers/api/v1/maintainers_controller_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/controllers/api/v1/maintainers_controller_spec.rb b/spec/controllers/api/v1/maintainers_controller_spec.rb index 15493e1c2..e98420f20 100644 --- a/spec/controllers/api/v1/maintainers_controller_spec.rb +++ b/spec/controllers/api/v1/maintainers_controller_spec.rb @@ -1,6 +1,10 @@ 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