From d8986f542d0cc9d06ed86b67081cb1fd31cc0f5d Mon Sep 17 00:00:00 2001 From: Andrew Radev Date: Tue, 28 Jul 2015 15:43:19 +0300 Subject: [PATCH] Update profiles, not users --- .../personal_profiles_controller.rb | 77 +++++++++++++++++ .../management/users_controller.rb | 84 ------------------- app/models/personal_profile.rb | 2 + app/models/user.rb | 2 - .../layouts/management/_navigation.html.slim | 3 + .../personal_profiles/edit.html.slim | 41 +++++++++ .../index.html.slim | 4 +- .../show.html.slim | 2 +- app/views/management/users/edit.html.slim | 44 ---------- config/locales/bg.yml | 3 + config/routes.rb | 2 +- 11 files changed, 130 insertions(+), 134 deletions(-) create mode 100644 app/controllers/management/personal_profiles_controller.rb delete mode 100644 app/controllers/management/users_controller.rb create mode 100644 app/views/management/personal_profiles/edit.html.slim rename app/views/management/{users => personal_profiles}/index.html.slim (83%) rename app/views/management/{users => personal_profiles}/show.html.slim (95%) delete mode 100644 app/views/management/users/edit.html.slim diff --git a/app/controllers/management/personal_profiles_controller.rb b/app/controllers/management/personal_profiles_controller.rb new file mode 100644 index 0000000..07b534d --- /dev/null +++ b/app/controllers/management/personal_profiles_controller.rb @@ -0,0 +1,77 @@ +module Management + # TODO (2015-07-28) How to create a new profile for a particular profile if given a user? What would be the interface? + class PersonalProfilesController < ManagementController + def index + @conference = find_conference + @profiles = @conference.participant_profiles + end + + def toggle_admin + @conference = find_conference + @user = find_profile.user + + @user.toggle_admin! + redirect_to :back + end + + def show + @conference = find_conference + @profile = find_profile + @user = @profile.user + + if not @profile + flash[:error] = "No profile, needs to be created" + redirect_to action: :edit + end + end + + def edit + @conference = find_conference + @profile = find_profile + @user = @profile.user + end + + def update + @conference = find_conference + @profile = find_profile + + if @profile.update_attributes(profile_params) + redirect_to [:management, @conference, @profile] + else + render action: 'edit' + end + end + + def destroy + @profile = find_profile + @profile.destroy + + redirect_to action: :index + end + + private + + def find_profile + PersonalProfile.find(params[:id]) + end + + def find_conference + Conference.find(params[:conference_id]) + end + + def profile_params + params.require(:personal_profile).permit( + :picture, + :first_name, + :last_name, + :mobile_phone, + :biography, + :organisation, + :public_email, + :github, + :twitter, + user_attributes: [:id, :email] + ) + end + end +end diff --git a/app/controllers/management/users_controller.rb b/app/controllers/management/users_controller.rb deleted file mode 100644 index 4ef457e..0000000 --- a/app/controllers/management/users_controller.rb +++ /dev/null @@ -1,84 +0,0 @@ -# TODO Needs to work with profiles, not with users -module Management - class UsersController < ManagementController - def index - @conference = find_conference - @profiles = @conference.participant_profiles - end - - def toggle_admin - @conference = find_conference - @user = find_user - - @user.toggle_admin! - redirect_to :back - end - - def show - @conference = find_conference - @user = find_user - @profile = @user.personal_profile(@conference) - - if not @profile - flash[:error] = "No profile, needs to be created" - redirect_to action: :edit - end - end - - def edit - @conference = find_conference - @user = find_user - @profile = @user.personal_profile(@conference) - - # TODO (2015-07-26) Totally not working, can't create new profile properly - if not @profile - @profile = @user.clone_recent_profile(@conference) - end - end - - def update - @conference = find_conference - @user = find_user - - if @user.update_attributes(user_params) - redirect_to [:management, @conference, @user] - else - render action: 'edit' - end - end - - def destroy - @user = find_user - @user.destroy - - redirect_to action: :index - end - - private - - def find_user - User.find(params[:id]) - end - - def find_conference - Conference.find(params[:conference_id]) - end - - def user_params - params.require(:user).permit( - :email, - personal_profiles_attributes: [ - :picture, - :first_name, - :last_name, - :mobile_phone, - :biography, - :organisation, - :public_email, - :github, - :twitter, - ] - ) - end - end -end diff --git a/app/models/personal_profile.rb b/app/models/personal_profile.rb index afebe25..f513571 100644 --- a/app/models/personal_profile.rb +++ b/app/models/personal_profile.rb @@ -15,6 +15,8 @@ class PersonalProfile < ActiveRecord::Base mount_uploader :picture, PictureUploader + accepts_nested_attributes_for :user + def twitter=(handle) write_attribute :twitter, handle.gsub(/\A@/,'') if handle end diff --git a/app/models/user.rb b/app/models/user.rb index 1738677..69831b6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,8 +9,6 @@ class User < ActiveRecord::Base has_many :workshops has_many :events - accepts_nested_attributes_for :personal_profiles - default_scope { order id: :desc } def personal_profile(conference) diff --git a/app/views/layouts/management/_navigation.html.slim b/app/views/layouts/management/_navigation.html.slim index be138bf..e4ee0cf 100644 --- a/app/views/layouts/management/_navigation.html.slim +++ b/app/views/layouts/management/_navigation.html.slim @@ -20,6 +20,9 @@ nav.navbar.navbar-static-top.navbar-inverse role="navigation" li class="#{'active' if controller_name == 'events'}" = link_to [:management, current_conference, :events] do => icon 'file-text', Event.model_name.human(count: 2).mb_chars.capitalize, class: 'fa-fw' + li class="#{'active' if controller_name == 'personal_profiles'}" + = link_to [:management, current_conference, :personal_profiles] do + => icon 'user', t('activerecord.models.personal_profile', count: 2).mb_chars.capitalize, class: 'fa-fw' li class="#{'active' if controller_name == 'sponsorship_offers'}" = link_to '#' do => icon 'money', t('activerecord.models.sponsorship_offer', count: 2).mb_chars.capitalize, class: 'fa-fw' diff --git a/app/views/management/personal_profiles/edit.html.slim b/app/views/management/personal_profiles/edit.html.slim new file mode 100644 index 0000000..70d1e09 --- /dev/null +++ b/app/views/management/personal_profiles/edit.html.slim @@ -0,0 +1,41 @@ +- content_for :title + => t 'actions.edit.title', model: User.model_name.human + = @profile.name + +.row + .col-lg-12 + = simple_nested_form_for [:management, @conference, @profile], wrapper: :horizontal_form, html: { class: 'form-horizontal' } do |f| + .panel.panel-primary + .panel-heading + h1.panel-title + = t 'views.user.info' + = link_to icon(:eye), [:management, @conference, @profile], class: 'btn btn-xs btn-info pull-right' + + .panel-body + .row + .col-lg-12 + = f.simple_fields_for :user do |ff| + = ff.input :email + hr + .row + .col-lg-12 + - if f.object.picture.present? + .col-sm-offset-3.col-sm-9 + = image_tag f.object.picture.medium.url, class: 'img-thumbnail' + + = f.input :picture, wrapper: :horizontal_file_input + + -# Required + = f.input :first_name + = f.input :last_name + = f.input :mobile_phone + = f.input :biography + + -# Optional + = f.input :organisation + = f.input :public_email + = f.input :github + = f.input :twitter + + .panel-footer.text-right + = f.submit class: 'btn btn-primary' diff --git a/app/views/management/users/index.html.slim b/app/views/management/personal_profiles/index.html.slim similarity index 83% rename from app/views/management/users/index.html.slim rename to app/views/management/personal_profiles/index.html.slim index 06e105c..dd3a77b 100644 --- a/app/views/management/users/index.html.slim +++ b/app/views/management/personal_profiles/index.html.slim @@ -19,7 +19,7 @@ td= image_tag(profile.picture.thumb.url) td= profile.name td.boolean-col - = link_to toggle_admin_management_user_path(user), method: :put do + = link_to [:toggle_admin, :management, @conference, profile], method: :put do - if user.admin? .btn.btn-primary= icon('circle-o') - else @@ -27,4 +27,4 @@ td.actions div.btn-group.btn-group-sm - = action_buttons(@conference, user, [:show, :edit, :destroy]) + = action_buttons(@conference, profile, [:show, :edit, :destroy]) diff --git a/app/views/management/users/show.html.slim b/app/views/management/personal_profiles/show.html.slim similarity index 95% rename from app/views/management/users/show.html.slim rename to app/views/management/personal_profiles/show.html.slim index 1004508..303c3a6 100644 --- a/app/views/management/users/show.html.slim +++ b/app/views/management/personal_profiles/show.html.slim @@ -7,7 +7,7 @@ .panel-heading h1.panel-title = @profile.name - = link_to icon(:edit), [:edit, :management, @conference, @user], class: 'btn btn-xs btn-danger pull-right' + = link_to icon(:edit), [:edit, :management, @conference, @profile], class: 'btn btn-xs btn-danger pull-right' .panel-body .center = image_tag @profile.picture.medium.url, class: "profile-image" diff --git a/app/views/management/users/edit.html.slim b/app/views/management/users/edit.html.slim deleted file mode 100644 index 46d6408..0000000 --- a/app/views/management/users/edit.html.slim +++ /dev/null @@ -1,44 +0,0 @@ -- content_for :title - => t 'actions.edit.title', model: User.model_name.human - = @profile.name - -.row - .col-lg-12 - = simple_nested_form_for [:management, @conference, @user], wrapper: :horizontal_form, html: { class: 'form-horizontal' } do |f| - .panel.panel-primary - .panel-heading - h1.panel-title - = t 'views.user.info' - = link_to icon(:eye), [:management, @conference, @user], class: 'btn btn-xs btn-info pull-right' - - .panel-body - .row - .col-lg-12 - = f.input :email - hr - .row - .col-lg-12 - = f.simple_fields_for :personal_profiles do |ff| - = ff.object.inspect - - next if ff.object.conference != @conference - - - if ff.object.picture.present? - .col-sm-offset-3.col-sm-9 - = image_tag ff.object.picture.medium.url, class: 'img-thumbnail' - - = ff.input :picture, wrapper: :horizontal_file_input - - -# Required - = ff.input :first_name - = ff.input :last_name - = ff.input :mobile_phone - = ff.input :biography - - -# Optional - = ff.input :organisation - = ff.input :public_email - = ff.input :github - = ff.input :twitter - - .panel-footer.text-right - = f.submit class: 'btn btn-primary' diff --git a/config/locales/bg.yml b/config/locales/bg.yml index 3e892db..13dfe4a 100644 --- a/config/locales/bg.yml +++ b/config/locales/bg.yml @@ -72,6 +72,9 @@ bg: user: one: Потребител other: Потребители + personal_profile: + one: Профил + other: Профили lecture: one: Лекция other: Лекции diff --git a/config/routes.rb b/config/routes.rb index 9303a22..dd4b99c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,7 +11,7 @@ Rails.application.routes.draw do resources :sponsorship_offers resource :call_for_participation, only: [:create, :destroy] - resources :users do + resources :personal_profiles do member do put :toggle_admin end