"Fossies" - the Fresh Open Source Software Archive

Member "redmine-4.1.1/app/controllers/enumerations_controller.rb" (6 Apr 2020, 3318 Bytes) of package /linux/www/redmine-4.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "enumerations_controller.rb": 4.1.0_vs_4.1.1.

    1 # frozen_string_literal: true
    2 
    3 # Redmine - project management software
    4 # Copyright (C) 2006-2019  Jean-Philippe Lang
    5 #
    6 # This program is free software; you can redistribute it and/or
    7 # modify it under the terms of the GNU General Public License
    8 # as published by the Free Software Foundation; either version 2
    9 # of the License, or (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program; if not, write to the Free Software
   18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   19 
   20 class EnumerationsController < ApplicationController
   21   layout 'admin'
   22   self.main_menu = false
   23 
   24   before_action :require_admin, :except => :index
   25   before_action :require_admin_or_api_request, :only => :index
   26   before_action :build_new_enumeration, :only => [:new, :create]
   27   before_action :find_enumeration, :only => [:edit, :update, :destroy]
   28   accept_api_auth :index
   29 
   30   helper :custom_fields
   31 
   32   def index
   33     respond_to do |format|
   34       format.html
   35       format.api {
   36         @klass = Enumeration.get_subclass(params[:type])
   37         if @klass
   38           @enumerations = @klass.shared.sorted.to_a
   39         else
   40           render_404
   41         end
   42       }
   43     end
   44   end
   45 
   46   def new
   47   end
   48 
   49   def create
   50     if request.post? && @enumeration.save
   51       flash[:notice] = l(:notice_successful_create)
   52       redirect_to enumerations_path
   53     else
   54       render :action => 'new'
   55     end
   56   end
   57 
   58   def edit
   59   end
   60 
   61   def update
   62     if @enumeration.update(enumeration_params)
   63       respond_to do |format|
   64         format.html {
   65           flash[:notice] = l(:notice_successful_update)
   66           redirect_to enumerations_path
   67         }
   68         format.js { head 200 }
   69       end
   70     else
   71       respond_to do |format|
   72         format.html { render :action => 'edit' }
   73         format.js { head 422 }
   74       end
   75     end
   76   end
   77 
   78   def destroy
   79     if !@enumeration.in_use?
   80       # No associated objects
   81       @enumeration.destroy
   82       redirect_to enumerations_path
   83       return
   84     elsif params[:reassign_to_id].present? && (reassign_to = @enumeration.class.find_by_id(params[:reassign_to_id].to_i))
   85       @enumeration.destroy(reassign_to)
   86       redirect_to enumerations_path
   87       return
   88     end
   89     @enumerations = @enumeration.class.system.to_a - [@enumeration]
   90   end
   91 
   92   private
   93 
   94   def build_new_enumeration
   95     class_name = params[:enumeration] && params[:enumeration][:type] || params[:type]
   96     @enumeration = Enumeration.new_subclass_instance(class_name)
   97     if @enumeration
   98       @enumeration.attributes = enumeration_params || {}
   99     else
  100       render_404
  101     end
  102   end
  103 
  104   def find_enumeration
  105     @enumeration = Enumeration.find(params[:id])
  106   rescue ActiveRecord::RecordNotFound
  107     render_404
  108   end
  109 
  110   def enumeration_params
  111     # can't require enumeration on #new action
  112     cf_ids = @enumeration.available_custom_fields.map {|c| c.multiple? ? {c.id.to_s => []} : c.id.to_s}
  113     params.permit(:enumeration => [:name, :active, :is_default, :position, :custom_field_values => cf_ids])[:enumeration]
  114   end
  115 end