Update profiles, not users

This commit is contained in:
Andrew Radev 2015-07-28 15:43:19 +03:00
parent 7a8bba1646
commit d8986f542d
11 changed files with 130 additions and 134 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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'

View File

@ -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'

View File

@ -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])

View File

@ -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"

View File

@ -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'

View File

@ -72,6 +72,9 @@ bg:
user:
one: Потребител
other: Потребители
personal_profile:
one: Профил
other: Профили
lecture:
one: Лекция
other: Лекции

View File

@ -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