diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..1e5eaf1 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,13 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + before_filter :configure_permitted_parameters, if: :devise_controller? + + protected + + def configure_permitted_parameters + devise_parameter_sanitizer.for(:account_update) do |u| + u.permit(:email, :password, :password_confirmation, :current_password, speaker_profile_attributes: [:first_name, :last_name, :public_email, :organisation, :github, :twitter, :mobile_phone, :biography]) + end + end end diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb new file mode 100644 index 0000000..a781c0c --- /dev/null +++ b/app/controllers/registrations_controller.rb @@ -0,0 +1,35 @@ +class RegistrationsController < Devise::RegistrationsController + def edit + resource.build_speaker_profile unless resource.speaker_profile.present? + end + + def update + @user = User.find(current_user.id) + + successfully_updated = if needs_password?(@user, params) + @user.update_with_password(devise_parameter_sanitizer.sanitize(:account_update)) + else + # remove the virtual current_password attribute + # update_without_password doesn't know how to ignore it + params[:user].delete(:current_password) + @user.update_without_password(devise_parameter_sanitizer.sanitize(:account_update)) + end + + if successfully_updated + set_flash_message :notice, :updated + # Sign in the user bypassing validation in case their password changed + sign_in @user, :bypass => true + redirect_to after_update_path_for(@user) + else + render "edit" + end + end + + private + + def needs_password?(user, params) + user.email != params[:user][:email] || + params[:user][:password].present? || + params[:user][:password_confirmation].present? + end +end diff --git a/app/controllers/speaker_profiles_controller.rb b/app/controllers/speaker_profiles_controller.rb new file mode 100644 index 0000000..a51e033 --- /dev/null +++ b/app/controllers/speaker_profiles_controller.rb @@ -0,0 +1,16 @@ +class SpeakerProfilesController < ApplicationController + before_filter :authenticate_user! + before_action :assign_speaker_profile + + def edit + end + + def update + end + + private + + def assign_speaker_profile + @speaker_profile = SpeakerProfile.find_or_initialize_by(user: current_user) + end +end diff --git a/app/models/user.rb b/app/models/user.rb index f7fa261..8ae638a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,4 +7,6 @@ class User < ActiveRecord::Base has_one :speaker_profile has_many :lectures has_many :workshops + + accepts_nested_attributes_for :speaker_profile, update_only: true end diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb deleted file mode 100644 index 5db350b..0000000 --- a/app/views/devise/registrations/edit.html.erb +++ /dev/null @@ -1,27 +0,0 @@ -
Currently waiting confirmation for: <%= resource.unconfirmed_email %>
- <% end %> - - <%= f.input :password, autocomplete: "off", hint: "leave it blank if you don't want to change it", required: false %> - <%= f.input :password_confirmation, required: false %> - <%= f.input :current_password, hint: "we need your current password to confirm your changes", required: true %> -Unhappy? <%= link_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %>
- -<%= link_to "Back", :back %> diff --git a/app/views/devise/registrations/edit.html.slim b/app/views/devise/registrations/edit.html.slim new file mode 100644 index 0000000..205756f --- /dev/null +++ b/app/views/devise/registrations/edit.html.slim @@ -0,0 +1,30 @@ +h2.entry-title Редакция на акаунт + += simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| + = f.error_notification + + .form-inputs + h3 Данни за вход в системата + = f.input :email, required: true, autofocus: true + + - if devise_mapping.confirmable? && resource.pending_reconfirmation? + p Currently waiting confirmation for: #{resource.unconfirmed_email} + + = f.input :password, autocomplete: "off", hint: "leave it blank if you don't want to change it", required: false + = f.input :password_confirmation, required: false + = f.input :current_password, hint: "we need your current password to confirm your changes", required: true + + .form_inputs + h3 Лекторски профил + = f.simple_fields_for :speaker_profile do |ff| + = ff.input :first_name + = ff.input :last_name + = ff.input :public_email + = ff.input :organisation + = ff.input :github + = ff.input :twitter + = ff.input :mobile_phone, input_html: {value: resource.speaker_profile.mobile_phone.try(:phony_formatted, format: :international)} + = ff.input :biography + + .form-actions + = f.button :submit, 'Обнови' diff --git a/app/views/speaker_profiles/_form.html.slim b/app/views/speaker_profiles/_form.html.slim new file mode 100644 index 0000000..6c3702b --- /dev/null +++ b/app/views/speaker_profiles/_form.html.slim @@ -0,0 +1,14 @@ +== simple_form_for @speaker_profile do |form| + p + = form.error_notification + + .form-inputs + = form.input :first_name, autofocus: true + = form.input :last_name + = form.input :public_email + = form.input :organisation + = form.input :github + = form.input :twitter + = form.input :mobile_phone + = form.input :biography + = form.button :submit diff --git a/app/views/speaker_profiles/edit.html.slim b/app/views/speaker_profiles/edit.html.slim new file mode 100644 index 0000000..bcfe4cf --- /dev/null +++ b/app/views/speaker_profiles/edit.html.slim @@ -0,0 +1,3 @@ +h1.entry-title Редакция на лекторски профил + +== render 'form' diff --git a/config/locales/bg.yml b/config/locales/bg.yml index 7851063..1a0bf49 100644 --- a/config/locales/bg.yml +++ b/config/locales/bg.yml @@ -38,6 +38,15 @@ bg: password: Парола password_confirmation: Отново паролата remember_me: Запомни ме + speaker_profile: + first_name: Име + last_name: Фамилия + organisation: Организация + public_email: Публичен email + mobile_phone: Мобилен телефон + biography: Биография + github: Github акаунт + twitter: Twitter акаунт lecture: title: Заглавие subtitle: Подзаглавие diff --git a/config/locales/simple_form.bg.yml b/config/locales/simple_form.bg.yml index 80cf975..4adc6e2 100644 --- a/config/locales/simple_form.bg.yml +++ b/config/locales/simple_form.bg.yml @@ -12,6 +12,15 @@ bg: email: Имейл адресът Ви. Ще бъде видим само от организаторите. password: Парола с дължина между 8 и 128 символа password_confirmation: Отново въведената отгоре парола + speaker_profile: + first_name: Малкото Ви име + last_name: Фамилното Ви име + organisation: Организацията, която представлявате + public_email: E-mail адрес, който ще бъде видим за посетителите + mobile_phone: Мобилен телефон, който ще бъде видим само за организаторите + biography: Опишете се с няколко изречения в трето лице :). + github: Github акаунтът Ви + twitter: Twitter акаунтът Ви lecture: title: Заглавието на лекцията Ви subtitle: Подзаглавието на лекцията Ви (ако има такова) @@ -29,4 +38,7 @@ bg: language: Език, на който ще бъде воден уъркшопа abstract: Резюме на уъркшопа, което да може да бъде прочетено от посетителите description: Подробно описание на уъркшопа, което да бъде използвано от организаторския екип - notes: Забележки, които искате да споделите с организаторския екип \ No newline at end of file + notes: Забележки, които искате да споделите с организаторския екип + labels: + user: + a: b \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 65c7913..0b5afc5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,11 @@ Rails.application.routes.draw do resources :lectures, only: [:index, :new, :create, :edit, :update, :show] resources :workshops, only: [:index, :new, :create, :edit, :update, :show] - devise_for :users + devise_for :users, controllers: {registrations: 'registrations'} + + resource :user, only: [] do + resource :speaker_profile, only: [:edit, :update] + end root 'home#index' # The priority is based upon order of creation: first created -> highest priority. diff --git a/db/migrate/20140807103632_create_speaker_profiles.rb b/db/migrate/20140807103632_create_speaker_profiles.rb index 286e036..9abfa88 100644 --- a/db/migrate/20140807103632_create_speaker_profiles.rb +++ b/db/migrate/20140807103632_create_speaker_profiles.rb @@ -1,15 +1,15 @@ class CreateSpeakerProfiles < ActiveRecord::Migration def change create_table :speaker_profiles do |t| - t.string :first_name, null: false - t.string :last_name, null: false + t.string :first_name + t.string :last_name t.string :organisation t.string :public_email - t.string :photo_url, null: false - t.string :mobile_phone, null: false - t.text :biography, null: false - t.string :github, null: false - t.string :twitter, null: false + t.string :photo_url + t.string :mobile_phone + t.text :biography + t.string :github + t.string :twitter t.timestamps end