custom_fields_controller.rb
102 lines
| 3.0 KiB
| text/x-ruby
|
RubyLexer
|
r2272 | # Redmine - project management software | ||
|
r14856 | # Copyright (C) 2006-2016 Jean-Philippe Lang | ||
|
r330 | # | ||
# This program is free software; you can redistribute it and/or | ||||
# modify it under the terms of the GNU General Public License | ||||
# as published by the Free Software Foundation; either version 2 | ||||
# of the License, or (at your option) any later version. | ||||
|
r6781 | # | ||
|
r330 | # This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
|
r6781 | # | ||
|
r330 | # You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
class CustomFieldsController < ApplicationController | ||||
|
r3062 | layout 'admin' | ||
|
r15601 | self.main_menu = false | ||
|
r6781 | |||
|
r15273 | before_action :require_admin | ||
before_action :build_new_custom_field, :only => [:new, :create] | ||||
before_action :find_custom_field, :only => [:edit, :update, :destroy] | ||||
|
r11935 | accept_api_auth :index | ||
|
r10 | |||
|
r2 | def index | ||
|
r11935 | respond_to do |format| | ||
format.html { | ||||
@custom_fields_by_type = CustomField.all.group_by {|f| f.class.name } | ||||
|
r15687 | @custom_fields_projects_count = | ||
IssueCustomField.where(is_for_all: false).joins(:projects).group(:custom_field_id).count | ||||
|
r11935 | } | ||
format.api { | ||||
@custom_fields = CustomField.all | ||||
} | ||||
end | ||||
|
r2 | end | ||
|
r6781 | |||
|
r330 | def new | ||
|
r12125 | @custom_field.field_format = 'string' if @custom_field.field_format.blank? | ||
@custom_field.default_value = nil | ||||
|
r8024 | end | ||
|
r6781 | |||
|
r8024 | def create | ||
|
r10755 | if @custom_field.save | ||
|
r330 | flash[:notice] = l(:notice_successful_create) | ||
|
r2535 | call_hook(:controller_custom_fields_new_after_save, :params => params, :custom_field => @custom_field) | ||
|
r14364 | redirect_to edit_custom_field_path(@custom_field) | ||
|
r5491 | else | ||
|
r8024 | render :action => 'new' | ||
|
r330 | end | ||
end | ||||
def edit | ||||
|
r8024 | end | ||
def update | ||||
|
r15307 | @custom_field.safe_attributes = params[:custom_field] | ||
if @custom_field.save | ||||
|
r2535 | call_hook(:controller_custom_fields_edit_after_save, :params => params, :custom_field => @custom_field) | ||
|
r14954 | respond_to do |format| | ||
format.html { | ||||
flash[:notice] = l(:notice_successful_update) | ||||
redirect_back_or_default edit_custom_field_path(@custom_field) | ||||
} | ||||
|
r15305 | format.js { head 200 } | ||
|
r14954 | end | ||
|
r5491 | else | ||
|
r14954 | respond_to do |format| | ||
format.html { render :action => 'edit' } | ||||
|
r15305 | format.js { head 422 } | ||
|
r14954 | end | ||
|
r330 | end | ||
end | ||||
|
r6781 | |||
|
r2 | def destroy | ||
|
r10755 | begin | ||
@custom_field.destroy | ||||
rescue | ||||
flash[:error] = l(:error_can_not_delete_custom_field) | ||||
end | ||||
|
r10752 | redirect_to custom_fields_path(:tab => @custom_field.class.name) | ||
|
r330 | end | ||
|
r8024 | |||
private | ||||
def build_new_custom_field | ||||
|
r15307 | @custom_field = CustomField.new_subclass_instance(params[:type]) | ||
|
r8024 | if @custom_field.nil? | ||
|
r12574 | render :action => 'select_type' | ||
|
r15307 | else | ||
@custom_field.safe_attributes = params[:custom_field] | ||||
|
r8024 | end | ||
end | ||||
def find_custom_field | ||||
@custom_field = CustomField.find(params[:id]) | ||||
rescue ActiveRecord::RecordNotFound | ||||
render_404 | ||||
end | ||||
|
r2 | end | ||