app/controllers/user_trainings_controller.rb
# AJAX-only action to retrieve a filtered row list or a single instance.
#
# TODO [Steve, 20140122] [Future DEV] Move this action to an API-dedicated controller.
#
# == Params:
# - <tt>:id</tt> => filter bypass, used to retrieve the specified single row instance identified by its id
# - <tt>:training_step_type_id</tt> => when present, it is used as a filtering parameter for the result data set.
# - <tt>:limit</tt> => to limit the array of results; the default top limit is set to 1000 for performance reasons (but it can be overridden by this parameter).
# - <tt>:query</tt> => a string match for the verbose description; when equal to '%', the parameter is ignored.
#
# == Returns:
# A JSON array of Hash instances having the structure returned by
# TrainingDecorator#.drop_down_attrs( current_user )
#
AJAX-only action to retrieve a filtered row list or a single instance.
TODO [Steve, 20140122] [Future DEV] Move this action to an API-dedicated controller.

:id => filter bypass, used to retrieve the specified single row instance identified by its id
:training_step_type_id => when present, it is used as a filtering parameter for the result data set.
:limit => to limit the array of results; the default top limit is set to 1000 for performance reasons (but it can be overridden by this parameter).
:query => a string match for the verbose description; when equal to '%', the parameter is ignored.

A JSON array of Hash instances having the structure returned by
TrainingDecorator#.drop_down_attrs( current_user )
AJAX - only action to retrieve a filtered row list or a single instance . TODO [ Steve 20140122 ] [ Future DEV ] Move this action to an API - dedicated controller . : id = > filter bypass used to retrieve the specified single row instance identified by its id : training_step_type_id = > when present it is used as a filtering parameter for the result data set . : limit = > to limit the array of results ; the default top limit is set to 1000 for performance reasons ( but it can be overridden by this parameter ) . : query = > a string match for the verbose description ; when equal to ' % ' the parameter is ignored . A JSON array of Hash instances having the structure returned by TrainingDecorator# . drop_down_attrs ( current_user )
def json_list
    if request.xhr?                                 
      if @user_training
        render( json: TrainingDecorator.decorate( @user_training ).drop_down_attrs() ) and return
      end
      limit = ( params[:limit].to_i > 0 ? params[:limit].to_i : 1000 )
      results = UserTraining.visible_to_user( current_user )
      if params[:query] && ( params[:query].to_s != QUERY_WILDCHAR )
        results = results.find_all { |row|
          row.get_full_name() =~ Regexp.new( params[:query], true )
        }
      end
      if results.respond_to?( :map ) && results.respond_to?( :sort_by )
        result_array = results.map{ |row|
          TrainingDecorator.decorate( row ).drop_down_attrs()
        }.sort_by{ |item| item[:label] }            
      else
        result_array = []
      end
      result_array = result_array[0 .. limit-1] if result_array.size > limit
      render( json: result_array )
    else
      flash[:info] = I18n.t(:invalid_action_request)
      redirect_to( exercises_path() ) and return
    end
  end
def json_list if request . xhr? if @user_training render ( json : TrainingDecorator . decorate ( @user_training ) . drop_down_attrs ( ) ) and return end limit = ( params [ :limit ] . to_i > 0 ? params [ :limit ] . to_i : 1000 ) results = UserTraining . visible_to_user ( current_user ) if params [ :query ] && ( params [ :query ] . to_s != QUERY_WILDCHAR ) results = results . find_all { | row | row . get_full_name ( ) =~ Regexp . new ( params [ :query ] , true ) } end if results . respond_to? ( :map ) && results . respond_to? ( :sort_by ) result_array = results . map { | row | TrainingDecorator . decorate ( row ) . drop_down_attrs ( ) } . sort_by { | item | item [ :label ] } else result_array = [ ] end result_array = result_array [ 0 .. limit - 1 ] if result_array . size > limit render ( json : result_array ) else flash [ :info ] = I18n . t ( :invalid_action_request ) redirect_to ( exercises_path ( ) ) and return end end
AJAX-only action to retrieve a filtered row list or a single instance.
AJAX - only action to retrieve a filtered row list or a single instance .
# Make sure the request is an AJAX one # DEBUG #      logger.debug "\r\n\r\n!! ------ #{self.class.name}.index() -----" #      logger.debug "PARAMS: #{params.inspect}" # -- Single id retrieval: # -- Result List retrieval: # Get the results and filter them even more using the query chars: # DEBUG #        logger.debug "results (before filtering): #{results.inspect}" # Map the actual results to an array of custom objects (label with values, for drop-down list combo setup): # Sort also the result array by the label itself # Limit the result array, if necessary: # Finally, render the result array as JSON: