Sunday, September 4, 2011

rails spork autotest failures repeating endlessly

Rails 3.1
Spork 0.9 rc9
ZenTest (installs autotest binary)
Fedora Linux

After launching spork, and then autotest, any failed tests will continuously get retested by autotest. This is because autotest is looking for changes in file dates and will rerun all tests if it finds a change.

Solution:

.autotest setup can be found in
http://ruby.railstutorial.org/chapters/static-pages
and
http://automate-everything.com/2009/08/gnome-and-autospec-notifications/

Edit ~/.autotest

  Autotest.add_hook :initialize do |autotest|
    autotest.add_exception %r{^\.git}  # ignore Version Control System
    autotest.add_exception %r{^./tmp}  # ignore temp files
    autotest.add_exception %r{^./log}  # ignore log

    # from rails tutorial 201109
    autotest.add_mapping(/^spec\/requests\/.*_spec\.rb$/) do
      autotest.files_matching(/^spec\/requests\/.*_spec\.rb$/)
    end 
  end


I had to add the log exception to stop the failure cycle.

rails, devise and how to edit user account profile without providing a password

There are several options on the wiki to update an account information without providing a password

https://github.com/plataformatec/devise/wiki/How-To%3a-Allow-users-to-edit-their-account-without-providing-a-password

This is due to the fact that devise is asking for the current password for all profile changes. The wiki makes changes to remove password fields or the current password from the form.

The workflow I was looking for was to allow the user to change details such as first and last name without a password. Only when the current password is change is a password required.

Solution:
rails 3.1
devise 1.4.4
From the Gem
/gems/devise-1.4.4/lib/devise/models/database_authenticatable.rb

copy the method update_with_password and add it to your user.rb model (or whatever model you're using to store account information)

      def update_with_password(params={})
        current_password = params.delete(:current_password)

        if params[:password].blank?
          params.delete(:password)
          params.delete(:password_confirmation) if params[:password_confirmation].blank?
        end

        result = if valid_password?(current_password)
          update_attributes(params)
        else
          self.attributes = params
          self.valid?
          self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
          false
        end

        clean_up_passwords
        result
      end

Next change the line
result = if valid_password?(current_password)
to
result = if params[:password].blank? && if params[:password].blank? || valid_password?(current_password) 

Don't check for current password if password is blank, go ahead and update