Fogbugz #74: Buddy list: allow user to mark that an existing address is to be re-used.
This commit is contained in:
parent
c3c9d972f3
commit
ace4a40c46
4 changed files with 106 additions and 19 deletions
|
|
@ -7,12 +7,7 @@ class ContactsController < ApplicationController
|
|||
end
|
||||
|
||||
def new
|
||||
@contact = Contact.new
|
||||
# @contact.address = Address.new
|
||||
@contact.build_address
|
||||
email = @contact.emails.build
|
||||
phone = @contact.phones.build
|
||||
setup_months_and_days
|
||||
prepare_for_new(nil)
|
||||
end
|
||||
|
||||
def create
|
||||
|
|
@ -20,13 +15,29 @@ class ContactsController < ApplicationController
|
|||
c.firstname = params[:contact][:firstname]
|
||||
c.lastname = params[:contact][:lastname]
|
||||
|
||||
c.build_address
|
||||
c.address.street = params[:contact][:address_attributes][:street]
|
||||
c.address.housenr = params[:contact][:address_attributes][:housenr]
|
||||
c.address.zipcode = params[:contact][:address_attributes][:zipcode]
|
||||
c.address.place = params[:contact][:address_attributes][:place]
|
||||
c.address.country = params[:contact][:address_attributes][:country]
|
||||
|
||||
# Do we have a buddy? If so, share the address.
|
||||
if params[:contact][:has_buddy] = 1
|
||||
# look up buddy address
|
||||
buddy_fn = params[:buddy_firstname]
|
||||
buddy_ln = params[:buddy_lastname]
|
||||
addr = Address.by_first_and_last_name(buddy_fn, buddy_ln)
|
||||
if addr.nil?
|
||||
flash[:error] = "Den Buddy " + buddy_fn + " " + buddy_ln + " gibt's nicht."
|
||||
# preserve all contact data to avoid user having to enter anything twice
|
||||
prepare_for_new(params[:contact])
|
||||
render(:action => "new") and return
|
||||
end
|
||||
# Buddy's address is OK.
|
||||
c.address = addr
|
||||
else # It's someone without a buddy.
|
||||
c.build_address
|
||||
c.address.street = params[:contact][:address_attributes][:street]
|
||||
c.address.housenr = params[:contact][:address_attributes][:housenr]
|
||||
c.address.zipcode = params[:contact][:address_attributes][:zipcode]
|
||||
c.address.place = params[:contact][:address_attributes][:place]
|
||||
c.address.country = params[:contact][:address_attributes][:country]
|
||||
end
|
||||
|
||||
# birthdate comes with the year as optional value
|
||||
c.birth_year = params[:birth_year].to_i unless params[:birth_year].blank?
|
||||
c.birth_month = params[:birth_month].to_i
|
||||
|
|
@ -35,16 +46,18 @@ class ContactsController < ApplicationController
|
|||
# emails come as: ["0", {"address"=>"a@b.c"}]
|
||||
params[:contact][:emails_attributes].map {|k,vs| vs}.each { |val|
|
||||
unless val["address"].blank?
|
||||
email = c.emails.build
|
||||
email.address = val["address"]
|
||||
email = c.emails.build
|
||||
email.desc = val["desc"]
|
||||
email.address = val["address"]
|
||||
end
|
||||
}
|
||||
|
||||
# phones come as: {"0"=>{"nr"=>"+49123456789"}, "1"=>{"nr"=>"..."}}
|
||||
params[:contact][:phones_attributes].map {|k,vs| vs}.each { |val|
|
||||
unless val["nr"].blank?
|
||||
phone = c.phones.build
|
||||
phone.nr = val["nr"]
|
||||
phone = c.phones.build
|
||||
phone.desc = val["desc"]
|
||||
phone.nr = val["nr"]
|
||||
end
|
||||
}
|
||||
|
||||
|
|
@ -66,10 +79,20 @@ class ContactsController < ApplicationController
|
|||
|
||||
def edit
|
||||
@contact = Contact.find(params[:id])
|
||||
@contact.build_address if @contact.address.nil?
|
||||
setup_months_and_days
|
||||
end
|
||||
|
||||
def update
|
||||
c = Contact.find(params[:id])
|
||||
c.update_attributes(params[:contact])
|
||||
if c.save!
|
||||
flash[:notice] = 'Kontakt aktualisiert.'
|
||||
redirect_to :action => :index
|
||||
else
|
||||
flash[:error] = 'Fehler beim Aktualisieren des Kontakts'
|
||||
render :action => :edit
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
|
|
@ -77,6 +100,20 @@ class ContactsController < ApplicationController
|
|||
|
||||
private
|
||||
|
||||
# does everything needed to render :new.
|
||||
# param: a pre filled Contact map (params) or 'nil' if we don't know anything, yet.
|
||||
def prepare_for_new(contact)
|
||||
@contact = Contact.new
|
||||
if contact.nil?
|
||||
@contact.build_address
|
||||
email = @contact.emails.build
|
||||
phone = @contact.phones.build
|
||||
else
|
||||
@contact.fill_attributes_from_map(contact)
|
||||
end
|
||||
setup_months_and_days
|
||||
end
|
||||
|
||||
def setup_months_and_days
|
||||
# list of valid months and days
|
||||
@months = Array.new
|
||||
|
|
|
|||
|
|
@ -1,3 +1,10 @@
|
|||
class Address < ActiveRecord::Base
|
||||
has_many :contacts
|
||||
|
||||
# returns the address of the contact /w the given first + last name.
|
||||
def self.by_first_and_last_name(firstname, lastname)
|
||||
c = Contact.where("firstname = ? AND lastname = ?", firstname, lastname)
|
||||
ret = c[0].address unless c[0].nil?
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -38,4 +38,35 @@ class Contact < ActiveRecord::Base
|
|||
def twitter_username_link
|
||||
'<a href="http://twitter.com/' + twitter_username + '">@' + twitter_username + "</a>"
|
||||
end
|
||||
|
||||
# Fills all attributes with the ones given in the passed map
|
||||
# (which may come as 'params' from the user.
|
||||
def fill_attributes_from_map(c)
|
||||
self.firstname = c[:firstname]
|
||||
self.lastname = c[:lastname]
|
||||
self.build_address
|
||||
self.address.street = c[:address_attributes][:street]
|
||||
self.address.housenr = c[:address_attributes][:housenr]
|
||||
self.address.zipcode = c[:address_attributes][:zipcode]
|
||||
self.address.place = c[:address_attributes][:place]
|
||||
self.address.country = c[:address_attributes][:country]
|
||||
|
||||
# emails come as: ["0", {"address"=>"a@b.c"}]
|
||||
c[:emails_attributes].map {|k,vs| vs}.each { |val|
|
||||
unless val["address"].blank?
|
||||
self.email = c.emails.build
|
||||
self.email.desc = val["desc"]
|
||||
self.email.address = val["address"]
|
||||
end
|
||||
}
|
||||
|
||||
# phones come as: {"0"=>{"nr"=>"+49123456789"}, "1"=>{"nr"=>"..."}}
|
||||
c[:phones_attributes].map {|k,vs| vs}.each { |val|
|
||||
unless val["nr"].blank?
|
||||
self.phone = c.phones.build
|
||||
self.phone.desc = val["desc"]
|
||||
self.phone.nr = val["nr"]
|
||||
end
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,16 +1,28 @@
|
|||
<%= form_for @contact, :html => {:class => "new_data_form"} do |f| %>
|
||||
|
||||
<fieldset>
|
||||
<fieldset>
|
||||
<%# <legend>Contact details</legend> %>
|
||||
<div class="row">
|
||||
<div class="twocol"><%= label_tag(:firstname, "First name") %></div>
|
||||
<div class="threecol"><%= f.text_field :firstname, :placeholder => "Vorname" %></div>
|
||||
<div class="threecol"><%= f.text_field :firstname, :placeholder => "Vorname", :value => @contact.firstname %></div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="twocol"><%= label_tag(:lastname, "Last name") %></div>
|
||||
<div class="threecol"><%= f.text_field :lastname, :placeholder => "Nachname" %></div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="twocol">Buddies?</div>
|
||||
<div class="threecol">
|
||||
<%= check_box_tag(:has_buddy) %>
|
||||
<%= label_tag(:has_buddy, "wohnt zusammen mit") %>
|
||||
</div>
|
||||
<div class="sixcol">
|
||||
<%= text_field_tag(:buddy_firstname, nil, :placeholder => "first name") %>
|
||||
<%= text_field_tag(:buddy_lastname, nil, :placeholder => "last name") %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<%= f.fields_for :address do |address_f| %>
|
||||
<div class="row">
|
||||
<div class="twocol"><%= address_f.label :street, "Street · no.".html_safe %></div>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue