diff --git a/app/controllers/management/users_controller.rb b/app/controllers/management/users_controller.rb index 16e4d40..4ef457e 100644 --- a/app/controllers/management/users_controller.rb +++ b/app/controllers/management/users_controller.rb @@ -18,19 +18,30 @@ module Management @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 - @user = find_user + @conference = find_conference + @user = find_user if @user.update_attributes(user_params) - redirect_to [:management, @user] + redirect_to [:management, @conference, @user] else render action: 'edit' end @@ -56,7 +67,7 @@ module Management def user_params params.require(:user).permit( :email, - personal_profile_attributes: [ + personal_profiles_attributes: [ :picture, :first_name, :last_name, diff --git a/app/models/event.rb b/app/models/event.rb index f760184..c1c1f2c 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -12,4 +12,8 @@ class Event < ActiveRecord::Base validates :agreement, acceptance: true scope :confirmed, -> { where.not confirmed_at: nil } + + def proposer_profile + proposer.personal_profile(conference) + end end diff --git a/app/models/user.rb b/app/models/user.rb index 613b481..1738677 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,15 +9,28 @@ class User < ActiveRecord::Base has_many :workshops has_many :events - accepts_nested_attributes_for :personal_profiles, update_only: true + accepts_nested_attributes_for :personal_profiles default_scope { order id: :desc } def personal_profile(conference) - personal_profiles.find_by!(conference_id: conference.id) + personal_profiles.find_by(conference_id: conference.id) end def toggle_admin! update admin: !admin end + + def clone_recent_profile(new_conference) + recent_profile = personal_profiles.order('created_at DESC').first + + personal_profiles.build(conference: new_conference) do |new_profile| + if recent_profile.present? + new_profile.attributes = recent_profile.attributes.except( + "id", "created_at", "updated_at", "conference_id" + ) + new_profile.remote_picture_url = recent_profile.picture.url + end + end + end end diff --git a/app/views/management/events/index.html.slim b/app/views/management/events/index.html.slim index 41aaa15..c58d3a0 100644 --- a/app/views/management/events/index.html.slim +++ b/app/views/management/events/index.html.slim @@ -17,11 +17,12 @@ tbody - @events.each do |event| - proposer = event.proposer + - proposer_profile = event.proposer_profile tr td= event.title td= event.subtitle - td= link_to proposer.personal_profile(@conference).name, [:management, @conference, proposer] + td= link_to proposer_profile.name, [:management, @conference, proposer] td #{event.length} minutes td= event.language diff --git a/app/views/management/users/edit.html.slim b/app/views/management/users/edit.html.slim index 078f276..46d6408 100644 --- a/app/views/management/users/edit.html.slim +++ b/app/views/management/users/edit.html.slim @@ -18,11 +18,13 @@ hr .row .col-lg-12 - -# TODO we have many personal_profiles now - = f.simple_fields_for :personal_profile do |ff| - - if @profile.picture.present? + = 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 @profile.picture.medium.url, class: 'img-thumbnail' + = image_tag ff.object.picture.medium.url, class: 'img-thumbnail' = ff.input :picture, wrapper: :horizontal_file_input diff --git a/config/application.rb b/config/application.rb index 0cc5d28..90aca35 100644 --- a/config/application.rb +++ b/config/application.rb @@ -21,6 +21,8 @@ module Clarion config.i18n.available_locales = [:bg, :en] config.i18n.default_locale = :bg + config.active_record.raise_in_transactional_callbacks = true + config.generators do |g| g.test_framework :rspec, fixtures: true, view_specs: false, diff --git a/spec/factories/personal_profile.rb b/spec/factories/personal_profile.rb new file mode 100644 index 0000000..081fddb --- /dev/null +++ b/spec/factories/personal_profile.rb @@ -0,0 +1,14 @@ +FactoryGirl.define do + factory :personal_profile do + association :user + + first_name "Some" + last_name "Person" + picture File.open(Rails.root.join("spec/support/picture.jpg")) + mobile_phone "0883 123 456" + biography "Biography" + sequence(:public_email) { |n| "email#{n}@example.com" } + twitter "example" + github "example" + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 155b44a..9272a0f 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -4,4 +4,28 @@ RSpec.describe User do it 'lets Devise handle email and password validations' do expect(build(:user)).to be_a Devise::Models::Validatable end + + it "can clone a previous profile for use in a different conference" do + user = create :user + old_conference = create :conference + new_conference = create :conference + + old_profile = create :personal_profile, { + user: user, + biography: "Old profile bio", + conference: old_conference + } + + expect(user.personal_profile(old_conference)).to eq old_profile + expect(user.personal_profile(new_conference)).to be_nil + + new_profile = user.clone_recent_profile(new_conference) + expect(new_profile.biography).to eq old_profile.biography + + user.reload + user.personal_profiles << new_profile + + expect(user.personal_profile(old_conference)).to eq old_profile + expect(user.personal_profile(new_conference)).to eq new_profile + end end