Skip to content

Instantly share code, notes, and snippets.

@elle
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elle/c5b85803fc6823a7dfa1 to your computer and use it in GitHub Desktop.
Save elle/c5b85803fc6823a7dfa1 to your computer and use it in GitHub Desktop.
Sequencer
# migration
t.integer :sequence
# controller action
# and the way we've done the method, it's probably logical to put the action in test_scenarios_controller
def sequence
TestScenario.sequence=(params[:sequence])
render :json => { :status => :ok }
end
# routes.rb
put 'sequence', :on => :collection
# sequencer module
module Concerns::Sequence
extend ActiveSupport::Concern
included do
default_scope :order => :sequence
before_create :set_sequence
end
def set_sequence
self.sequence = (self.class.maximum('sequence') || 0) + 1 unless self.sequence
end
module ClassMethods
def sequence=(params)
transaction do
params.each_pair do |key, value|
if record = find(value["id"])
record.update_attributes!(:sequence => key)
end
end
end
end
end
end
# test scenario
include Concerns::Sequence
# in the view
%table.sortable(data-url='#{path_to_the_sequence_action}')
#...
# this will add the id for each table row
%tr[test_scenario]
# And then you can use jquery ui sortable to move the rows around
# and have a event callback to send the order of rows and ids back to the path you specified in the data-url
# Let me know if you need help with that part
# a test from a previous project
it 'should order questions according to sequence' do
@module = ...
[4, 2, 1, 5, 3].each do |sequence|
q = create(:training_question, :training_module_id => @module.id, :text => sequence.to_s, :sequence => sequence)
end
# and then check that the sequence is kept
@module.reload
assert_equal '1', @module.questions.first.text
end
# this is just an example for one of the test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment