rosa-build/app/controllers/api/v1/issues_controller.rb

109 lines
3.1 KiB
Ruby
Raw Normal View History

2013-04-12 17:52:56 +01:00
# -*- encoding : utf-8 -*-
class Api::V1::IssuesController < Api::V1::BaseController
2013-04-15 13:32:11 +01:00
respond_to :json
2013-04-12 17:52:56 +01:00
before_filter :authenticate_user!
skip_before_filter :authenticate_user!, :only => [:show] if APP_CONFIG['anonymous_access']
load_and_authorize_resource :group, :only => :group_index
load_resource :project
load_and_authorize_resource :issue, :through => :project, :find_by => :serial_id, :only => [:show, :update, :destroy, :create, :index]
def index
@issues = @project.issues
render_issues_list
end
def all_index
2013-04-17 18:32:04 +01:00
project_ids = get_all_project_ids Project.accessible_by(current_ability, :membered)
@issues = Issue.where('issues.project_id IN (?)', project_ids)
2013-04-12 17:52:56 +01:00
render_issues_list
end
def user_index
2013-04-17 18:32:04 +01:00
project_ids = get_all_project_ids current_user.projects
@issues = Issue.where('issues.project_id IN (?)', project_ids)
2013-04-12 17:52:56 +01:00
render_issues_list
end
def group_index
2013-04-17 19:28:50 +01:00
project_ids = @group.projects.uniq.pluck(:id)
2013-04-12 17:52:56 +01:00
@issues = Issue.where(:project_id => project_ids)
render_issues_list
end
def show
2013-04-15 13:32:11 +01:00
respond_with @issue
2013-04-12 17:52:56 +01:00
end
def create
2013-04-15 20:21:11 +01:00
@issue.user = current_user
2013-04-12 17:52:56 +01:00
create_subject @issue
end
def update
update_subject @issue
end
def destroy
destroy_subject @issue
end
private
def render_issues_list
@issues = @issues.includes(:user, :assignee, :labels).without_pull_requests
if params[:status] == 'closed'
@issues = @issues.closed
else
@issues = @issues.opened
end
2013-04-12 17:52:56 +01:00
if action_name == 'index' && params[:assignee].present?
case params[:assignee]
when 'none'
@issues = @issues.where(:assigned_id => nil)
when '*'
@issues = @issues.where('assigned_id IS NOT NULL')
else
@issues = @issues.where('assignees_issues.uname = ?', params[:assignee])
end
end
if %w[all_index user_index group_index].include?(action_name)
case params[:filter]
when 'created'
@issues = @issues.where(:user_id => current_user)
when 'all'
else
@issues = @issues.where(:assignee_id => current_user)
end
else
@issues.where('users.uname = ?', params[:creator]) if params[:creator].present?
end
if params[:labels].present?
labels = params[:labels].split(',').map {|e| e.strip}.select {|e| e.present?}
@issues = @issues.where('labels.name IN (?)', labels)
end
sort = params[:sort] == 'updated' ? 'issues.updated_at' : 'issues.created_at'
direction = params[:direction] == 'asc' ? 'ASC' : 'DESC'
@issues = @issues.order("#{sort} #{direction}")
@issues = @issues.where('created_at >= to_timestamp(?)', params[:since]) if params[:since] =~ /\A\d+\z/
@issues.paginate(paginate_params)
2013-04-15 20:22:39 +01:00
respond_with @issues
2013-04-12 17:52:56 +01:00
end
2013-04-17 18:32:04 +01:00
def get_all_project_ids default_projects
project_ids = []
if ['created', 'all'].include? params[:filter]
# add own issues
project_ids = Project.accessible_by(current_ability, :show).joins(:issues).
2013-04-17 19:28:50 +01:00
where(:issues => {:user_id => current_user.id}).uniq.pluck('projects.id')
2013-04-12 17:52:56 +01:00
end
2013-04-17 19:28:50 +01:00
project_ids |= default_projects.uniq.pluck(:id)
2013-04-12 17:52:56 +01:00
end
end