module KonoUtils::Concerns::BaseEditing

Public Instance Methods

_failed_create(format) click to toggle source
# File lib/kono_utils/concerns/base_editing.rb, line 267
def _failed_create(format)
  format.html do
    flash.now[:error] = @object.errors.full_messages.join(',')
    render :action => :new
  end
  format.xml { render :xml => @object.errors, :status => :unprocessable_entity }
  format.inject { render :action => :edit, :layout => false }
end
_failed_destroy(format) click to toggle source
# File lib/kono_utils/concerns/base_editing.rb, line 253
def _failed_destroy(format)
  format.html { redirect_to index_custom_polymorphic_path(base_class),
                            :flash => {:error => @object.errors.full_messages.join(',')} }
  format.xml { head :ko }
  format.json { render json: {success: false, errors: @object.errors.to_json}, status: 422 }
end
_failed_update(format) click to toggle source
# File lib/kono_utils/concerns/base_editing.rb, line 282
def _failed_update(format)
  format.html do
    flash.now[:error] = @object.errors.full_messages.join(',')
    render :action => :edit
  end
  format.xml { render :xml => @object.errors, :status => :unprocessable_entity }
  format.inject { render :action => :edit, :layout => false }
end
_successful_create(format) click to toggle source
# File lib/kono_utils/concerns/base_editing.rb, line 276
def _successful_create(format)
  format.html { redirect_to edit_custom_polymorphic_path(@object), :notice => success_create_message(@object) }
  format.xml { render :xml => @object, :status => :created, :location => @object }
  format.inject { render :action => :success_create_show, :layout => false }
end
_successful_destroy(format) click to toggle source
# File lib/kono_utils/concerns/base_editing.rb, line 260
def _successful_destroy(format)
  format.html { redirect_to index_custom_polymorphic_path(base_class),
                            :notice => success_destroy_message(@object) }
  format.xml { head :ok }
  format.json { render json: {success: true} }
end
_successful_update(format) click to toggle source
# File lib/kono_utils/concerns/base_editing.rb, line 291
def _successful_update(format)
  format.html { redirect_to edit_custom_polymorphic_path(@object), :notice => success_update_message(@object) }
  format.xml { head :ok }
  format.inject { render :action => :success_update_show, :layout => false }
end
base_class() click to toggle source
# File lib/kono_utils/concerns/base_editing.rb, line 133
def base_class
  return @_base_class if @_base_class
  controller = controller_name
  modello = controller.singularize.camelize.safe_constantize
  logger.debug { "Editazione del controller:#{controller} per modello: #{modello.to_s}" }

  raise "Non riesco a restituire la classe base per il controller #{controller}" if modello.nil?

  @_base_class = modello
end
base_scope() click to toggle source

Scope iniziale per index, viene passato al policy_scope in index. nel caso sia stata attivata la ricerca, lo scope viene filtrato

# File lib/kono_utils/concerns/base_editing.rb, line 175
def base_scope
  if @search
    @search.make_query
  else
    base_class
  end
end
check_errors() click to toggle source
# File lib/kono_utils/concerns/base_editing.rb, line 225
def check_errors
  unless @object.valid?
    logger.debug { "Invalid Obj:" }
    logger.debug { @object.errors.inspect }
  end
end
clean_params(object = base_class.new) click to toggle source

@param object [ActiveRecord::Base] oggetto per cui estrapolare gli attributi ripuliti, di default utilizza

la classe base

@return [ActionController::Parameters]

# File lib/kono_utils/concerns/base_editing.rb, line 203
def clean_params(object = base_class.new)
  permitted = policy(object).permitted_attributes
  dati = require_params_for!(object.class).permit(permitted)
  ::Rails.logger.info { "Permitted Attributes: #{permitted.inspect}" }
  ::Rails.logger.info { "Parametri puliti: #{dati.inspect}" }
  dati
end
create() { |object| ... } click to toggle source

POST /utenti POST /utenti.xml

# File lib/kono_utils/concerns/base_editing.rb, line 87
def create
  @object = base_class.new(clean_params)
  authorize @object
  @object = yield(@object) if block_given?
  logger.debug { "Nuovo oggetto #{@object.inspect}" }

  respond_to do |format|
    if @object.save
      _successful_create(format)
    else
      _failed_create(format)
    end
  end
end
destroy() { |object| ... } click to toggle source

DELETE /utenti/1 DELETE /utenti/1.xml

# File lib/kono_utils/concerns/base_editing.rb, line 104
def destroy
  @object = yield(@object) if block_given?


  respond_to do |format|
    if @object.destroy
      _successful_destroy(format)
    else
      _failed_destroy(format)
    end
  end
end
destroy_custom_polymorphic_path(*rec) click to toggle source
# File lib/kono_utils/concerns/base_editing.rb, line 245
def destroy_custom_polymorphic_path(*rec)
  polymorphic_path(*rec)
end
edit() { |object| ... } click to toggle source

GET /utenti/1/edit

# File lib/kono_utils/concerns/base_editing.rb, line 68
def edit
  @object = yield(@object) if block_given?
end
edit_custom_polymorphic_path(*rec) click to toggle source
# File lib/kono_utils/concerns/base_editing.rb, line 237
def edit_custom_polymorphic_path(*rec)
  edit_polymorphic_path(*rec)
end
form_attributes(model = base_class.new) click to toggle source

Elenco degli attributi da visualizzare nella form

# File lib/kono_utils/concerns/base_editing.rb, line 119
def form_attributes(model = base_class.new)
  ActiveSupport::Deprecation.warn('Utilizzato solo nel vecchio sistema')
  policy(model).permitted_attributes
end
index(respond_to_call: nil) { |objects| ... } click to toggle source

E' possibile passare una callback per poter renderizzare ulteriori formati oppure cambiare la normale renderizzazione. La callback riceve il format della respond_to GET /utenti GET /utenti.xml

# File lib/kono_utils/concerns/base_editing.rb, line 35
def index(respond_to_call: nil)
  @objects = policy_scope(base_scope).all
  @objects = yield(@objects) if block_given?
  @objects = KonoUtils.configuration.pagination_proxer.new(@objects).paginate(params)

  respond_to do |format|
    format.html # index.html.erb
    format.xml { render :xml => @objects }
    unless respond_to_call.nil?
      respond_to_call.call(format)
    end
  end
end
index_custom_polymorphic_path(*rec) click to toggle source
# File lib/kono_utils/concerns/base_editing.rb, line 241
def index_custom_polymorphic_path(*rec)
  polymorphic_path(*rec)
end
kono_user() click to toggle source

Come per pundit, potrebbe essere il caso in cui l'utente autenticato non è quello standard di device ma uno con modello differente, quindi utilizziamo la stessa configurazione di pundit per avere l'utente da utilizzare nelle authorizzazioni. Centralizziamo questa cosa in questa funzione di modo che possiamo cambiarla in un futuro staccandoci dalle logiche di pundit. @example

Sovrascrivere questa funzione restituendo l'utente corretto che viene sottoposto a pundit

@return [Object] probabilmente un active-record di tipo User

# File lib/kono_utils/concerns/base_editing.rb, line 160
def kono_user
  pundit_user
end
load_object() click to toggle source
# File lib/kono_utils/concerns/base_editing.rb, line 164
def load_object
  @object = base_class.find(params[:id])
  authorize @object
  logger.debug { "Oggetto #{@object.inspect}" }

end
new() { |object| ... } click to toggle source

GET /utenti/new GET /utenti/new.xml GET /utenti/new.inject -> javascript che si occupa di avere un js che injetta il risultato in un

determinato target che deve essere passato tramite params,
se non presente il target viene scritto un warning in console
# File lib/kono_utils/concerns/base_editing.rb, line 54
def new
  @object = base_class.new
  authorize @object
  @object = yield(@object) if block_given?
  logger.debug { "Nuovo oggetto #{@object.inspect}" }

  respond_to do |format|
    format.html
    format.xml { render :xml => @object }
    format.inject { render :layout => false }
  end
end
new_custom_polymorphic_path(*base_class) click to toggle source
# File lib/kono_utils/concerns/base_editing.rb, line 233
def new_custom_polymorphic_path(*base_class)
  new_polymorphic_path(*base_class)
end
require_params_for(klass) click to toggle source

Come sopra. ma fallendo su un ActionController::Parameters vuoto

# File lib/kono_utils/concerns/base_editing.rb, line 221
def require_params_for(klass)
  require_params_for!(klass) rescue ActionController::Parameters.new({})
end
require_params_for!(klass) click to toggle source

Estrapola i parametri dalla classe in questione, partendo da params, fancendo un require

# File lib/kono_utils/concerns/base_editing.rb, line 213
def require_params_for!(klass)
  required_params_name = klass.name.underscore.gsub('/', '_').to_sym
  Rails.logger.info { "Required attibute: #{required_params_name}" }
  params.required(required_params_name)
end
show_custom_polymorphic_path(*rec) click to toggle source
# File lib/kono_utils/concerns/base_editing.rb, line 249
def show_custom_polymorphic_path(*rec)
  polymorphic_path(*rec)
end
table_columns() click to toggle source

Elenco ordinato dei campi da utilizzare nella visualizzazione della tabella index

# File lib/kono_utils/concerns/base_editing.rb, line 126
def table_columns
  ActiveSupport::Deprecation.warn('Utilizzato solo nel vecchio sistema')
  policy(base_class.new).permitted_attributes
end
update() { |object| ... } click to toggle source

PUT /utenti/1 PUT /utenti/1.xml

# File lib/kono_utils/concerns/base_editing.rb, line 74
def update
  @object = yield(@object) if block_given?
  respond_to do |format|
    if @object.update(clean_params(@object))
      _successful_update(format)
    else
      _failed_update(format)
    end
  end
end
user_not_authorized() click to toggle source
# File lib/kono_utils/concerns/base_editing.rb, line 146
def user_not_authorized
  flash[:alert] = t('.user_not_authorized', :model => @object.mn,
                    default: t('kono_utils.user_not_authorized', :model => @object.mn, default: "You are not authorized to perform this action."))
  redirect_to(request.referrer || root_path)
end