Bergrebell / CyberCoach

1 stars 1 forks source link

Save User profile without changing password #95

Closed wanze closed 9 years ago

wanze commented 9 years ago

When updating the user profile /users/xx/edit, it does not work if the password is left blank... I tried to solve it by modifying the validation rules like this:

  validates :password, presence: true, confirmation: true, length: {within: 4..10}, on: :create
  validates :password, presence: true, confirmation: true, length: {within: 4..10}, if: "not password.nil?", on: :update

However, it does not seem to work. Furthermore if there are errors, somehow the form is not displayed in the edit context anymore, but in the create context. I have no idea why, just open this for documentation :P

lexruee commented 9 years ago

@wanze There is a good reason for that.

If you want to update the password it is synchronized with the cybercoach server.

lexruee commented 9 years ago

@wanze This is done! A user can edit his account / profile data BUT cannot change his password.

wanze commented 9 years ago

@lexruee Can't change it, why? :)

lexruee commented 9 years ago

@wanze Technically it's possible. But I would separate editing the user profile data and editing the password.

Edit: I'm not sure if the cyber coach supports this. And if so then it's not really working :-).

wanze commented 9 years ago

Sorry for my comment, we better leave it like that then :)

lexruee commented 9 years ago

Nope, will be done in 10 minutes!

lexruee commented 9 years ago

@wanze Okay, it took me an hour. But it is beautiful and clean integrated.

Here is an example why I really wanted to replace the backend. Such a clean integration and we get the rails model validation for free :-)

# password rules for updating a user
  validates :new_password, length: {within: 4..10}, :if => :new_password_present?, on: :update
  validates_presence_of :password_confirmation, :if => :new_password_present?, on: :update
  validates_confirmation_of :new_password, :if => :new_password_present?, on: :update
...

  def update_coach_user
    proxy = Coach4rb::Proxy::Access.new self.username, self.password, Coach
    raise 'Update Error' unless proxy.valid?

    coach_user = proxy.update_user(get_coach_user) do |user|
      user.email = self.email if self.email.present?
      user.real_name = self.real_name if self.real_name.present?

      if self.new_password.present?
        user.password = self.new_password
        self.update_column(:password, self.new_password)
      end

    end
    ObjectStore::Store.set([:coach_user,self.id],coach_user)
  end