From a1dad6bab9a848feb20898bf3026a8a00e8ff14f Mon Sep 17 00:00:00 2001 From: Braulio Bhavamitra Date: Fri, 3 Apr 2015 16:35:23 -0300 Subject: [PATCH] rails4: replace :conditions by where --- app/controllers/admin/admin_panel_controller.rb | 2 +- app/controllers/admin/categories_controller.rb | 8 ++++---- app/controllers/admin/environment_role_manager_controller.rb | 2 +- app/controllers/admin/features_controller.rb | 4 ++-- app/controllers/admin/users_controller.rb | 2 +- app/controllers/box_organizer_controller.rb | 4 ++-- app/controllers/my_profile/cms_controller.rb | 15 ++++----------- app/controllers/my_profile/profile_members_controller.rb | 4 ++-- app/controllers/my_profile/tasks_controller.rb | 2 +- app/controllers/public/profile_controller.rb | 8 ++++---- app/mailers/environment_mailing.rb | 4 +++- app/mailers/organization_mailing.rb | 4 +++- app/models/article.rb | 73 +++++++++++++++++++++++++++++++++++++------------------------------------ app/models/block.rb | 2 +- app/models/blog_archives_block.rb | 2 +- app/models/box.rb | 2 +- app/models/categorization.rb | 4 ++-- app/models/category.rb | 24 ++++++++++++------------ app/models/comment.rb | 2 +- app/models/domain.rb | 2 +- app/models/enterprise.rb | 2 +- app/models/environment.rb | 14 ++++++++------ app/models/event.rb | 31 +++++++++++++------------------ app/models/external_feed.rb | 6 +++--- app/models/feed_reader_block.rb | 4 ++-- app/models/folder.rb | 9 +++++---- app/models/input.rb | 2 +- app/models/invite_friend.rb | 2 +- app/models/invite_member.rb | 2 +- app/models/organization.rb | 6 +++--- app/models/person.rb | 57 ++++++++++++++++++++++++++++++++++----------------------- app/models/person_notifier.rb | 2 +- app/models/product.rb | 4 ++-- app/models/product_category.rb | 22 +++++++++++----------- app/models/profile.rb | 53 +++++++++++++++++++++++++++++------------------------ app/models/profile_categorization.rb | 2 +- app/models/profile_suggestion.rb | 6 +++--- app/models/rss_feed.rb | 2 +- app/models/scrap.rb | 4 ++-- app/models/search_term_occurrence.rb | 4 +++- app/models/slideshow_block.rb | 2 +- app/models/task.rb | 27 ++++++++++++++------------- app/models/user.rb | 4 ++-- app/views/features/index.html.erb | 2 +- app/views/search/_full_forum.html.erb | 2 +- app/views/search/_image.html.erb | 2 +- config/initializers/activities_counter_cache.rb | 2 +- db/migrate/059_add_birth_date_to_person.rb | 2 +- lib/acts_as_filesystem.rb | 10 +++++----- lib/acts_as_having_image.rb | 6 +++--- lib/extended_tag.rb | 16 ++++++++-------- lib/spammable.rb | 6 +++--- plugins/bsc/controllers/bsc_plugin_admin_controller.rb | 5 +++-- plugins/bsc/controllers/bsc_plugin_myprofile_controller.rb | 8 +++++--- plugins/bsc/lib/bsc_plugin/contract.rb | 4 ++-- plugins/bsc/lib/bsc_plugin/ext/enterprise.rb | 4 ++-- plugins/comment_classification/lib/comment_classification_plugin/label.rb | 2 +- plugins/comment_classification/lib/comment_classification_plugin/status.rb | 2 +- plugins/comment_group/lib/ext/article.rb | 6 ++++-- plugins/comment_group/lib/ext/comment.rb | 7 ++----- plugins/community_track/lib/community_track_plugin/step.rb | 4 ++-- plugins/custom_forms/lib/custom_forms_plugin/form.rb | 6 +++--- plugins/custom_forms/lib/custom_forms_plugin/membership_survey.rb | 2 +- plugins/display_content/lib/display_content_block.rb | 2 +- plugins/mark_comment_as_read/lib/mark_comment_as_read_plugin/ext/comment.rb | 4 ++-- plugins/oauth_client/lib/oauth_client_plugin/provider.rb | 2 +- plugins/people_block/lib/ext/person.rb | 5 +++-- plugins/recent_content/lib/recent_content_block.rb | 9 ++++----- plugins/relevant_content/lib/ext/article.rb | 95 +++++++++++++++++++++++++++++++++-------------------------------------------------------------- plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb | 2 +- plugins/spaminator/lib/spaminator_plugin/report.rb | 2 +- plugins/statistics/lib/statistics_block.rb | 2 +- plugins/stoa/lib/ext/person.rb | 4 ++-- plugins/stoa/lib/stoa_plugin.rb | 2 +- plugins/sub_organizations/controllers/sub_organizations_plugin_myprofile_controller.rb | 9 +++++---- plugins/sub_organizations/lib/ext/organization.rb | 26 ++++++++++---------------- plugins/work_assignment/controllers/work_assignment_plugin_myprofile_controller.rb | 4 ++-- script/apply-template | 6 +++--- script/sample-enterprises | 2 +- script/sample-profiles | 2 +- test/unit/article_categorization_test.rb | 2 +- test/unit/person_test.rb | 8 ++++---- test/unit/profile_test.rb | 2 +- test/unit/suggest_article_test.rb | 4 ++-- test/unit/uploaded_file_test.rb | 10 +++++----- vendor/plugins/access_control/lib/acts_as_accessor.rb | 12 ++++++------ vendor/plugins/access_control/lib/role_assignment.rb | 14 +++++++++----- vendor/plugins/action_tracker/lib/action_tracker.rb | 2 +- vendor/plugins/action_tracker/lib/action_tracker_model.rb | 10 +++++----- vendor/plugins/acts_as_list/lib/active_record/acts/list.rb | 10 +++------- vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb | 14 +++++++------- 91 files changed, 392 insertions(+), 409 deletions(-) diff --git a/app/controllers/admin/admin_panel_controller.rb b/app/controllers/admin/admin_panel_controller.rb index 6c2b103..9ecbe6d 100644 --- a/app/controllers/admin/admin_panel_controller.rb +++ b/app/controllers/admin/admin_panel_controller.rb @@ -54,7 +54,7 @@ class AdminPanelController < AdminController if request.post? env = environment - folders = params[:folders].map{|fid| Folder.find(:first, :conditions => {:profile_id => env.portal_community, :id => fid})} if params[:folders] + folders = Folder.where :profile_id => env.portal_community, :id => params[:folders] if params[:folders] env.portal_folders = folders if env.save session[:notice] = _('Saved the portal folders') diff --git a/app/controllers/admin/categories_controller.rb b/app/controllers/admin/categories_controller.rb index 9e24e07..0907c5d 100644 --- a/app/controllers/admin/categories_controller.rb +++ b/app/controllers/admin/categories_controller.rb @@ -1,13 +1,13 @@ class CategoriesController < AdminController protect 'manage_environment_categories', :environment - + helper :categories def index - @categories = environment.categories.find(:all, :conditions => "parent_id is null AND type is null") - @regions = environment.regions.find(:all, :conditions => {:parent_id => nil}) - @product_categories = environment.product_categories.find(:all, :conditions => {:parent_id => nil}) + @categories = environment.categories.where("parent_id is null AND type is null") + @regions = environment.regions.where(:parent_id => nil) + @product_categories = environment.product_categories.where(:parent_id => nil) end def get_children diff --git a/app/controllers/admin/environment_role_manager_controller.rb b/app/controllers/admin/environment_role_manager_controller.rb index fef08a8..67822e5 100644 --- a/app/controllers/admin/environment_role_manager_controller.rb +++ b/app/controllers/admin/environment_role_manager_controller.rb @@ -2,7 +2,7 @@ class EnvironmentRoleManagerController < AdminController protect 'manage_environment_roles', :environment def index - @admins = Person.find(:all, :conditions => ['role_assignments.resource_type = ?', 'Environment'], :include => :role_assignments ) + @admins = Person.where('role_assignments.resource_type = ?', 'Environment').includes(:role_assignments) end def change_roles diff --git a/app/controllers/admin/features_controller.rb b/app/controllers/admin/features_controller.rb index 4bbe006..4c2afc0 100644 --- a/app/controllers/admin/features_controller.rb +++ b/app/controllers/admin/features_controller.rb @@ -1,6 +1,6 @@ class FeaturesController < AdminController protect 'edit_environment_features', :environment - + def index @features = Environment.available_features.sort_by{|k,v|v} end @@ -53,7 +53,7 @@ class FeaturesController < AdminController def search_members arg = params[:q].downcase - result = environment.people.find(:all, :conditions => ['LOWER(name) LIKE ? OR identifier LIKE ?', "%#{arg}%", "%#{arg}%"]) + result = environment.people.where('LOWER(name) LIKE ? OR identifier LIKE ?', "%#{arg}%", "%#{arg}%") render :text => prepare_to_token_input(result).to_json end diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index b827994..8e1e47c 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -63,7 +63,7 @@ class UsersController < AdminController respond_to do |format| format.html format.xml do - users = User.find(:all, :conditions => {:environment_id => environment.id}, :include => [:person]) + users = User.where(:environment_id => environment.id).includes(:person) send_data users.to_xml( :skip_types => true, :only => %w[email login created_at updated_at], diff --git a/app/controllers/box_organizer_controller.rb b/app/controllers/box_organizer_controller.rb index eb13d5d..772e18d 100644 --- a/app/controllers/box_organizer_controller.rb +++ b/app/controllers/box_organizer_controller.rb @@ -84,9 +84,9 @@ class BoxOrganizerController < ApplicationController if request.xhr? and params[:query] search = params[:query] path_list = if boxes_holder.is_a?(Environment) && boxes_holder.enabled?('use_portal_community') && boxes_holder.portal_community - boxes_holder.portal_community.articles.find(:all, :conditions=>"name ILIKE '%#{search}%' or path ILIKE '%#{search}%'", :limit=>20).map { |content| "/{portal}/"+content.path } + boxes_holder.portal_community.articles.where("name ILIKE ? OR path ILIKE ?", "%#{search}%", "%#{search}%").limit(20).map { |content| "/{portal}/"+content.path } elsif boxes_holder.is_a?(Profile) - boxes_holder.articles.find(:all, :conditions=>"name ILIKE '%#{search}%' or path ILIKE '%#{search}%'", :limit=>20).map { |content| "/{profile}/"+content.path } + boxes_holder.articles.where("name ILIKE ? OR path ILIKE ?", "%#{search}%", "%#{search}%").limit(20).map { |content| "/{profile}/"+content.path } else [] end diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index 9ff7a92..7288e07 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -57,16 +57,9 @@ class CmsController < MyProfileController def view @article = profile.articles.find(params[:id]) - conditions = [] - if @article.has_posts? - conditions = ['type != ?', 'RssFeed'] - end - - @articles = @article.children.reorder("case when type = 'Folder' then 0 when type ='Blog' then 1 else 2 end, updated_at DESC, name").paginate( - :conditions => conditions, - :per_page => per_page, - :page => params[:npage] - ) + @articles = @article.children.reorder("case when type = 'Folder' then 0 when type ='Blog' then 1 else 2 end, updated_at DESC, name") + @articles = @articles.where type: 'RssFeed' if @article.has_posts? + @articles = @articles.paginate per_page: per_page, page: params[:npage] end def index @@ -390,7 +383,7 @@ class CmsController < MyProfileController def search_article_privacy_exceptions arg = params[:q].downcase - result = profile.members.find(:all, :conditions => ['LOWER(name) LIKE ?', "%#{arg}%"]) + result = profile.members.where('LOWER(name) LIKE ?', "%#{arg}%") render :text => prepare_to_token_input(result).to_json end diff --git a/app/controllers/my_profile/profile_members_controller.rb b/app/controllers/my_profile/profile_members_controller.rb index 26efff7..8fc39be 100644 --- a/app/controllers/my_profile/profile_members_controller.rb +++ b/app/controllers/my_profile/profile_members_controller.rb @@ -47,7 +47,7 @@ class ProfileMembersController < MyProfileController end def remove_role - @association = RoleAssignment.find(:all, :conditions => {:id => params[:id], :target_id => profile.id}) + @association = RoleAssignment.where(:id => params[:id], :target_id => profile.id) if @association.destroy session[:notice] = 'Member succefully unassociated' else @@ -120,7 +120,7 @@ class ProfileMembersController < MyProfileController def search_user role = Role.find(params[:role]) - render :text => environment.people.find(:all, :conditions => ['LOWER(name) LIKE ? OR LOWER(identifier) LIKE ?', "%#{params['q_'+role.key]}%", "%#{params['q_'+role.key]}%"]). + render :text => environment.people.where('LOWER(name) LIKE ? OR LOWER(identifier) LIKE ?', "%#{params['q_'+role.key]}%", "%#{params['q_'+role.key]}%"). select { |person| !profile.members_by_role(role).include?(person) }. map {|person| {:id => person.id, :name => person.name} }. to_json diff --git a/app/controllers/my_profile/tasks_controller.rb b/app/controllers/my_profile/tasks_controller.rb index 78dcee3..976c0cc 100644 --- a/app/controllers/my_profile/tasks_controller.rb +++ b/app/controllers/my_profile/tasks_controller.rb @@ -86,7 +86,7 @@ class TasksController < MyProfileController end def ticket_details - @ticket = Ticket.find(:first, :conditions => ['(requestor_id = ? or target_id = ?) and id = ?', profile.id, profile.id, params[:id]]) + @ticket = Ticket.where('(requestor_id = ? or target_id = ?) and id = ?', profile.id, profile.id, params[:id]).first end end diff --git a/app/controllers/public/profile_controller.rb b/app/controllers/public/profile_controller.rb index da07102..77509b1 100644 --- a/app/controllers/public/profile_controller.rb +++ b/app/controllers/public/profile_controller.rb @@ -36,7 +36,7 @@ class ProfileController < PublicController def tag_feed @tag = params[:id] - tagged = profile.articles.paginate(:per_page => 20, :page => 1, :order => 'published_at DESC', :include => :tags, :conditions => ['tags.name LIKE ?', @tag]) + tagged = profile.articles.paginate(:per_page => 20, :page => 1).order('published_at DESC').includes(:tags).where('tags.name LIKE ?', @tag) feed_writer = FeedWriter.new data = feed_writer.write( tagged, @@ -201,7 +201,7 @@ class ProfileController < PublicController def more_comments profile_filter = @profile.person? ? {:user_id => @profile} : {:target_id => @profile} - activity = ActionTracker::Record.find(:first, :conditions => {:id => params[:activity]}.merge(profile_filter)) + activity = ActionTracker::Record.where({:id => params[:activity]}.merge profile_filter).first comments_count = activity.comments.count comment_page = (params[:comment_page] || 1).to_i comments_per_page = 5 @@ -221,7 +221,7 @@ class ProfileController < PublicController end def more_replies - activity = Scrap.find(:first, :conditions => {:id => params[:activity], :receiver_id => @profile, :scrap_id => nil}) + activity = Scrap.where(:id => params[:activity], :receiver_id => @profile, :scrap_id => nil).first comments_count = activity.replies.count comment_page = (params[:comment_page] || 1).to_i comments_per_page = 5 @@ -268,7 +268,7 @@ class ProfileController < PublicController def remove_notification begin raise if !can_edit_profile - notification = ActionTrackerNotification.find(:first, :conditions => {:profile_id => profile.id, :action_tracker_id => params[:activity_id]}) + notification = ActionTrackerNotification.where(profile_id: profile.id, action_tracker_id: params[:activity_id]).first notification.destroy render :text => _('Notification successfully removed.') rescue diff --git a/app/mailers/environment_mailing.rb b/app/mailers/environment_mailing.rb index 02f4e45..3b16a76 100644 --- a/app/mailers/environment_mailing.rb +++ b/app/mailers/environment_mailing.rb @@ -1,7 +1,9 @@ class EnvironmentMailing < Mailing def recipients(offset=0, limit=100) - source.people.all(:order => :id, :offset => offset, :limit => limit, :joins => "LEFT OUTER JOIN mailing_sents m ON (m.mailing_id = #{id} AND m.person_id = profiles.id)", :conditions => { "m.person_id" => nil }) + source.people.order(:id).offset(offset).limit(limit) + .joins("LEFT OUTER JOIN mailing_sents m ON (m.mailing_id = #{id} AND m.person_id = profiles.id)") + .where("m.person_id" => nil) end def each_recipient diff --git a/app/mailers/organization_mailing.rb b/app/mailers/organization_mailing.rb index b4704d3..1829a21 100644 --- a/app/mailers/organization_mailing.rb +++ b/app/mailers/organization_mailing.rb @@ -5,7 +5,9 @@ class OrganizationMailing < Mailing end def recipients(offset=0, limit=100) - source.members.all(:order => :id, :offset => offset, :limit => limit, :joins => "LEFT OUTER JOIN mailing_sents m ON (m.mailing_id = #{id} AND m.person_id = profiles.id)", :conditions => { "m.person_id" => nil }) + source.members.order(:id).offset(offset).limit(limit) + .joins("LEFT OUTER JOIN mailing_sents m ON (m.mailing_id = #{id} AND m.person_id = profiles.id)") + .where("m.person_id" => nil) end def each_recipient diff --git a/app/models/article.rb b/app/models/article.rb index 6600e89..86e0025 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -73,7 +73,7 @@ class Article < ActiveRecord::Base has_many :comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc' - has_many :article_categorizations, :conditions => [ 'articles_categories.virtual = ?', false ] + has_many :article_categorizations, -> { where 'articles_categories.virtual = ?', false } has_many :categories, :through => :article_categorizations has_many :article_categorizations_including_virtual, :class_name => 'ArticleCategorization' @@ -125,15 +125,13 @@ class Article < ActiveRecord::Base xss_terminate :only => [ :name ], :on => 'validation', :with => 'white_list' - scope :in_category, lambda { |category| - {:include => 'categories_including_virtual', :conditions => { 'categories.id' => category.id }} + scope :in_category, -> (category) { + includes('categories_including_virtual').where('categories.id' => category.id) } - scope :by_range, lambda { |range| { - :conditions => [ - 'articles.published_at BETWEEN :start_date AND :end_date', { :start_date => range.first, :end_date => range.last } - ] - }} + scope :by_range, -> (range) { + where 'articles.published_at BETWEEN :start_date AND :end_date', { start_date: range.first, end_date: range.last } + } URL_FORMAT = /\A(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?\Z/ix @@ -255,20 +253,21 @@ class Article < ActiveRecord::Base # retrieves all articles belonging to the given +profile+ that are not # sub-articles of any other article. - scope :top_level_for, lambda { |profile| - {:conditions => [ 'parent_id is null and profile_id = ?', profile.id ]} + scope :top_level_for, -> { + where 'parent_id is null and profile_id = ?', profile.id } - # :public is reserved on rails 4.1, use is_public instead - scope :is_public, - :conditions => [ "advertise = ? AND published = ? AND profiles.visible = ? AND profiles.public_profile = ?", true, true, true, true ], :joins => [:profile] + scope :is_public, -> { + joins(:profile). + where("advertise = ? AND published = ? AND profiles.visible = ? AND profiles.public_profile = ?", true, true, true, true) + } - scope :more_recent, - :conditions => [ "advertise = ? AND published = ? AND profiles.visible = ? AND profiles.public_profile = ? AND - ((articles.type != ?) OR articles.type is NULL)", - true, true, true, true, 'RssFeed' - ], - :order => 'articles.published_at desc, articles.id desc' + scope :more_recent, -> { + order('articles.published_at desc, articles.id desc') + .where("advertise = ? AND published = ? AND profiles.visible = ? AND profiles.public_profile = ? AND + ((articles.type != ?) OR articles.type is NULL)", + true, true, true, true, 'RssFeed') + } # retrives the most commented articles, sorted by the comment count (largest # first) @@ -276,8 +275,10 @@ class Article < ActiveRecord::Base paginate(:order => 'comments_count DESC', :page => 1, :per_page => limit) end - scope :more_popular, :order => 'hits DESC' - scope :relevant_as_recent, :conditions => ["(articles.type != 'UploadedFile' and articles.type != 'RssFeed' and articles.type != 'Blog') OR articles.type is NULL"] + scope :more_popular, -> { order 'hits DESC' } + scope :relevant_as_recent, -> { + where "(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}). @@ -406,7 +407,7 @@ class Article < ActiveRecord::Base self.translations.map(&:language) end - scope :native_translations, :conditions => { :translation_of_id => nil } + scope :native_translations, -> { where :translation_of_id => nil } def translatable? false @@ -461,7 +462,7 @@ class Article < ActiveRecord::Base elsif self.native_translation.language == locale self.native_translation else - self.native_translation.translations.first(:conditions => { :language => locale }) + self.native_translation.translations.where(:language => locale).first end end @@ -485,19 +486,19 @@ class Article < ActiveRecord::Base ['TextArticle', 'TextileArticle', 'TinyMceArticle'] end - scope :published, :conditions => ['articles.published = ?', true] - scope :folders, lambda {|profile|{:conditions => ['articles.type IN (?)', profile.folder_types] }} - scope :no_folders, lambda {|profile|{:conditions => ['articles.type NOT IN (?)', profile.folder_types]}} - scope :galleries, :conditions => [ "articles.type IN ('Gallery')" ] - scope :images, :conditions => { :is_image => true } - scope :no_images, :conditions => { :is_image => false } - scope :text_articles, :conditions => [ 'articles.type IN (?)', text_article_types ] - scope :files, :conditions => { :type => 'UploadedFile' } - scope :with_types, lambda { |types| { :conditions => [ 'articles.type IN (?)', types ] } } + scope :published, -> { where 'articles.published = ?', true } + scope :folders, -> (profile) { where 'articles.type IN (?)', profile.folder_types } + scope :no_folders, -> (profile) { where 'articles.type NOT IN (?)', profile.folder_types } + scope :galleries, -> { where "articles.type IN ('Gallery')" } + scope :images, -> { where :is_image => true } + scope :no_images, -> { where :is_image => false } + scope :text_articles, -> { where 'articles.type IN (?)', text_article_types } + scope :files, -> { where :type => 'UploadedFile' } + scope :with_types, -> (types) { where 'articles.type IN (?)', types } - scope :more_popular, :order => 'hits DESC' - scope :more_comments, :order => "comments_count DESC" - scope :more_recent, :order => "created_at DESC" + scope :more_popular, -> { order 'hits DESC' } + scope :more_comments, -> { order "comments_count DESC" } + scope :more_recent, -> { order "created_at DESC" } scope :display_filter, lambda {|user, profile| return published if (user.nil? && profile && profile.public?) @@ -619,7 +620,7 @@ class Article < ActiveRecord::Base ] def self.find_by_old_path(old_path) - find(:first, :include => :versions, :conditions => ['article_versions.path = ?', old_path], :order => 'article_versions.id desc') + self.includes(:versions).where( 'article_versions.path = ?', old_path).order('article_versions.id DESC') end def hit diff --git a/app/models/block.rb b/app/models/block.rb index a18a5b3..8fbe600 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -20,7 +20,7 @@ class Block < ActiveRecord::Base acts_as_having_settings - scope :enabled, :conditions => { :enabled => true } + scope :enabled, -> { where :enabled => true } after_save do |block| if block.owner.kind_of?(Profile) && block.owner.is_template? && block.mirror? diff --git a/app/models/blog_archives_block.rb b/app/models/blog_archives_block.rb index 4368d93..c700d3e 100644 --- a/app/models/blog_archives_block.rb +++ b/app/models/blog_archives_block.rb @@ -35,7 +35,7 @@ class BlogArchivesBlock < Block posts.except(:order).count(:all, :group => 'EXTRACT(YEAR FROM published_at)').sort_by {|year, count| -year.to_i}.each do |year, count| results << content_tag('li', content_tag('strong', "#{year} (#{count})")) results << "" diff --git a/app/models/box.rb b/app/models/box.rb index 7a925bf..57a6174 100644 --- a/app/models/box.rb +++ b/app/models/box.rb @@ -7,7 +7,7 @@ class Box < ActiveRecord::Base include Noosfero::Plugin::HotSpot - scope :with_position, :conditions => ['boxes.position > 0'] + scope :with_position, -> { where 'boxes.position > 0' } def environment owner ? (owner.kind_of?(Environment) ? owner : owner.environment) : nil diff --git a/app/models/categorization.rb b/app/models/categorization.rb index 519b1f4..33e6500 100644 --- a/app/models/categorization.rb +++ b/app/models/categorization.rb @@ -1,11 +1,11 @@ module Categorization def add_category_to_object(category, object) - if !find(:first, :conditions => {object_id_column => object, :category_id => category} ) + if !self.where(object_id_column => object, :category_id => category).first connection.execute("insert into #{table_name} (category_id, #{object_id_column}) values(#{category.id}, #{object.id})") c = category.parent - while !c.nil? && !self.find(:first, :conditions => {object_id_column => object, :category_id => c}) + while !c.nil? && !self.where(object_id_column => object, :category_id => c).first connection.execute("insert into #{table_name} (category_id, #{object_id_column}, virtual) values(#{c.id}, #{object.id}, 1>0)") c = c.parent end diff --git a/app/models/category.rb b/app/models/category.rb index 655c4f5..f5ca326 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -14,12 +14,12 @@ class Category < ActiveRecord::Base validates_uniqueness_of :slug,:scope => [ :environment_id, :parent_id ], :message => N_('{fn} is already being used by another category.').fix_i18n belongs_to :environment - # Finds all top level categories for a given environment. - scope :top_level_for, lambda { |environment| - {:conditions => ['parent_id is null and environment_id = ?', environment.id ]} + # Finds all top level categories for a given environment. + scope :top_level_for, -> (environment) { + where 'parent_id is null and environment_id = ?', environment.id } - scope :on_level, lambda { |parent| {:conditions => {:parent_id => parent}} } + scope :on_level, -> (parent) { where :parent_id => parent } acts_as_filesystem @@ -46,10 +46,10 @@ class Category < ActiveRecord::Base display_color = nil if display_color.blank? end - scope :from_types, lambda { |types| - types.select{ |t| t.blank? }.empty? ? - { :conditions => { :type => types } } : - { :conditions => [ "type IN (?) OR type IS NULL", types.reject{ |t| t.blank? } ] } + scope :from_types, -> (types) { + if types.select{ |t| t.blank? }.empty? then + where(type: types) else + where("type IN (?) OR type IS NULL", types.reject{ |t| t.blank? }) end } def recent_people(limit = 10) @@ -81,7 +81,7 @@ class Category < ActiveRecord::Base end def upcoming_events(limit = 10) - self.events.paginate(:conditions => [ 'start_date >= ?', Date.today ], :order => 'start_date', :page => 1, :per_page => limit) + self.events.where('start_date >= ?', Date.today).order('start_date').paginate(:page => 1, :per_page => limit) end def display_in_menu? @@ -90,11 +90,11 @@ class Category < ActiveRecord::Base def children_for_menu results = [] - pending = children.find(:all, :conditions => { :display_in_menu => true}) + pending = children.where :display_in_menu => true while !pending.empty? cat = pending.shift results << cat - pending += cat.children.find(:all, :conditions => { :display_in_menu => true} ) + pending += cat.children.where :display_in_menu => true end results @@ -102,7 +102,7 @@ class Category < ActiveRecord::Base def is_leaf_displayable_in_menu? return false if self.display_in_menu == false - self.children.find(:all, :conditions => {:display_in_menu => true}).empty? + self.children.where(:display_in_menu => true).empty? end def with_color diff --git a/app/models/comment.rb b/app/models/comment.rb index c9be027..5d0da80 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -18,7 +18,7 @@ class Comment < ActiveRecord::Base has_many :children, :class_name => 'Comment', :foreign_key => 'reply_of_id', :dependent => :destroy belongs_to :reply_of, :class_name => 'Comment', :foreign_key => 'reply_of_id' - scope :without_reply, :conditions => ['reply_of_id IS NULL'] + scope :without_reply, -> { where 'reply_of_id IS NULL' } # unauthenticated authors: validates_presence_of :name, :if => (lambda { |record| !record.email.blank? }) diff --git a/app/models/domain.rb b/app/models/domain.rb index 052bd86..3595708 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -37,7 +37,7 @@ class Domain < ActiveRecord::Base # "www.", but it will be removed before searching. So searching for # 'www.example.net' is exactly the same as searching for just 'example.net' def self.find_by_name(name) - self.find(:first, :conditions => [ 'name = ?', self.extract_domain_name(name) ]) + self.where('name = ?', self.extract_domain_name(name)).first end # turns the argument (expected to be a String) into a domain name that is diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index cab8386..0138adc 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -68,7 +68,7 @@ class Enterprise < Organization end def highlighted_products_with_image(options = {}) - Product.find(:all, {:conditions => {:highlighted => true}, :joins => :image}.merge(options)) + Product.where(:highlighted => true).joins(:image) end def required_fields diff --git a/app/models/environment.rb b/app/models/environment.rb index 757eaf4..3eee221 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -111,7 +111,7 @@ class Environment < ActiveRecord::Base def admins admin_role = Environment::Roles.admin(self) return [] if admin_role.blank? - Person.members_of(self).all(:conditions => ['role_assignments.role_id = ?', admin_role.id]) + Person.members_of(self).where 'role_assignments.role_id = ?', admin_role.id end # returns the available features for a Environment, in the form of a @@ -222,9 +222,11 @@ class Environment < ActiveRecord::Base has_many :licenses has_many :categories - has_many :display_categories, :class_name => 'Category', :conditions => 'display_color is not null and parent_id is null', :order => 'display_color' + has_many :display_categories, -> { + order('display_color').where('display_color is not null and parent_id is null') + }, class_name: 'Category' - has_many :product_categories, :conditions => { :type => 'ProductCategory'} + has_many :product_categories, -> { where type: 'ProductCategory'} has_many :regions has_many :states has_many :cities @@ -672,7 +674,7 @@ class Environment < ActiveRecord::Base # the default Environment. def self.default - self.find(:first, :conditions => [ 'is_default = ?', true ] ) + self.where('is_default = ?', true).first end # returns an array with the top level categories for this environment. @@ -853,7 +855,7 @@ class Environment < ActiveRecord::Base end def portal_folders - (settings[:portal_folders] || []).map{|fid| portal_community.articles.find(:first, :conditions => { :id => fid }) }.compact + (settings[:portal_folders] || []).map{|fid| portal_community.articles.where(id: fid).first }.compact end def portal_folders=(folders) @@ -940,7 +942,7 @@ class Environment < ActiveRecord::Base end def highlighted_products_with_image(options = {}) - Product.find(:all, {:conditions => {:highlighted => true, :profile_id => self.enterprises.find(:all, :select => :id) }, :joins => :image}.merge(options)) + Product.where(highlighted: true, profile_id: self.enterprises.where(select: :id)).joins(:image) end settings_items :home_cache_in_minutes, :type => :integer, :default => 5 diff --git a/app/models/event.rb b/app/models/event.rb index 743a1a7..c834067 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -34,23 +34,20 @@ class Event < Article end end - scope :by_day, lambda { |date| - { :conditions => ['start_date = :date AND end_date IS NULL OR (start_date <= :date AND end_date >= :date)', {:date => date}], - :order => 'start_date ASC' - } + scope :by_day, -> (date) { + order('start_date ASC') + .where('start_date = :date AND end_date IS NULL OR (start_date <= :date AND end_date >= :date)', {:date => date}) } - scope :next_events_from_month, lambda { |date| + scope :next_events_from_month, -> (date) { date_temp = date.strftime("%Y-%m-%d") - { :conditions => ["start_date >= ?","#{date_temp}"], - :order => 'start_date ASC' - } + order('start_date ASC') + .where("start_date >= ?","#{date_temp}") } - scope :by_month, lambda { |date| - { :conditions => ["EXTRACT(YEAR FROM start_date) = ? AND EXTRACT(MONTH FROM start_date) = ?",date.year,date.month], - :order => 'start_date ASC' - } + scope :by_month, -> (date) { + order('start_date ASC') + .where("EXTRACT(YEAR FROM start_date) = ? AND EXTRACT(MONTH FROM start_date) = ?", date.year, date.month) } include WhiteListFilter @@ -71,12 +68,10 @@ class Event < Article 'event' end - scope :by_range, lambda { |range| { - :conditions => [ - 'start_date BETWEEN :start_day AND :end_day OR end_date BETWEEN :start_day AND :end_day', - { :start_day => range.first, :end_day => range.last } - ] - }} + scope :by_range, -> (range) { + where('start_date BETWEEN :start_day AND :end_day OR end_date BETWEEN :start_day AND :end_day', + {:start_day => range.first, :end_day => range.last}) + } def self.date_range(year, month) if year.nil? || month.nil? diff --git a/app/models/external_feed.rb b/app/models/external_feed.rb index 2220262..e3111fd 100644 --- a/app/models/external_feed.rb +++ b/app/models/external_feed.rb @@ -5,9 +5,9 @@ class ExternalFeed < ActiveRecord::Base validates_presence_of :address, :if => lambda {|efeed| efeed.enabled} validates_uniqueness_of :blog_id - scope :enabled, :conditions => { :enabled => true } - scope :expired, lambda { - { :conditions => ['(fetched_at is NULL) OR (fetched_at < ?)', Time.now - FeedUpdater.update_interval] } + scope :enabled, -> { where enabled: true } + scope :expired, -> { + where '(fetched_at is NULL) OR (fetched_at < ?)', Time.now - FeedUpdater.update_interval } attr_accessible :address, :enabled, :only_once diff --git a/app/models/feed_reader_block.rb b/app/models/feed_reader_block.rb index 555feeb..dd9b91f 100644 --- a/app/models/feed_reader_block.rb +++ b/app/models/feed_reader_block.rb @@ -27,8 +27,8 @@ class FeedReaderBlock < Block settings_items :update_errors, :type => :integer, :default => 0 settings_items :error_message, :type => :string - scope :expired, lambda { - { :conditions => [ '(fetched_at is NULL) OR (fetched_at < ?)', Time.now - FeedUpdater.update_interval] } + scope :expired, -> { + where '(fetched_at is NULL) OR (fetched_at < ?)', Time.now - FeedUpdater.update_interval } before_create do |block| diff --git a/app/models/folder.rb b/app/models/folder.rb index 7640c12..26229ac 100644 --- a/app/models/folder.rb +++ b/app/models/folder.rb @@ -56,10 +56,11 @@ class Folder < Article profile.recent_documents(limit, ["articles.type != ? AND articles.highlighted = ? AND articles.parent_id = ?", 'Folder', highlight, id]) end - has_many :images, :class_name => 'Article', - :foreign_key => 'parent_id', - :order => 'articles.type, articles.name', - :conditions => ["articles.type = 'UploadedFile' and articles.content_type in (?) or articles.type in ('Folder','Gallery')", UploadedFile.content_types] + has_many :images, -> { + order('articles.type, articles.name'). + where("articles.type = 'UploadedFile' and articles.content_type in (?) or articles.type in ('Folder','Gallery')", UploadedFile.content_types) + }, class_name: 'Article', foreign_key: 'parent_id' + def accept_uploads? !self.has_posts? || self.gallery? diff --git a/app/models/input.rb b/app/models/input.rb index a4938cc..9bf4546 100644 --- a/app/models/input.rb +++ b/app/models/input.rb @@ -13,7 +13,7 @@ class Input < ActiveRecord::Base belongs_to :unit - scope :relevant_to_price, :conditions => { :relevant_to_price => true } + scope :relevant_to_price, -> { where relevant_to_price: true } include FloatHelper diff --git a/app/models/invite_friend.rb b/app/models/invite_friend.rb index b3b6dc5..b74b0bc 100644 --- a/app/models/invite_friend.rb +++ b/app/models/invite_friend.rb @@ -45,7 +45,7 @@ class InviteFriend < Invitation private def check_for_invitation_existence if friend - friend.tasks.pending.of("InviteFriend").find(:all, :conditions => {:requestor_id => person.id, :target_id => friend.id}).blank? + friend.tasks.pending.of("InviteFriend").where(requestor_id: person.id, target_id: friend.id).blank? end end diff --git a/app/models/invite_member.rb b/app/models/invite_member.rb index 884b40c..69212b7 100644 --- a/app/models/invite_member.rb +++ b/app/models/invite_member.rb @@ -65,7 +65,7 @@ class InviteMember < Invitation private def check_for_invitation_existence if friend - friend.tasks.pending.of("InviteMember").find(:all, :conditions => {:requestor_id => person.id}).select { |t| t.data[:community_id] == community_id }.blank? + friend.tasks.pending.of("InviteMember").where(requestor_id: person.id).select{ |t| t.data[:community_id] == community_id }.blank? end end diff --git a/app/models/organization.rb b/app/models/organization.rb index 17fb7b8..2b24b14 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -56,7 +56,7 @@ class Organization < Profile end def find_pending_validation(code) - validations.pending.find(:first, :conditions => {:code => code}) + validations.pending.where(code: code).first end def processed_validations @@ -64,7 +64,7 @@ class Organization < Profile end def find_processed_validation(code) - validations.finished.find(:first, :conditions => {:code => code}) + validations.finished.where(code: code).first end def is_validation_entity? @@ -162,7 +162,7 @@ class Organization < Profile end def already_request_membership?(person) - self.tasks.pending.find_by_requestor_id(person.id, :conditions => { :type => 'AddMember' }) + self.tasks.pending.where(type: 'AddMember', requestor_id: person.id).first end def jid(options = {}) diff --git a/app/models/person.rb b/app/models/person.rb index 487b543..27cab2b 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -16,27 +16,26 @@ class Person < Profile acts_as_trackable :after_add => Proc.new {|p,t| notify_activity(t)} acts_as_accessor - scope :members_of, lambda { |resources| + scope :members_of, -> (resources) { resources = [resources] if !resources.kind_of?(Array) conditions = resources.map {|resource| "role_assignments.resource_type = '#{resource.class.base_class.name}' AND role_assignments.resource_id = #{resource.id || -1}"}.join(' OR ') - { :select => 'DISTINCT profiles.*', :joins => :role_assignments, :conditions => [conditions] } + select('DISTINCT profiles.*').joins(:role_assignments).where([conditions]) } - scope :not_members_of, lambda { |resources| + scope :not_members_of, -> (resources) { resources = [resources] if !resources.kind_of?(Array) conditions = resources.map {|resource| "role_assignments.resource_type = '#{resource.class.base_class.name}' AND role_assignments.resource_id = #{resource.id || -1}"}.join(' OR ') - { :select => 'DISTINCT profiles.*', :conditions => ['"profiles"."id" NOT IN (SELECT DISTINCT profiles.id FROM "profiles" INNER JOIN "role_assignments" ON "role_assignments"."accessor_id" = "profiles"."id" AND "role_assignments"."accessor_type" = (\'Profile\') WHERE "profiles"."type" IN (\'Person\') AND (%s))' % conditions] } + select('DISTINCT profiles.*').where('"profiles"."id" NOT IN (SELECT DISTINCT profiles.id FROM "profiles" INNER JOIN "role_assignments" ON "role_assignments"."accessor_id" = "profiles"."id" AND "role_assignments"."accessor_type" = (\'Profile\') WHERE "profiles"."type" IN (\'Person\') AND (%s))' % conditions) } - scope :by_role, lambda { |roles| + scope :by_role, -> (roles) { roles = [roles] unless roles.kind_of?(Array) - { :select => 'DISTINCT profiles.*', :joins => :role_assignments, :conditions => ['role_assignments.role_id IN (?)', -roles] } + select('DISTINCT profiles.*').joins(:role_assignments).where('role_assignments.role_id IN (?)', roles) } - scope :not_friends_of, lambda { |resources| + scope :not_friends_of, -> (resources) { resources = Array(resources) - { :select => 'DISTINCT profiles.*', :conditions => ['"profiles"."id" NOT IN (SELECT DISTINCT profiles.id FROM "profiles" INNER JOIN "friendships" ON "friendships"."person_id" = "profiles"."id" WHERE "friendships"."friend_id" IN (%s))' % resources.map(&:id)] } + select('DISTINCT profiles.*').where('"profiles"."id" NOT IN (SELECT DISTINCT profiles.id FROM "profiles" INNER JOIN "friendships" ON "friendships"."person_id" = "profiles"."id" WHERE "friendships"."friend_id" IN (%s))' % resources.map(&:id)) } def has_permission_with_admin?(permission, resource) @@ -71,7 +70,9 @@ roles] } has_many :friendships, :dependent => :destroy has_many :friends, :class_name => 'Person', :through => :friendships - scope :online, lambda { { :include => :user, :conditions => ["users.chat_status != '' AND users.chat_status_at >= ?", DateTime.now - User.expires_chat_status_every.minutes] } } + scope :online, -> { + includes(:user).where("users.chat_status != '' AND users.chat_status_at >= ?", DateTime.now - User.expires_chat_status_every.minutes) + } has_many :requested_tasks, :class_name => 'Task', :foreign_key => :requestor_id, :dependent => :destroy @@ -84,21 +85,31 @@ roles] } has_and_belongs_to_many :acepted_forums, :class_name => 'Forum', :join_table => 'terms_forum_people' has_and_belongs_to_many :articles_with_access, :class_name => 'Article', :join_table => 'article_privacy_exceptions' - has_many :suggested_profiles, :class_name => 'ProfileSuggestion', :foreign_key => :person_id, :order => 'score DESC', :dependent => :destroy - has_many :suggested_people, :through => :suggested_profiles, :source => :suggestion, :conditions => ['profile_suggestions.suggestion_type = ? AND profile_suggestions.enabled = ?', 'Person', true] - has_many :suggested_communities, :through => :suggested_profiles, :source => :suggestion, :conditions => ['profile_suggestions.suggestion_type = ? AND profile_suggestions.enabled = ?', 'Community', true] + has_many :suggested_profiles, class_name: 'ProfileSuggestion', foreign_key: :person_id, order: 'score DESC', dependent: :destroy + has_many :suggested_people, -> { + where 'profile_suggestions.suggestion_type = ? AND profile_suggestions.enabled = ?', 'Person', true + }, through: :suggested_profiles, source: :suggestion + has_many :suggested_communities, -> { + where 'profile_suggestions.suggestion_type = ? AND profile_suggestions.enabled = ?', 'Community', true + }, through: :suggested_profiles, source: :suggestion - scope :more_popular, :order => 'friends_count DESC' + scope :more_popular, -> { order 'friends_count DESC' } - scope :abusers, :joins => :abuse_complaints, :conditions => ['tasks.status = 3'], :select => 'DISTINCT profiles.*' - scope :non_abusers, :joins => "LEFT JOIN tasks ON profiles.id = tasks.requestor_id AND tasks.type='AbuseComplaint'", :conditions => ["tasks.status != 3 OR tasks.id is NULL"], :select => "DISTINCT profiles.*" + scope :abusers, -> { + joins(:abuse_complaints).where('tasks.status = 3').select('DISTINCT profiles.*') + } + scope :non_abusers, -> { + select("DISTINCT profiles.*"). + joins("LEFT JOIN tasks ON profiles.id = tasks.requestor_id AND tasks.type='AbuseComplaint'"). + where("tasks.status != 3 OR tasks.id is NULL") + } - scope :admins, :joins => [:role_assignments => :role], :conditions => ['roles.key = ?', 'environment_administrator' ] - scope :activated, :joins => :user, :conditions => ['users.activation_code IS NULL AND users.activated_at IS NOT NULL'] - scope :deactivated, :joins => :user, :conditions => ['NOT (users.activation_code IS NULL AND users.activated_at IS NOT NULL)'] + scope :admins, -> { joins(:role_assignments => :role).where('roles.key = ?', 'environment_administrator') } + scope :activated, -> { joins(:user).where('users.activation_code IS NULL AND users.activated_at IS NOT NULL') } + scope :deactivated, -> { joins(:user).where('NOT (users.activation_code IS NULL AND users.activated_at IS NOT NULL)') } after_destroy do |person| - Friendship.find(:all, :conditions => { :friend_id => person.id}).each { |friendship| friendship.destroy } + Friendship.where(friend_id: person.id).each{ |friendship| friendship.destroy } end belongs_to :user, :dependent => :delete @@ -156,7 +167,7 @@ roles] } end def remove_friend(friend) - Friendship.find(:first, :conditions => {:friend_id => friend, :person_id => id}).destroy + Friendship.where(friend_id: friend, person_id: id).first.destroy end FIELDS = %w[ @@ -267,7 +278,7 @@ roles] } end validates_each :email, :on => :update do |record,attr,value| - if User.find(:first, :conditions => ['email = ? and id != ? and environment_id = ?', value, record.user.id, record.environment.id]) + if User.where('email = ? and id != ? and environment_id = ?', value, record.user.id, record.environment.id).first record.errors.add(attr, _('{fn} is already used by other user').fix_i18n) end end @@ -379,7 +390,7 @@ roles] } def ask_to_join?(community) return false if !community.visible? return false if memberships.include?(community) - return false if AddMember.find(:first, :conditions => {:requestor_id => self.id, :target_id => community.id}) + return false if AddMember.where(requestor_id: self.id, target_id: community.id).first !refused_communities.include?(community) end diff --git a/app/models/person_notifier.rb b/app/models/person_notifier.rb index 9cb0d53..c2f672e 100644 --- a/app/models/person_notifier.rb +++ b/app/models/person_notifier.rb @@ -28,7 +28,7 @@ class PersonNotifier def notify if @person.notification_time && @person.notification_time > 0 - notifications = @person.tracked_notifications.find(:all, :conditions => ["created_at > ?", notify_from]) + notifications = @person.tracked_notifications.where("created_at > ?", notify_from) tasks = Task.to(@person).without_spam.pending.where("created_at > ?", notify_from).order_by('created_at', 'asc') Noosfero.with_locale @person.environment.default_language do diff --git a/app/models/product.rb b/app/models/product.rb index e9137ff..bb2aa10 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -47,8 +47,8 @@ class Product < ActiveRecord::Base scope :more_recent, :order => "created_at DESC" - scope :from_category, lambda { |category| - {:joins => :product_category, :conditions => ['categories.path LIKE ?', "%#{category.slug}%"]} if category + scope :from_category, -> (category) { + joins(:product_category).where('categories.path LIKE ?', "%#{category.slug}%") if category } after_update :save_image diff --git a/app/models/product_category.rb b/app/models/product_category.rb index db73ae2..f1a56a4 100644 --- a/app/models/product_category.rb +++ b/app/models/product_category.rb @@ -6,19 +6,19 @@ class ProductCategory < Category attr_accessible :name, :parent, :environment scope :unique, :select => 'DISTINCT ON (path) categories.*' - scope :by_enterprise, lambda { |enterprise| { - :joins => :products, - :conditions => ['products.profile_id = ?', enterprise.id] - }} - scope :by_environment, lambda { |environment| { - :conditions => ['environment_id = ?', environment.id] - }} - scope :unique_by_level, lambda { |level| { - :select => "DISTINCT ON (filtered_category) split_part(path, '/', #{level}) AS filtered_category, categories.*" - }} + scope :by_enterprise, -> (enterprise) { + joins(:products). + where('products.profile_id = ?', enterprise.id) + } + scope :by_environment, -> (environment) { + where 'environment_id = ?', environment.id + } + scope :unique_by_level, -> (level) { + select "DISTINCT ON (filtered_category) split_part(path, '/', #{level}) AS filtered_category, categories.*" + } def all_products - Product.find(:all, :conditions => { :product_category_id => (all_children << self).map(&:id) }) + Product.where(product_category_id: (all_children << self).map(&:id)) end def self.menu_categories(top_category, env) diff --git a/app/models/profile.rb b/app/models/profile.rb index 12a83c4..a28cb7e 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -48,7 +48,7 @@ class Profile < ActiveRecord::Base all_roles(env_id).select{ |r| r.key.match(/^profile_/) unless r.key.blank? || !r.profile_id.nil?} end def self.all_roles(env_id) - Role.all :conditions => { :environment_id => env_id } + Role.where(environment_id: env_id) end def self.method_missing(m, *args, &block) role = find_role(m, args[0]) @@ -85,20 +85,26 @@ class Profile < ActiveRecord::Base include Noosfero::Plugin::HotSpot - scope :memberships_of, lambda { |person| { :select => 'DISTINCT profiles.*', :joins => :role_assignments, :conditions => ['role_assignments.accessor_type = ? AND role_assignments.accessor_id = ?', person.class.base_class.name, person.id ] } } + scope :memberships_of, -> (person) { + select('DISTINCT profiles.*'). + joins(:role_assignments). + where('role_assignments.accessor_type = ? AND role_assignments.accessor_id = ?', person.class.base_class.name, person.id) + } #FIXME: these will work only if the subclass is already loaded - scope :enterprises, lambda { {:conditions => (Enterprise.send(:subclasses).map(&:name) << 'Enterprise').map { |klass| "profiles.type = '#{klass}'"}.join(" OR ")} } - scope :communities, lambda { {:conditions => (Community.send(:subclasses).map(&:name) << 'Community').map { |klass| "profiles.type = '#{klass}'"}.join(" OR ")} } - scope :templates, lambda { |template_id = nil| - conditions = {:conditions => {:is_template => true}} - conditions[:conditions].merge!({:id => template_id}) unless template_id.nil? - conditions + scope :enterprises, -> { + where((Enterprise.send(:subclasses).map(&:name) << 'Enterprise').map { |klass| "profiles.type = '#{klass}'"}.join(" OR ")) + } + scope :communities, -> { + where((Community.send(:subclasses).map(&:name) << 'Community').map { |klass| "profiles.type = '#{klass}'"}.join(" OR ")) + } + scope :templates, -> (template_id = nil) { + if template_id then where id: template_id else where is_template: true end } - scope :with_templates, lambda { |templates| - {:conditions => {:template_id => templates}} + scope :with_templates, -> (templates) { + where template_id: templates } - scope :no_templates, {:conditions => {:is_template => false}} + scope :no_templates, -> { where is_template: false } def members scopes = plugins.dispatch_scopes(:organization_members, self) @@ -131,10 +137,10 @@ class Profile < ActiveRecord::Base Profile.column_names.map{|n| [Profile.table_name, n].join('.')}.join(',') end - scope :visible, :conditions => { :visible => true, :secret => false } - scope :disabled, :conditions => { :visible => false } - scope :is_public, :conditions => { :visible => true, :public_profile => true, :secret => false } - scope :enabled, :conditions => { :enabled => true } + scope :visible, -> { where visible: true, secret: false } + scope :disabled, -> { where visible: false } + scope :is_public, -> { where visible: true, public_profile: true, secret: false } + scope :enabled, -> { where enabled: true } # Subclasses must override this method scope :more_popular @@ -234,7 +240,7 @@ class Profile < ActiveRecord::Base end end - has_many :profile_categorizations, :conditions => [ 'categories_profiles.virtual = ?', false ] + has_many :profile_categorizations, -> { where 'categories_profiles.virtual = ?', false } has_many :categories, :through => :profile_categorizations has_many :profile_categorizations_including_virtual, :class_name => 'ProfileCategorization' @@ -461,14 +467,13 @@ class Profile < ActiveRecord::Base self.articles.recent(limit, options, pagination) end - def last_articles(limit = 10, options = {}) - options = { :limit => limit, - :conditions => ["advertise = ? AND published = ? AND - ((articles.type != ? and articles.type != ? and articles.type != ?) OR - articles.type is NULL)", - true, true, 'UploadedFile', 'RssFeed', 'Blog'], - :order => 'articles.published_at desc, articles.id desc' }.merge(options) - self.articles.find(:all, options) + def last_articles limit = 10 + self.articles.limit(limit).where( + "advertise = ? AND published = ? AND + ((articles.type != ? and articles.type != ? and articles.type != ?) OR + articles.type is NULL)", + true, true, 'UploadedFile', 'RssFeed', 'Blog' + ).order('articles.published_at desc, articles.id desc') end class << self diff --git a/app/models/profile_categorization.rb b/app/models/profile_categorization.rb index b11472e..5d689bf 100644 --- a/app/models/profile_categorization.rb +++ b/app/models/profile_categorization.rb @@ -13,7 +13,7 @@ class ProfileCategorization < ActiveRecord::Base end def self.remove_region(profile) - region = profile.categories.find(:first, :conditions => { :type => [Region, State, City].map(&:name) }) + region = profile.categories.where(type: [Region, State, City].map(&:name)).first if region ids = region.hierarchy.map(&:id) self.delete_all(:profile_id => profile.id, :category_id => ids) diff --git a/app/models/profile_suggestion.rb b/app/models/profile_suggestion.rb index 9f0c669..cf58b30 100644 --- a/app/models/profile_suggestion.rb +++ b/app/models/profile_suggestion.rb @@ -26,9 +26,9 @@ class ProfileSuggestion < ActiveRecord::Base end validates_uniqueness_of :suggestion_id, :scope => [ :person_id ] - scope :of_person, :conditions => { :suggestion_type => 'Person' } - scope :of_community, :conditions => { :suggestion_type => 'Community' } - scope :enabled, :conditions => { :enabled => true } + scope :of_person, -> { where suggestion_type: 'Person' } + scope :of_community, -> { where suggestion_type: 'Community' } + scope :enabled, -> { where enabled: true } # {:category_type => ['category-icon', 'category-label']} CATEGORIES = { diff --git a/app/models/rss_feed.rb b/app/models/rss_feed.rb index 97dd9aa..7a0efda 100644 --- a/app/models/rss_feed.rb +++ b/app/models/rss_feed.rb @@ -69,7 +69,7 @@ class RssFeed < Article def fetch_articles if parent && parent.has_posts? language = self.language.blank? ? {} : { :language => self.language } - return parent.posts.find(:all, :conditions => { :published => true }.merge(language), :limit => self.limit, :order => 'id desc') + return parent.posts.where({published: true}.merge language).limit(self.limit).order('id desc') end articles = diff --git a/app/models/scrap.rb b/app/models/scrap.rb index 362d696..05188de 100644 --- a/app/models/scrap.rb +++ b/app/models/scrap.rb @@ -13,9 +13,9 @@ class Scrap < ActiveRecord::Base has_many :replies, :class_name => 'Scrap', :foreign_key => 'scrap_id', :dependent => :destroy belongs_to :root, :class_name => 'Scrap', :foreign_key => 'scrap_id' - scope :all_scraps, lambda {|profile| {:conditions => ["receiver_id = ? OR sender_id = ?", profile, profile], :limit => 30}} + scope :all_scraps, -> (profile) { limit(30).where("receiver_id = ? OR sender_id = ?", profile, profile) } - scope :not_replies, :conditions => {:scrap_id => nil} + scope :not_replies, -> { where scrap_id: nil } track_actions :leave_scrap, :after_create, :keep_params => ['sender.name', 'content', 'receiver.name', 'receiver.url'], :if => Proc.new{|s| s.sender != s.receiver && s.sender != s.top_root.receiver}, :custom_target => :action_tracker_target diff --git a/app/models/search_term_occurrence.rb b/app/models/search_term_occurrence.rb index 75a8f29..83c64e5 100644 --- a/app/models/search_term_occurrence.rb +++ b/app/models/search_term_occurrence.rb @@ -1,9 +1,11 @@ class SearchTermOccurrence < ActiveRecord::Base + belongs_to :search_term validates_presence_of :search_term attr_accessible :search_term, :created_at, :total, :indexed EXPIRATION_TIME = 1.year - scope :valid, :conditions => ["search_term_occurrences.created_at > ?", DateTime.now - EXPIRATION_TIME] + scope :valid, -> { where "search_term_occurrences.created_at > ?", DateTime.now - EXPIRATION_TIME } + end diff --git a/app/models/slideshow_block.rb b/app/models/slideshow_block.rb index c73e095..e15df70 100644 --- a/app/models/slideshow_block.rb +++ b/app/models/slideshow_block.rb @@ -13,7 +13,7 @@ class SlideshowBlock < Block end def gallery - gallery_id ? Gallery.find(:first, :conditions => { :id => gallery_id }) : nil + if gallery_id then Gallery.where(id: gallery_id).first else nil end end def public_filename_for(image) diff --git a/app/models/task.rb b/app/models/task.rb index 927031d..446ddc4 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -236,27 +236,28 @@ class Task < ActiveRecord::Base end end - scope :pending, :conditions => { :status => Task::Status::ACTIVE } - scope :hidden, :conditions => { :status => Task::Status::HIDDEN } - scope :finished, :conditions => { :status => Task::Status::FINISHED } - scope :canceled, :conditions => { :status => Task::Status::CANCELLED } - scope :closed, :conditions => { :status => [Task::Status::CANCELLED, Task::Status::FINISHED] } - scope :opened, :conditions => { :status => [Task::Status::ACTIVE, Task::Status::HIDDEN] } - scope :of, lambda { |type| conditions = type ? "type LIKE '#{type}'" : "1=1"; {:conditions => [conditions]} } - scope :order_by, lambda { |attribute, ord| {:order => "#{attribute} #{ord}"} } - scope :like, lambda { |field, value| where("LOWER(#{field}) LIKE ?", "%#{value.downcase}%") if value} - scope :pending_all, lambda { |profile, filter_type, filter_text| + scope :pending, -> { where status: Task::Status::ACTIVE } + scope :hidden, -> { where status: Task::Status::HIDDEN } + scope :finished, -> { where status: Task::Status::FINISHED } + scope :canceled, -> { where status: Task::Status::CANCELLED } + scope :closed, -> { where status: [Task::Status::CANCELLED, Task::Status::FINISHED] } + scope :opened, -> { where status: [Task::Status::ACTIVE, Task::Status::HIDDEN] } + scope :of, -> (type) { where "type LIKE ?", type if type } + scope :order_by, -> (attribute, ord) { order "#{attribute} #{ord}" } + scope :like, -> (field, value) { where "LOWER(#{field}) LIKE ?", "%#{value.downcase}%" if value } + scope :pending_all, -> (profile, filter_type, filter_text) { self.to(profile).without_spam.pending.of(filter_type).like('data', filter_text) } scope :to, lambda { |profile| environment_condition = nil if profile.person? - envs_ids = Environment.find(:all).select{ |env| profile.is_admin?(env) }.map { |env| "target_id = #{env.id}"}.join(' OR ') + envs_ids = Environment.all.select{ |env| profile.is_admin?(env) }.map{ |env| "target_id = #{env.id}"}.join(' OR ') environment_condition = envs_ids.blank? ? nil : "(target_type = 'Environment' AND (#{envs_ids}))" end profile_condition = "(target_type = 'Profile' AND target_id = #{profile.id})" - { :conditions => [environment_condition, profile_condition].compact.join(' OR ') } + + where [environment_condition, profile_condition].compact.join(' OR ') } def self.pending_types_for(profile) @@ -319,7 +320,7 @@ class Task < ActiveRecord::Base # # Can be used in subclasses to find only their instances. def find_by_code(code) - self.find(:first, :conditions => { :code => code, :status => Task::Status::ACTIVE }) + self.where(code: code, status: Task::Status::ACTIVE) end def per_page diff --git a/app/models/user.rb b/app/models/user.rb index 4b28f12..8d6903d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -111,8 +111,8 @@ class User < ActiveRecord::Base # Authenticates a user by their login name or email and unencrypted password. Returns the user or nil. def self.authenticate(login, password, environment = nil) environment ||= Environment.default - u = self.first :conditions => ['(login = ? OR email = ?) AND environment_id = ? AND activated_at IS NOT NULL', - login, login, environment.id] # need to get the salt + u = self.where('(login = ? OR email = ?) AND environment_id = ? AND activated_at IS NOT NULL', + login, login, environment.id).first # need to get the salt u && u.authenticated?(password) ? u : nil end diff --git a/app/views/features/index.html.erb b/app/views/features/index.html.erb index 9d17809..9280cd1 100644 --- a/app/views/features/index.html.erb +++ b/app/views/features/index.html.erb @@ -44,7 +44,7 @@ Check all the features you want to enable for your environment, uncheck all the
<%= _('Allow these people to access this environment:') %>
- <% tokenized_members = prepare_to_token_input(environment.people.find(:all, :conditions => {:id => environment.members_whitelist})) %> + <% tokenized_members = prepare_to_token_input(environment.people.where(id: environment.members_whitelist)) %> <%= token_input_field_tag('environment[members_whitelist]', 'search-members', {:action => 'search_members'}, {:focus => false, :hint_text => _('Type in a search term for a user'), :pre_populate => tokenized_members}) %>

diff --git a/app/views/search/_full_forum.html.erb b/app/views/search/_full_forum.html.erb index 8e3a60c..abb26a6 100644 --- a/app/views/search/_full_forum.html.erb +++ b/app/views/search/_full_forum.html.erb @@ -11,7 +11,7 @@ <%= _("Last topics") %> - <% r = forum.children.find(:all, :order => :updated_at, :conditions => ['type != ?', 'RssFeed']).last(3) %> + <% r = forum.children.order(:updated_at).where('type != ?', 'RssFeed').last(3) %> "> <% r.each do |a| %> <%= link_to a.title, a.view_url, :class => 'search-forum-sample-item '+icon_for_article(a) %> diff --git a/app/views/search/_image.html.erb b/app/views/search/_image.html.erb index b3800c0..dbb3080 100644 --- a/app/views/search/_image.html.erb +++ b/app/views/search/_image.html.erb @@ -26,7 +26,7 @@ <% end %> <% elsif image.is_a? Gallery %>