diff --git a/app/controllers/public/profile_controller.rb b/app/controllers/public/profile_controller.rb index 81b3078..b775f7a 100644 --- a/app/controllers/public/profile_controller.rb +++ b/app/controllers/public/profile_controller.rb @@ -49,36 +49,36 @@ class ProfileController < PublicController def communities if is_cache_expired?(profile.communities_cache_key(params)) - @communities = profile.communities.paginate(:per_page => per_page, :page => params[:npage]) + @communities = profile.communities.includes(relations_to_include).paginate(:per_page => per_page, :page => params[:npage]) end end def enterprises - @enterprises = profile.enterprises + @enterprises = profile.enterprises.includes(relations_to_include) end def friends if is_cache_expired?(profile.friends_cache_key(params)) - @friends = profile.friends.paginate(:per_page => per_page, :page => params[:npage]) + @friends = profile.friends.includes(relations_to_include).paginate(:per_page => per_page, :page => params[:npage]) end end def members if is_cache_expired?(profile.members_cache_key(params)) - @members = profile.members.paginate(:per_page => members_per_page, :page => params[:npage]) + @members = profile.members.includes(relations_to_include).paginate(:per_page => members_per_page, :page => params[:npage]) end end def fans - @fans = profile.fans + @fans = profile.fans.includes(relations_to_include) end def favorite_enterprises - @favorite_enterprises = profile.favorite_enterprises + @favorite_enterprises = profile.favorite_enterprises.includes(relations_to_include) end def sitemap - @articles = profile.top_level_articles + @articles = profile.top_level_articles.includes([:profile, :parent]) end def join @@ -246,7 +246,7 @@ class ProfileController < PublicController def profile_info begin - @block = profile.blocks.find(params[:block_id]) + @block = profile.blocks.find(params[:block_id]).includes(:box) rescue render :text => _('Profile information could not be loaded') end @@ -359,4 +359,8 @@ class ProfileController < PublicController @can_edit_profile ||= user && user.has_permission?('edit_profile', profile) end helper_method :can_edit_profile + + def relations_to_include + [:image, :domains, :preferred_domain, :environment] + end end diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index 89a7ff9..689bf65 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -46,7 +46,7 @@ class SearchController < PublicController if !@empty_query full_text_search ['public:true'] else - @results[@asset] = @environment.people.visible.send(@filter).paginate(paginate_options) + @results[@asset] = visible_profiles(Person).send(@filter).paginate(paginate_options) end end @@ -76,7 +76,7 @@ class SearchController < PublicController full_text_search ['public:true'] else @filter_title = _('Enterprises from network') - @results[@asset] = @environment.enterprises.visible.paginate(paginate_options) + @results[@asset] = visible_profiles(Enterprise, [{:products => :product_category}]).paginate(paginate_options) end end @@ -84,7 +84,7 @@ class SearchController < PublicController if !@empty_query full_text_search ['public:true'] else - @results[@asset] = @environment.communities.visible.send(@filter).paginate(paginate_options) + @results[@asset] = visible_profiles(Community).send(@filter).paginate(paginate_options) end end @@ -310,4 +310,12 @@ class SearchController < PublicController @all_facets = ret[:all_facets] end + private + + def visible_profiles(klass, *extra_relations) + relations = [:image, :domains, :environment, :preferred_domain] + relations += extra_relations + @environment.send(klass.name.underscore.pluralize).visible.includes(relations) + end + end diff --git a/app/helpers/boxes_helper.rb b/app/helpers/boxes_helper.rb index a6b64ee..3ca6ce3 100644 --- a/app/helpers/boxes_helper.rb +++ b/app/helpers/boxes_helper.rb @@ -66,7 +66,7 @@ module BoxesHelper def display_box_content(box, main_content) context = { :article => @page, :request_path => request.path, :locale => locale } - box_decorator.select_blocks(box.blocks, context).map { |item| display_block(item, main_content) }.join("\n") + box_decorator.block_target(box) + box_decorator.select_blocks(box.blocks.includes(:box), context).map { |item| display_block(item, main_content) }.join("\n") + box_decorator.block_target(box) end def select_blocks(arr, context) diff --git a/app/models/article.rb b/app/models/article.rb index a99defd..64bad14 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -179,37 +179,23 @@ class Article < ActiveRecord::Base end named_scope :more_popular, :order => 'hits DESC' - - # retrieves the latest +limit+ articles, sorted from the most recent to the - # oldest. - # - # Only includes articles where advertise == true - def self.recent(limit = nil, extra_conditions = {}) - # FIXME this method is a horrible hack - options = { :page => 1, :per_page => limit, - :conditions => [ - "advertise = ? AND - published = ? AND - profiles.visible = ? AND - profiles.public_profile = ? AND - ((articles.type != ? and articles.type != ? and articles.type != ?) OR articles.type is NULL)", true, true, true, true, 'UploadedFile', 'RssFeed', 'Blog' - ], - :include => 'profile', - :order => 'articles.published_at desc, articles.id desc' - } - if ( scoped_methods && scoped_methods.last && - scoped_methods.last[:find] && - scoped_methods.last[:find][:joins] && - scoped_methods.last[:find][:joins].index('profiles') ) - options.delete(:include) - end - if extra_conditions == {} - self.paginate(options) - else - with_scope :find => {:conditions => extra_conditions} do - self.paginate(options) - end + named_scope :relevant_as_recent, :conditions => ["(articles.type != 'UploadedFile' and articles.type != 'RssFeed' and articles.type != 'Blog') OR articles.type is NULL"] + + def self.recent(limit = nil, extra_conditions = {}, pagination = true) + result = scoped({:conditions => extra_conditions}). + public. + relevant_as_recent. + limit(limit). + order(['articles.published_at desc', 'articles.id desc']) + + if !( scoped_methods && scoped_methods.last && + scoped_methods.last[:find] && + scoped_methods.last[:find][:joins] && + scoped_methods.last[:find][:joins].index('profiles') ) + result.includes(:profile) end + + pagination ? result.paginate({:page => 1, :per_page => limit}) : result end # produces the HTML code that is to be displayed as this article's contents. diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 52bd36e..5001a69 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -17,7 +17,7 @@ class Enterprise < Organization after_save_reindex [:products], :with => :delayed_job extra_data_for_index :product_categories def product_categories - products.map{|p| p.category_full_name}.compact + products.includes(:product_category).map{|p| p.category_full_name}.compact end N_('Organization website'); N_('Historic and current context'); N_('Activities short description'); N_('City'); N_('State'); N_('Country'); N_('ZIP code') diff --git a/app/models/environment.rb b/app/models/environment.rb index 205fe00..aa92b52 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -591,8 +591,8 @@ class Environment < ActiveRecord::Base end has_many :articles, :through => :profiles - def recent_documents(limit = 10) - self.articles.recent(limit) + def recent_documents(limit = 10, options = {}, pagination = true) + self.articles.recent(limit, options, pagination) end has_many :events, :through => :profiles, :source => :articles, :class_name => 'Event' diff --git a/app/models/person.rb b/app/models/person.rb index 378ad3a..60c14a8 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -251,7 +251,7 @@ class Person < Profile def is_admin?(environment = nil) environment ||= self.environment - role_assignments.select { |ra| ra.resource == environment }.map{|ra|ra.role.permissions}.any? do |ps| + role_assignments.includes([:role, :resource]).select { |ra| ra.resource == environment }.map{|ra|ra.role.permissions}.any? do |ps| ps.any? do |p| ActiveRecord::Base::PERMISSIONS['Environment'].keys.include?(p) end diff --git a/app/models/profile.rb b/app/models/profile.rb index b0bd67d..c36ea3e 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -398,8 +398,8 @@ class Profile < ActiveRecord::Base # # +limit+ is the maximum number of documents to be returned. It defaults to # 10. - def recent_documents(limit = 10, options = {}) - self.articles.recent(limit, options) + def recent_documents(limit = 10, options = {}, pagination = true) + self.articles.recent(limit, options, pagination) end def last_articles(limit = 10, options = {}) diff --git a/app/models/profile_list_block.rb b/app/models/profile_list_block.rb index 75fa66c..ad1371e 100644 --- a/app/models/profile_list_block.rb +++ b/app/models/profile_list_block.rb @@ -14,12 +14,13 @@ class ProfileListBlock < Block def profile_list result = nil + visible_profiles = profiles.visible.includes([:image,:domains,:preferred_domain,:environment]) if !prioritize_profiles_with_image - result = profiles.visible.all(:limit => limit, :order => 'updated_at DESC').sort_by{ rand } - elsif profiles.visible.with_image.count >= limit - result = profiles.visible.with_image.all(:limit => limit * 5, :order => 'updated_at DESC').sort_by{ rand } + result = visible_profiles.all(:limit => limit, :order => 'updated_at DESC').sort_by{ rand } + elsif visible_profiles.with_image.count >= limit + result = visible_profiles.with_image.all(:limit => limit * 5, :order => 'updated_at DESC').sort_by{ rand } else - result = profiles.visible.with_image.sort_by{ rand } + profiles.visible.without_image.all(:limit => limit * 5, :order => 'updated_at DESC').sort_by{ rand } + result = visible_profiles.with_image.sort_by{ rand } + visible_profiles.without_image.all(:limit => limit * 5, :order => 'updated_at DESC').sort_by{ rand } end result.slice(0..limit-1) end diff --git a/app/models/recent_documents_block.rb b/app/models/recent_documents_block.rb index 6173fac..42a99a8 100644 --- a/app/models/recent_documents_block.rb +++ b/app/models/recent_documents_block.rb @@ -16,11 +16,9 @@ class RecentDocumentsBlock < Block include ActionController::UrlWriter def content(args={}) - docs = self.limit.nil? ? owner.recent_documents : owner.recent_documents(self.limit) - + docs = self.limit.nil? ? owner.recent_documents(nil, {}, false) : owner.recent_documents(self.limit, {}, false) block_title(title) + content_tag('ul', docs.map {|item| content_tag('li', link_to(h(item.title), item.url))}.join("\n")) - end def footer diff --git a/app/views/profile/_person_profile.rhtml b/app/views/profile/_person_profile.rhtml index 6c95cc7..02191a8 100644 --- a/app/views/profile/_person_profile.rhtml +++ b/app/views/profile/_person_profile.rhtml @@ -37,7 +37,7 @@ <%= __('Enterprises') %> - <% profile.enterprises.each do |item| %> + <% profile.enterprises.includes(:environment,:domains, :preferred_domain).each do |item| %> <%= button 'menu-enterprise', item.name, item.url %> diff --git a/lib/acts_as_having_boxes.rb b/lib/acts_as_having_boxes.rb index 48eeebe..95330e6 100644 --- a/lib/acts_as_having_boxes.rb +++ b/lib/acts_as_having_boxes.rb @@ -18,7 +18,7 @@ module ActsAsHavingBoxes @blocks = nil end if @blocks.nil? - @blocks = boxes.inject([]) do |acc,obj| + @blocks = boxes.includes(:blocks).inject([]) do |acc,obj| acc.concat(obj.blocks) end @blocks.send(:extend, BlockArray) diff --git a/vendor/plugins/access_control/lib/acts_as_accessor.rb b/vendor/plugins/access_control/lib/acts_as_accessor.rb index f252352..e6b5667 100644 --- a/vendor/plugins/access_control/lib/acts_as_accessor.rb +++ b/vendor/plugins/access_control/lib/acts_as_accessor.rb @@ -4,7 +4,7 @@ class ActiveRecord::Base def has_permission?(permission, resource = nil) return true if resource == self - role_assignments.any? {|ra| ra.has_permission?(permission, resource)} + role_assignments.includes([:resource,:role]).any? {|ra| ra.has_permission?(permission, resource)} end def define_roles(roles, resource) -- libgit2 0.21.2