WIP attempt to create new profile for conference

This commit is contained in:
Andrew Radev 2015-07-26 15:58:08 +03:00
parent a1a3155e0b
commit 7a8bba1646
8 changed files with 81 additions and 10 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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