Use a module for this?

Hi,

I have some code that has to be used by more than one method in the same class. Would I create a module for this or is there a better way?

eg. module QuestionSequencer    class Sequence

def sequence_questions       initial_now_playing_length = play_list[0].length       now_playing = play_list[0]       clone_number = 3       order = "random" # $$$Variable       if order == "random"         now_playing_temp = Array.new         for i in 1..clone_number           now_playing = now_playing.dup.shuffle           now_playing_temp = now_playing_temp + now_playing         end         now_playing = now_playing_temp         for d in 1..clone_number # Check for sequential duplicates:          if now_playing[(initial_now_playing_length * d) -1 ] == now_playing[initial_now_playing_length * d]             value = now_playing.delete_at(initial_now_playing_length * d)             now_playing.insert(now_playing.length, value) # Reinsert the item in a new position           end         end       else # Sequential         now_playing = now_playing.dup * clone_number       end       now_playing

    end   end end

in method:      now_playing = play_list.sequence_questions in model:     include QuestionSequencer

This is not working but would like to know if I'm at least going in the correct direction.

Thanks, Dave

Is play_list an instance of a model? If so then why is sequence_questions not just a method of the model?

Colin

Is play_list an instance of a model? If so then why is sequence_questions not just a method of the model?

Colin

Yes it is. I'll try that now.

Thank you.

Dave Castellano

Dave Castellano wrote in post #1140813:

Is play_list an instance of a model? If so then why is sequence_questions not just a method of the model?

Colin

Yes it is. I'll try that now.

Thank you.

Dave Castellano

Well, maybe...

Here is the code, te question is is it an instance if its not yet created?

# POST /drills   # POST /drills.json   def create     if params[:level] == "subject"       session[:scope_id] = session[:student_subject_id]       session[:scope] = "subject"     elsif params[:level] == "book"       session[:scope_id] = session[:student_book_id]       session[:scope] = "book"     elsif params[:level] == "chapter"       session[:scope_id] = session[:student_chapter_id]       session[:scope] = "chapter"     end

    if !Drill.exists? user_id: current_user.id, scope: params[:level], scope_id: session[:scope_id]       if params[:level] == "subject"         klass = ScopeSelector::Subject         scope_id = session[:student_subject_id]       elsif params[:level] == "book"         klass = ScopeSelector::Book         scope_id = session[:student_book_id]       elsif params[:level] == "chapter"         klass = ScopeSelector::Chapter         scope_id = session[:student_chapter_id]       end       scope_selector = klass.new(scope_id)       play_list = scope_selector.play_list       # play_list[0] contains question ids corresponding to minisection id in play_list[1]       # play_list[1] contains the minisection_id of the question ids in play_list[0]       # play_list[2..infinity] contain upcoming minisection ids waiting to be converted to their corresponding question ids

=begin       initial_now_playing_length = play_list[0].length       now_playing = play_list[0] # Create the now_playing from the long list by shifting starting_now_playing_length question id's fron question pool.       clone_number = 3 # $$$Variable # Number of repeats of now_playing used to create initial playlist       order = "random" # $$$Variable       if order == "random"         now_playing_temp = Array.new         for i in 1..clone_number           now_playing = now_playing.dup.shuffle           now_playing_temp = now_playing_temp + now_playing         end         now_playing = now_playing_temp         for d in 1..clone_number # Check for sequential duplicates:          if now_playing[(initial_now_playing_length * d) -1 ] == now_playing[initial_now_playing_length * d] # Finds a duplicate...             value = now_playing.delete_at(initial_now_playing_length * d) # Get the item and delete it from array             now_playing.insert(now_playing.length, value) # Reinsert the item in a new position           end         end       else # Sequential         now_playing = now_playing.dup * clone_number       end =end

      now_playing = play_list[0].sequence_questions       binding.pry

      play_list[0] = now_playing       @question = Question.find(play_list[0][0])       session[:play_list] = play_list       @drill = Drill.new(play_list: play_list, scope: params[:level], scope_id: scope_id, user_id: current_user.id)       if @drill.save         redirect_to(:controller => "questions", :action => "show", :id => @question)       else         redirect_to :back       end     else # Drill already exists       @existing_drill = Drill.where("user_id = ? AND scope = ? AND scope_id =?", current_user.id, params[:level], session[:scope_id]).first.play_list       session[:play_list] = @existing_drill       @question = @existing_drill[0][0]       redirect_to(:controller => "questions", :action => "show", :id => @question)     end   end

The code between begin and end is the code I need to use in more than one method...

Dave

Do not make us wade through lots of code trying to understand what is going on. Explain the problem and show us just a few lines of code if necessary. For instance of what type is play_list? You said it was an instance of a model, but it seems to be an array or collection of some sort.

Colin

Do not make us wade through lots of code trying to understand what is going on. Explain the problem and show us just a few lines of code if necessary. For instance of what type is play_list? You said it was an instance of a model, but it seems to be an array or collection of some sort.

Colin

Sorry about that :slight_smile:

Play_list is an array of arrays returned at the beginning of the create method. Each array contains a topic id. Array[0] needs to be converted to a list of the question id's contained in that topic. That is all taken care of.

The sequence of the question id's in array[0] needs to be manipulated, in more than one method.

So... I need to either duplicate the code in several methods (in the same controller) or create a method that is called by the controllers multiple methods. The model method seems right but play_list does not seem to be an instance as it is not yet created and is not yet an attribute of the model

Dave

Do not make us wade through lots of code trying to understand what is going on. Explain the problem and show us just a few lines of code if necessary. For instance of what type is play_list? You said it was an instance of a model, but it seems to be an array or collection of some sort.

Colin

Sorry about that :slight_smile:

Play_list is an array of arrays returned at the beginning of the create method. Each array contains a topic id. Array[0] needs to be converted to a list of the question id's contained in that topic. That is all taken care of.

That sounds to me like a horrible way to organize data, it is generally much better to deal in records and associations rather than extracting id values and manipulating them. However, if you feel you must do it that way...

The sequence of the question id's in array[0] needs to be manipulated, in more than one method.

So... I need to either duplicate the code in several methods (in the same controller) or create a method that is called by the controllers multiple methods. The model method seems right but play_list does not seem to be an instance as it is not yet created and is not yet an attribute of the model

You could either have a private method of the controller, or a class method of the model (rather than an instance method). Probably the latter.

Colin