Skip to content

Commit

Permalink
Merged a few duplicated display functions into merge_helper. Improved…
Browse files Browse the repository at this point in the history
… formatting of subscribed users.
  • Loading branch information
steveyken committed Feb 7, 2013
1 parent fdac40f commit aece50c
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 45 deletions.
3 changes: 3 additions & 0 deletions README.markdown
Expand Up @@ -32,6 +32,9 @@ New in version 1.3 (steveyken)
* Fixed bug where assigned_to, user_id and reports_to were not always set correctly
* Added ability to merge addresses visually
* Moved styles out of template and into css
* Custom field labels are translated on merge form
** Expects the translation lookup path to be 'active_record.attributes.contact.cf_your_custom_field_name'
* Improved subscribed_users formatting

MERGE HOOKS
===========
Expand Down
31 changes: 9 additions & 22 deletions app/helpers/accounts_merge_helper.rb
Expand Up @@ -6,43 +6,30 @@ module AccountsMergeHelper
# --------------------------------------------------------
def account_merge_attributes(account, html = true)
attr_hash = account.merge_attributes

# ------ Humanize attributes

merge_boolean_attributes!(attr_hash)
merge_subscribed_users_attributes!(attr_hash)
merge_address_attributes!(attr_hash, account)

attr_hash['source'] = attr_hash['source'].humanize if attr_hash['source']
# ------ Make websites into links and emails into mailto links
if html
%w(blog linkedin facebook twitter).each do |attribute|
if link = attr_hash[attribute] and not link.blank?
attr_hash[attribute] = link_to_new_window(link, link)
end
end
%w(email alt_email).each do |attribute|
%w(email website).each do |attribute|
if email = attr_hash[attribute] and not email.blank?
attr_hash[attribute] = mail_to(email)
end
end
end
# ------ Boolean values should be yes/no
attr_hash['do_not_call'] = attr_hash['do_not_call'] == true ? "Yes" : "No"

# ------ User relationships should display user's full name
%w(assigned_to reports_to user_id).each do |attribute|
%w(assigned_to user_id).each do |attribute|
if attr_hash[attribute]
user = User.find(attr_hash[attribute])
attr_hash[attribute] = html ? link_to_new_window(user.full_name, user_path(user)) : user.full_name
end
end
# ------ Lead relationship should display lead's full name
if attr_hash['lead_id']
lead = Lead.find(attr_hash['lead_id'])
attr_hash['lead_id'] = html ? link_to_new_window(lead.full_name, lead_path(lead)) : lead.full_name
end

# ------ Addresses should display in full
account.address_attributes.keys.each do |attribute|
if attr_hash[attribute]
address_type = attribute.gsub('_address', '')
attr_hash[attribute] = render("shared/address_show", :asset => account, :type => address_type)
end
end

attr_hash
end
Expand Down
18 changes: 8 additions & 10 deletions app/helpers/contacts_merge_helper.rb
Expand Up @@ -6,7 +6,13 @@ module ContactsMergeHelper
# --------------------------------------------------------
def contact_merge_attributes(contact, html = true)
attr_hash = contact.merge_attributes

# ------ Humanize attributes

merge_boolean_attributes!(attr_hash)
merge_subscribed_users_attributes!(attr_hash)
merge_address_attributes!(attr_hash, contact)

attr_hash['source'] = attr_hash['source'].humanize if attr_hash['source']
# ------ Make websites into links and emails into mailto links
if html
Expand All @@ -21,29 +27,21 @@ def contact_merge_attributes(contact, html = true)
end
end
end
# ------ Boolean values should be yes/no
attr_hash['do_not_call'] = attr_hash['do_not_call'] == true ? "Yes" : "No"

# ------ User relationships should display user's full name
%w(assigned_to reports_to user_id).each do |attribute|
if attr_hash[attribute]
user = User.find(attr_hash[attribute])
attr_hash[attribute] = html ? link_to_new_window(user.full_name, user_path(user)) : user.full_name
end
end

# ------ Lead relationship should display lead's full name
if attr_hash['lead_id']
lead = Lead.find(attr_hash['lead_id'])
attr_hash['lead_id'] = html ? link_to_new_window(lead.full_name, lead_path(lead)) : lead.full_name
end

# ------ Addresses should display in full
contact.address_attributes.keys.each do |attribute|
if attr_hash[attribute]
address_type = attribute.gsub('_address', '')
attr_hash[attribute] = render("shared/address_show", :asset => contact, :type => address_type)
end
end

attr_hash
end

Expand Down
36 changes: 34 additions & 2 deletions app/helpers/merge_helper.rb
@@ -1,7 +1,7 @@
module MergeHelper

def link_to_new_window(text, path)
link_to(text, path, :target => "_blank")
link_to(h(text), path, :target => "_blank")
end

# Generates a radio button for selecting which attributes
Expand Down Expand Up @@ -46,9 +46,41 @@ def calculate_default_merge(duplicate_attr, master_attr)
# format (ie, with links / model associations), to be
# displayed in the merge selection table.
# --------------------------------------------------------
def custom_field_merge_attributes(field_group, object, html = true)
def custom_field_merge_attributes(field_group, object)
custom_fields = field_group.fields.sort_by(&:position)
custom_fields.inject({}){ |hash, field| hash[field.name] = field.render_value(object); hash }
end

# Transforms any boolean attributes into a display format
# --------------------------------------------------------
def merge_boolean_attributes!(attributes)
attributes.each do |key, value|
if value.is_a?(TrueClass) or value.is_a?(FalseClass)
attributes[key] = (attributes[key] == true) ? "Yes" : "No"
end
end
end

# Transforms subscribed users into a display format
# --------------------------------------------------------
def merge_subscribed_users_attributes!(attr_hash)
if attr_hash.keys.include?('subscribed_users')
attr_hash['subscribed_users'] = attr_hash['subscribed_users'].to_a.compact.map do |value|
user = User.find(value)
link_to_new_window(user.full_name, user_path(user))
end.join(', ').html_safe
end
end

# Transforms any address attributes into a display format
# --------------------------------------------------------
def merge_address_attributes!(attributes, entity)
attributes.each do |key, value|
if key =~ /_address/ and attributes[key]
address_type = key.gsub('_address', '')
attributes[key] = render("shared/address_show", :asset => entity, :type => address_type)
end
end
end

end
7 changes: 4 additions & 3 deletions app/views/accounts/_into.html.haml
Expand Up @@ -2,8 +2,8 @@

%div{:id => "account_#{@duplicate.id}"}

- duplicate_attributes = contact_merge_attributes(@duplicate)
- master_attributes = contact_merge_attributes(@master)
- duplicate_attributes = account_merge_attributes(@duplicate)
- master_attributes = account_merge_attributes(@master)
- default_merge = calculate_default_merge(duplicate_attributes, master_attributes)

.remote.merge_form
Expand All @@ -26,7 +26,8 @@
= "#{t(:duplicate_asset, :asset => t(:account))}: #{@duplicate.name}"
%th{ :class => 'merge_title', :width => "40%" }
= "#{t(:master_asset, :asset => t(:account))}: #{@master.name}"
- master_attributes.keys.each do |attribute|
- attributes_without_custom_fields = master_attributes.keys.reject{|k| k =~ /^cf_/}
- attributes_without_custom_fields.each do |attribute|
- duplicate_value, master_value = duplicate_attributes[attribute], master_attributes[attribute]
- unless duplicate_value.blank? and master_value.blank?
%tr
Expand Down
3 changes: 2 additions & 1 deletion app/views/contacts/_into.html.haml
Expand Up @@ -26,7 +26,8 @@
= "#{t(:duplicate_asset, :asset => t(:contact))}: #{@duplicate.name}"
%th{ :class => 'merge_title', :width => "40%" }
= "#{t(:master_asset, :asset => t(:contact))}: #{@master.name}"
- master_attributes.keys.each do |attribute|
- attributes_without_custom_fields = master_attributes.keys.reject{|k| k =~ /^cf_/}
- attributes_without_custom_fields.each do |attribute|
- duplicate_value, master_value = duplicate_attributes[attribute], master_attributes[attribute]
- unless duplicate_value.blank? and master_value.blank?
%tr
Expand Down
14 changes: 7 additions & 7 deletions app/views/merge/_custom_fields.html.haml
Expand Up @@ -10,22 +10,22 @@
= field_group.label
%p
Only fields that contain data are shown.
%table{ :cellpadding => "3px", :style => "text-align:left; border-collapse: collapse; border: none;" }
%table
%tr
%th
%th{ :valign => :top, :style => "font-size: 13px;", :width => "40%" }
%th{ :class => 'merge_title', :width => "40%" }
= "#{t(:duplicate_asset, :asset => t(:contact))}: #{@duplicate.name}"
%th{ :valign => :top, :style => "font-size: 13px;", :width => "40%" }
%th{ :class => 'merge_title', :width => "40%" }
= "#{t(:master_asset, :asset => t(:contact))}: #{@master.name}"
- duplicate_attributes.keys.each do |attribute|
- duplicate_value, master_value = duplicate_attributes[attribute], master_attributes[attribute]
- unless duplicate_value.blank? and master_value.blank?
%tr{ :style => "border-bottom:1px dotted silver;" }
%tr
%th{ :width => "20%" }
.label #{attribute.humanize}
.label #{t(attribute, :default => attribute.humanize, :scope => [:activerecord, :attributes, @master.class.to_s.underscore])}
%td
= ignore_merge_radio_button("no", attribute, default_merge[attribute]) unless duplicate_value.blank?
.label{:style => "display: inline;"} #{duplicate_value}
.label #{duplicate_value}
%td
= ignore_merge_radio_button("yes", attribute, default_merge[attribute]) unless master_value.blank?
.label{:style => "display: inline;"} #{master_value}
.label #{master_value}

0 comments on commit aece50c

Please sign in to comment.