Commit 8b57c5f8e53f50ffac990765e25308db812cd5a7

Authored by Fabio Teixeira
Committed by David Silva
1 parent ade78acd
Exists in master and in 79 other branches add_sisp_to_chef, add_super_archives_plugin, api_for_colab, automates_core_packing, backup_not_prod, changes_in_buttons_on_content_panel, colab_automated_login, colab_spb_plugin_recipe, colab_widgets_settings, design_validation, dev_env_minimal, disable_email_dev, fix_breadcrumbs_position, fix_categories_software_link, fix_edit_institution, fix_edit_software_with_another_license, fix_get_license_info, fix_gitlab_assets_permission, fix_list_style_inside_article, fix_list_style_on_folder_elements, fix_members_pagination, fix_merge_request_url, fix_models_translations, fix_no_license, fix_software_api, fix_software_block_migration, fix_software_communities_translations, fix_software_communities_unit_test, fix_style_create_institution_admin_panel, fix_superarchives_imports, fix_sym_links_noosfero, focus_search_field_theme, gov-user-refactoring, gov-user-refactoring-rails4, header_fix, institution_modal_on_rating, kalibro-conf-refactoring, kalibro-processor-package, lxc_settings, margin_fix, mezuro_cookbook, prezento, refactor_download_block, refactor_software_communities, refactor_software_for_sisp, register_page, release-process, release-process-v2, remove-unused-images, remove_broken_theme, remove_secondary_email_from_user, remove_sisp_buttons, removing_super_archives_email, review_message, scope2method, signals_user_noosfero, sisp_catalog_header, sisp_colab_config, sisp_dev, sisp_dev_master, sisp_simple_version, software_as_organization, software_catalog_style_fix, software_communities_html_refactor, software_infos_api, spb_minimal_env, spb_to_rails4, spec_refactor, stable-4.1, stable-4.2, stable-4.x, temp_soft_comm_refactoring, theme_header, theme_javascript_refactory, thread_dropdown, thread_page, update_search_by_categories, update_software_api, update_softwares_boxes

Refactory softwares_block

Signed-off-by: Fabio Teixeira <fabio1079@gmail.com>
Showing 1 changed file with 42 additions and 27 deletions   Show diff stats
lib/softwares_block.rb
1 class SoftwaresBlock < CommunitiesBlock 1 class SoftwaresBlock < CommunitiesBlock
2 2
3 settings_items :software_type, :default => "All" 3 settings_items :software_type, :default => "All"
4 - attr_accessible :accessor_id, :accessor_type, :role_id, :resource_id, :resource_type, :software_type 4 + attr_accessible :accessor_id, :accessor_type, :role_id,
  5 + :resource_id, :resource_type, :software_type
5 6
6 def self.description 7 def self.description
7 _('Softwares') 8 _('Softwares')
@@ -27,11 +28,14 @@ class SoftwaresBlock &lt; CommunitiesBlock @@ -27,11 +28,14 @@ class SoftwaresBlock &lt; CommunitiesBlock
27 case owner 28 case owner
28 when Profile 29 when Profile
29 lambda do |context| 30 lambda do |context|
30 - link_to s_('softwares|View all'), :profile => owner.identifier, :controller => 'profile', :action => 'communities', :type => 'Software' 31 + link_to s_('softwares|View all'), :profile => owner.identifier,
  32 + :controller => 'profile', :action => 'communities',
  33 + :type => 'Software'
31 end 34 end
32 when Environment 35 when Environment
33 lambda do |context| 36 lambda do |context|
34 - link_to s_('softwares|View all'), :controller => 'search', :action => 'software_infos' 37 + link_to s_('softwares|View all'), :controller => 'search',
  38 + :action => 'software_infos'
35 end 39 end
36 else 40 else
37 '' 41 ''
@@ -47,33 +51,21 @@ class SoftwaresBlock &lt; CommunitiesBlock @@ -47,33 +51,21 @@ class SoftwaresBlock &lt; CommunitiesBlock
47 end 51 end
48 52
49 def profile_list 53 def profile_list
50 - result = nil  
51 - visible_profiles = profiles.visible.includes([:image,:domains,:preferred_domain,:environment])  
52 - if !prioritize_profiles_with_image  
53 - result = visible_profiles.all(:limit => get_limit, :order => 'profiles.updated_at DESC').sort_by{ rand }  
54 - elsif profiles.visible.with_image.count >= get_limit  
55 - result = visible_profiles.with_image.all(:limit => get_limit * 5, :order => 'profiles.updated_at DESC').sort_by{ rand }  
56 - else  
57 - result = visible_profiles.with_image.sort_by{ rand } + visible_profiles.without_image.all(:limit => get_limit * 5, :order => 'profiles.updated_at DESC').sort_by{ rand }  
58 - end 54 + profiles = get_visible_profiles
59 55
60 - list_with_software = []  
61 -  
62 - result.each do |profile|  
63 - if profile.class == Community and profile.software?  
64 - if self.software_type == "Public"  
65 - list_with_software << profile if profile.software_info.public_software?  
66 - elsif self.software_type == "Generic"  
67 - list_with_software << profile if !profile.software_info.public_software?  
68 - else  
69 - list_with_software << profile  
70 - end  
71 - end 56 + software_profiles = profiles.select do |profile|
  57 + profile.class == Community && profile.software?
72 end 58 end
73 59
74 - result = list_with_software 60 + block_softwares = if self.software_type == "Public"
  61 + software_profiles.select { |profile| profile.software_info.public_software? }
  62 + elsif self.software_type == "Generic"
  63 + software_profiles.select { |profile| !profile.software_info.public_software? }
  64 + else # All
  65 + software_profiles
  66 + end
75 67
76 - result.slice(0..get_limit-1) 68 + block_softwares.slice(0..get_limit-1)
77 end 69 end
78 70
79 def content(arg={}) 71 def content(arg={})
@@ -81,10 +73,33 @@ class SoftwaresBlock &lt; CommunitiesBlock @@ -81,10 +73,33 @@ class SoftwaresBlock &lt; CommunitiesBlock
81 block = self 73 block = self
82 74
83 proc do 75 proc do
84 - render :file => 'blocks/main_area_softwares', :locals => { :profiles=> block.profile_list(), :block => block } 76 + render :file => 'blocks/main_area_softwares',
  77 + :locals => {:profiles=> block.profile_list(), :block => block}
85 end 78 end
86 else 79 else
87 super(arg) 80 super(arg)
88 end 81 end
89 end 82 end
  83 +
  84 + protected
  85 +
  86 + def get_visible_profiles
  87 + profile_include_list = [:image, :domains, :preferred_domain, :environment]
  88 + visible_profiles = profiles.visible.includes(profile_include_list)
  89 +
  90 + if !prioritize_profiles_with_image
  91 + visible_profiles.all( :limit => get_limit,
  92 + :order => 'profiles.updated_at DESC'
  93 + ).sort_by{ rand }
  94 + elsif profiles.visible.with_image.count >= get_limit
  95 + visible_profiles.with_image.all( :limit => get_limit * 5,
  96 + :order => 'profiles.updated_at DESC'
  97 + ).sort_by{ rand }
  98 + else
  99 + visible_profiles.with_image.sort_by{ rand } +
  100 + visible_profiles.without_image.all( :limit => get_limit * 5,
  101 + :order => 'profiles.updated_at DESC'
  102 + ).sort_by{ rand }
  103 + end
  104 + end
90 end 105 end