You are currently browsing the tag archive for the ‘Ruby’ tag.

Deletes taking to long, just take a break :)
(so replication can catch up/things get unblocked)

Only use save sql, there is no escapeing.

  class ActiveRecord::Base
    def self.slow_delete_all(condition)
      count = 0
      loop do
        result = ActiveRecord::Base.connection.send(:delete_sql, "delete from #{table_name} where #{condition} limit 10000")
        count += result
        break if result == 0
        sleep 1
      end
      count
    end
  end

In case you spam a lot of threads and dont know how to get rid of them…

Thread.list.each do |thread|
  thread.exit unless thread == Thread.current
end

Usage

@updated_at = lambda{ @model.updated_at.to_i }
assert_change(@updated_at) { @model.touch }
assert_no_change(@updated_at) { @model.reload }

Code

  def assert_change(what)
    old = what.call
    yield
    assert_not_equal old, what.call
  end

  def assert_no_change(what)
    old = what.call
    yield
    assert_equal old, what.call
  end

Just built this little snippet in a hacknight, it simulates a geolcation via capybara, so you can test if you geo-magic actually works :)

View

<p>Finding your location... <span id="status">checking...</span></p>
<script>
  jQuery(function () {
    var timeout = (document.location.href.indexOf('test_location') >= 0 ? 100 : 0);

    setTimeout(function(){
      navigator.geolocation.getCurrentPosition(function(){
        jQuery('#status').html("found you!");
      });
    }, timeout)
  });
</script>

Capybara test via selenium in rspec

  def simulate_location(lat, lng)
    page.driver.browser.execute_script <<-JS
      window.navigator.geolocation.getCurrentPosition = function(success){
        var position = {"coords" : { "latitude": "#{lat}", "longitude": "#{lng}" }};
        success(position);
      }
    JS
  end

  it "can use location", :js => true do
    visit '/?test_location=true'
    simulate_location 20, 20
    sleep 0.2
    page.should have_content "found you!"
  end

require ‘resque/tasks’
require ‘resque_scheduler/tasks’
Scheduler needs very little cpu, just start it with a worker.

desc "schedule and work, so we only need 1 dyno"
task :schedule_and_work do
  if Process.fork
    sh "rake environment resque:work"
  else
    sh "rake resque:scheduler"
    Process.wait
  end
end
Follow

Get every new post delivered to your Inbox.

Join 76 other followers