From 2838f81327e95b3f8d6f832f0011b709037fa37e Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Sat, 15 Aug 2015 08:01:48 +0300 Subject: [PATCH] Streamline profile/registration editing --- app/controllers/public/events_controller.rb | 2 +- .../public/personal_profiles_controller.rb | 6 +----- app/models/user.rb | 12 +++++++++++- config/locales/bg.yml | 1 + .../views/public/personal_profiles/_form.slim | 18 ++++++++++++++++++ lib/initfest/views/public/shared/_nav.slim | 2 +- 6 files changed, 33 insertions(+), 8 deletions(-) diff --git a/app/controllers/public/events_controller.rb b/app/controllers/public/events_controller.rb index d3e3f4d..2262080 100644 --- a/app/controllers/public/events_controller.rb +++ b/app/controllers/public/events_controller.rb @@ -37,7 +37,7 @@ module Public if current_user.personal_profile(current_conference).present? redirect_to root_path else - redirect_to new_personal_profile_path, alert: I18n.t(:please_fill_in_your_speaker_profile) + redirect_to edit_personal_profile_path, alert: I18n.t(:please_fill_in_your_speaker_profile) end end end diff --git a/app/controllers/public/personal_profiles_controller.rb b/app/controllers/public/personal_profiles_controller.rb index 2c0f544..de8eb62 100644 --- a/app/controllers/public/personal_profiles_controller.rb +++ b/app/controllers/public/personal_profiles_controller.rb @@ -2,10 +2,6 @@ module Public class PersonalProfilesController < Public::ApplicationController before_filter :authenticate_user! - def new - @profile = current_user.build_personal_profile(current_conference) - end - def create @profile = current_user.build_personal_profile(current_conference, profile_params) @@ -18,7 +14,7 @@ module Public end def edit - @profile = current_user.personal_profile(current_conference) + @profile = current_user.find_or_build_personal_profile(current_conference) end def update diff --git a/app/models/user.rb b/app/models/user.rb index 002d401..f39b91b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,8 +9,18 @@ class User < ActiveRecord::Base has_many :workshops has_many :events + def find_or_build_personal_profile(conference, params = {}) + current_profile = personal_profile(conference) + if current_profile.present? + current_profile.assign_attributes params + current_profile + else + build_personal_profile(conference, params) + end + end + def build_personal_profile(conference, params = {}) - new_personal_profile = personal_profiles.last.dup || personal_profiles.build + new_personal_profile = personal_profiles.last.try(:dup) || personal_profiles.build new_personal_profile.conference = conference new_personal_profile.assign_attributes params new_personal_profile diff --git a/config/locales/bg.yml b/config/locales/bg.yml index b5fc7a7..5d22fde 100644 --- a/config/locales/bg.yml +++ b/config/locales/bg.yml @@ -119,6 +119,7 @@ bg: password: Парола password_confirmation: Отново паролата remember_me: Запомни ме + language: Език personal_profile: picture: Снимка first_name: Име diff --git a/lib/initfest/views/public/personal_profiles/_form.slim b/lib/initfest/views/public/personal_profiles/_form.slim index 0d5fd37..e9532b2 100644 --- a/lib/initfest/views/public/personal_profiles/_form.slim +++ b/lib/initfest/views/public/personal_profiles/_form.slim @@ -17,3 +17,21 @@ .form-actions = f.button :submit + +h3.entry-title = t :login_data + += simple_form_for(current_user, wrapper: :default, as: :user, url: registration_path(:user), html: { method: :put, multipart: true }) do |f| + .form-inputs + = f.input :email, required: true + = f.input :language, collection: I18n.available_locales, required: true + + - if current_user.pending_reconfirmation? + p + = t :expected_validation, email: current_user.unconfirmed_email + + = f.input :password, autocomplete: "off", hint: t(:pass_update_hint1), required: false + = f.input :password_confirmation, required: false + = f.input :current_password, hint: t(:pass_update_hint2), required: true + + .form-actions + = f.button :submit, t(:update) diff --git a/lib/initfest/views/public/shared/_nav.slim b/lib/initfest/views/public/shared/_nav.slim index 106de2d..cd3e21d 100644 --- a/lib/initfest/views/public/shared/_nav.slim +++ b/lib/initfest/views/public/shared/_nav.slim @@ -17,7 +17,7 @@ nav = link_to t(:login), new_user_session_path - else == content_tag :li, class: [('current_page_item' if controller_name == 'registrations')] do - = link_to t(:edit_speaker_profile), edit_user_registration_path + = link_to t(:edit_speaker_profile), edit_personal_profile_path li = link_to t(:logout), destroy_user_session_path, method: :delete li