Commit 8b57c5f8e53f50ffac990765e25308db812cd5a7

Authored by Fabio Teixeira
Committed by David Silva
1 parent ade78acd

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