From 9be859d42aea93e04ea8f520ee5258021abf3c05 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Thu, 9 Oct 2014 12:13:04 -0300 Subject: [PATCH] profile: refactor profile fields display process --- app/helpers/profile_helper.rb | 196 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------ app/views/profile/_common.html.erb | 50 ++++---------------------------------------------- app/views/profile/_organization_profile.html.erb | 39 +++------------------------------------ app/views/profile/_person_profile.html.erb | 41 ++++++----------------------------------- public/stylesheets/application.css | 5 +++-- 5 files changed, 188 insertions(+), 143 deletions(-) diff --git a/app/helpers/profile_helper.rb b/app/helpers/profile_helper.rb index b79ee9c..ee7d476 100644 --- a/app/helpers/profile_helper.rb +++ b/app/helpers/profile_helper.rb @@ -1,42 +1,190 @@ module ProfileHelper - def display_field(title, profile, field, force = false) + COMMON_CATEGORIES = ActiveSupport::OrderedHash.new + COMMON_CATEGORIES[:content] = [:blogs, :image_galleries, :events, :tags] + COMMON_CATEGORIES[:interests] = [:interests] + COMMON_CATEGORIES[:general] = nil + + PERSON_CATEGORIES = ActiveSupport::OrderedHash.new + PERSON_CATEGORIES[:basic_information] = [:nickname, :sex, :birth_date, :location, :privacy_setting, :created_at] + PERSON_CATEGORIES[:contact] = [:contact_phone, :cell_phone, :comercial_phone, :contact_information, :email, :personal_website, :jabber_id] + PERSON_CATEGORIES[:location] = [:address, :address_reference, :zip_code, :city, :state, :district, :country, :nationality] + PERSON_CATEGORIES[:work] = [:organization, :organization_website, :professional_activity] + PERSON_CATEGORIES[:study] = [:schooling, :formation, :area_of_study] + PERSON_CATEGORIES[:network] = [:friends, :communities, :enterprises] + PERSON_CATEGORIES.merge!(COMMON_CATEGORIES) + + ORGANIZATION_CATEGORIES = ActiveSupport::OrderedHash.new + ORGANIZATION_CATEGORIES[:basic_information] = [:display_name, :created_at, :foundation_year, :type, :language, :members_count, :location, :address_reference, :historic_and_current_context, :admins] + ORGANIZATION_CATEGORIES[:contact] = [:contact_person, :contact_phone, :contact_email, :organization_website, :jabber_id] + ORGANIZATION_CATEGORIES[:economic] = [:business_name, :acronym, :economic_activity, :legal_form, :products, :activities_short_description, :management_information] + ORGANIZATION_CATEGORIES.merge!(COMMON_CATEGORIES) + + CATEGORY_MAP = ActiveSupport::OrderedHash.new + CATEGORY_MAP[:person] = PERSON_CATEGORIES + CATEGORY_MAP[:organization] = ORGANIZATION_CATEGORIES + + FORCE = { + :person => [:privacy_setting], + :organization => [:privacy_setting, :location], + } + + MULTIPLE = { + :person => [:blogs, :image_galleries, :interests], + :organization => [:blogs, :image_galleries, :interests], + } + + CUSTOM_LABELS = { + :zip_code => _('ZIP code'), + :email => _('e-Mail'), + :jabber_id => _('Jabber'), + :birth_date => _('Date of birth'), + :created_at => _('Profile created at'), + :members_count => _('Members'), + } + + EXCEPTION = { + :person => [:image, :preferred_domain, :description, :tag_list], + :organization => [:image, :preferred_domain, :description, :tag_list, :address, :zip_code, :city, :state, :country, :district] + } + + def general_fields + categorized_fields = CATEGORY_MAP[kind].values.flatten + profile.class.fields.map(&:to_sym) - categorized_fields - EXCEPTION[kind] + end + + def kind + if profile.kind_of?(Person) + :person + else + :organization + end + end + + def title(field, entry = nil) + return self.send("#{field}_title", entry) if MULTIPLE[kind].include?(field) && entry.present? + CUSTOM_LABELS[field.to_sym] || field.to_s.humanize + end + + def display_field(field) + force = FORCE[kind].include?(field) + multiple = MULTIPLE[kind].include?(field) unless force || profile.may_display_field_to?(field, user) return '' end - value = profile.send(field) + value = begin profile.send(field) rescue nil end + p field if !value.blank? - if block_given? - value = yield(value) - end - content_tag('tr', content_tag('td', title, :class => 'field-name') + content_tag('td', value)) + entries = multiple ? value : [] << value + entries.map do |entry| + content = self.send("treat_#{field}", entry) + content_tag('tr', content_tag('td', title(field, entry), :class => 'field-name') + content_tag('td', content)) + end.join("\n") else '' end end - def display_contact(profile) - fields = [] - fields << display_field(_('Address:'), profile, :address).html_safe - fields << display_field(_('ZIP code:'), profile, :zip_code).html_safe - fields << display_field(_('Contact phone:'), profile, :contact_phone).html_safe - fields << display_field(_('e-Mail:'), profile, :email) { |email| link_to_email(email) }.html_safe - fields << display_field(_('Personal website:'), profile, :personal_website).html_safe - fields << display_field(_('Jabber:'), profile, :jabber_id).html_safe - if fields.reject!(&:blank?).empty? - '' - else - content_tag('tr', content_tag('th', _('Contact'), { :colspan => 2 })) + fields.join.html_safe + def treat_email(email) + link_to_email(email) + end + + def treat_organization_website(url) + link_to(url, url) + end + + def treat_sex(gender) + { 'male' => _('Male'), 'female' => _('Female') }[gender] + end + + def treat_date(date) + puts date.inspect + show_date(date.to_date) + end + alias :treat_birth_date :treat_date + alias :treat_created_at :treat_date + + def treat_friends(friends) + link_to friends.count, :controller => 'profile', :action => 'friends' + end + + def treat_communities(communities) + link_to communities.count, :controller => "profile", :action => 'communities' + end + + def treat_enterprises(enterprises) + if environment.disabled?('disable_asset_enterprises') + link_to enterprises.count, :controller => "profile", :action => 'enterprises' end end - def display_work_info(profile) - organization = display_field(_('Organization:'), profile, :organization) - organization_site = display_field(_('Organization website:'), profile, :organization_website) { |url| link_to(url, url) } - if organization.blank? && organization_site.blank? - '' + def treat_members_count(count) + link_to count, :controller => 'profile', :action => 'members' + end + + def treat_products(products) + if profile.kind_of?(Enterprise) && profile.environment.enabled?('products_for_enterprises') + link_to _('Products/Services'), :controller => 'catalog', :action => 'index' + end + end + + def treat_admins(admins) + profile.admins.map { |admin| link_to(admin.short_name, admin.url)}.join(', ') + end + + def treat_blogs(blog) + p blog + link_to(n_('One post', '%{num} posts', blog.posts.published.count) % { :num => blog.posts.published.count }, blog.url) + end + + def treat_image_galleries(gallery) + p gallery + link_to(n_('One picture', '%{num} pictures', gallery.images.published.count) % { :num => gallery.images.published.count }, gallery.url) + end + + def treat_events(events) + link_to events.published.count, :controller => 'events', :action => 'events' + end + + def treat_tags(tags) + tag_cloud @tags, :id, { :action => 'tags' }, :max_size => 18, :min_size => 10 + end + + def treat_interests(interest) + link_to interest.name, :controller => 'search', :action => 'category_index', :category_path => interest.explode_path + end + + def article_title(article) + article.name + end + alias :blogs_title :article_title + alias :image_galleries_title :article_title + + def interests_title(interest) + '' + end + + def method_missing(method, *args, &block) + if method.to_s =~ /^treat_(.+)$/ + args[0] + elsif method.to_s =~ /^display_(.+)$/ && CATEGORY_MAP[kind].has_key?($1.to_sym) + category = $1.to_sym + fields = category == :general ? general_fields : CATEGORY_MAP[kind][category] + contents = [] + + fields.each do |field| + contents << display_field(field).html_safe + end + + contents = contents.delete_if(&:blank?) + + unless contents.empty? + content_tag('tr', content_tag('th', title(category), { :colspan => 2 })) + contents.join.html_safe + else + '' + end else - content_tag('tr', content_tag('th', _('Work'), { :colspan => 2 })) + organization + organization_site + super end end diff --git a/app/views/profile/_common.html.erb b/app/views/profile/_common.html.erb index 54efeca..87b988d 100644 --- a/app/views/profile/_common.html.erb +++ b/app/views/profile/_common.html.erb @@ -1,53 +1,11 @@ <% unless @action %> <% cache_timeout(profile.cache_key + '-profile-general-info', 4.hours) do %> - - - <%= _('Content') %> - - - - <% profile.blogs.each do |blog| %> - - <%= blog.name + ':' %> - - <%= link_to(n_('One post', '%{num} posts', blog.posts.published.count) % { :num => blog.posts.published.count }, blog.url) %> - - - <% end %> - <% profile.image_galleries.each do |gallery| %> - - <%= gallery.name + ':' %> - - <%= link_to(n_('One picture', '%{num} pictures', gallery.images.published.count) % { :num => gallery.images.published.count }, gallery.url) %> - - - <% end %> - - - <%= _('Events:') %> - - <%= link_to profile.events.published.count, :controller => 'events', :action => 'events' %> - - - - - <%= _('Tags:') %> - - - <%= tag_cloud @tags, :id, { :action => 'tags' }, :max_size => 18, :min_size => 10%> - - + <%= display_content %> <% if !environment.enabled?('disable_categories') && !profile.interests.empty? %> - - <%= _('Interests') %> - - <% profile.interests.each do |item| %> - - - <%= link_to item.name, :controller => 'search', :action => 'category_index', :category_path => item.explode_path %> - - <% end %> + <%= display_interests %> <% end %> + + <%= display_general %> <% end %> <% end %> diff --git a/app/views/profile/_organization_profile.html.erb b/app/views/profile/_organization_profile.html.erb index 04de939..872abff 100644 --- a/app/views/profile/_organization_profile.html.erb +++ b/app/views/profile/_organization_profile.html.erb @@ -1,39 +1,6 @@ - - - - - - - - - - <%= display_field(_('Type:'), profile, :privacy_setting, true) %> - - <%= display_field(_('Location:'), profile, :location, true) %> - - - - - - - <% if profile.kind_of?(Enterprise) && profile.environment.enabled?('products_for_enterprises') %> - - - - - <% end %> - - - - - - + <%= display_basic_information %> + <%= display_contact %> + <%= display_economic %> <%= render :partial => 'common' %>
<%= _('Basic information')%>
<%= _('Members') %> - <%= link_to profile.members_count, :controller => 'profile', :action => 'members' %> -
<%= _('Profile created at:') %><%= show_date(profile.created_at) %>
- <%= link_to _('Products/Services'), :controller => 'catalog', :action => 'index' %> -
<%= _('Administrators:') %> - <%= profile.admins.map { |admin| link_to(admin.short_name, admin.url)}.join(', ') %> -
diff --git a/app/views/profile/_person_profile.html.erb b/app/views/profile/_person_profile.html.erb index 981b329..4048420 100644 --- a/app/views/profile/_person_profile.html.erb +++ b/app/views/profile/_person_profile.html.erb @@ -1,43 +1,14 @@ - - - - <%= display_field(_('Sex:'), profile, :sex) { |gender| { 'male' => _('Male'), 'female' => _('Female') }[gender] } %> - <%= display_field(_('Date of birth:'), profile, :birth_date) { |date| show_date(date) }%> - <%= display_field _('Location:'), profile, :location %> - - <%= display_field(_('Type:'), profile, :privacy_setting, true) %> - - - - - - - <%= display_contact profile %> + <%= display_basic_information %> + <%= display_contact %> + <%= display_location %> <% cache_timeout(profile.relationships_cache_key, 4.hours) do %> - <%= display_work_info profile %> - - - - - - - - - - - - - <% if environment.disabled?('disable_asset_enterprises') %> - - - - - <% end %> + <%= display_work %> + <%= display_study %> + <%= display_network %> <%= render :partial => 'common' %> - <% end %>
<%= _('Basic information')%>
<%= _('Profile created at:') %><%= show_date(profile.created_at) %>
<%= _('Network')%>
<%= _('Friends') + ':' %><%= link_to profile.friends.count, { :controller => 'profile', :action => 'friends' } %>
<%= _('Communities') + ':' %><%= link_to profile.communities.count, :controller => "profile", :action => 'communities' %>
<%= _('Enterprises') + ':' %><%= link_to profile.enterprises.count, :controller => "profile", :action => 'enterprises' %>
diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index 3e86a37..bc6913a 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -242,10 +242,11 @@ table.noborder:hover td.selected, td.selected { td .button { background-color: transparent; } -td.field-name { +table.profile td.field-name { width: 150px; text-align: left; - vertical-align: top; + vertical-align: middle; + padding-right: 5px; } table.profile .ui-tabs .ui-tabs-panel { -- libgit2 0.21.2