[Git][noosfero/noosfero][rails5] 2 commits: rails5: fix remaining person tests

Bráulio Bhavamitra gitlab at mg.gitlab.com
Sun Feb 21 19:41:57 BRT 2016


Bráulio Bhavamitra pushed to branch rails5 at Noosfero / noosfero


Commits:
96b1e007 by Braulio Bhavamitra at 2016-02-21T19:19:43-03:00
rails5: fix remaining person tests

- - - - -
3b190c3f by Braulio Bhavamitra at 2016-02-21T19:35:21-03:00
rails5: fix article test

- - - - -


23 changed files:

- app/controllers/my_profile/tasks_controller.rb
- app/controllers/public/content_viewer_controller.rb
- app/models/application_record.rb
- app/models/article.rb
- app/models/person.rb
- app/models/profile.rb
- config/initializers/action_tracker.rb
- config/initializers/dependencies.rb
- db/migrate/069_add_enviroment_id_to_role.rb
- db/migrate/20100730141134_set_owner_environment_to_enterprises_environment.rb
- lib/delayed_attachment_fu.rb
- lib/postgresql_attachment_fu.rb
- plugins/newsletter/controllers/newsletter_plugin_admin_controller.rb
- plugins/stoa/lib/ext/profile_suggestion.rb
- plugins/stoa/lib/stoa_plugin/usp_aluno_turma_grad.rb
- test/functional/profile_members_controller_test.rb
- test/unit/article_categorization_test.rb
- test/unit/article_test.rb
- test/unit/person_test.rb
- test/unit/profile_categorization_test.rb
- test/unit/profile_test.rb
- test/unit/uploaded_file_test.rb
- vendor/plugins/access_control/lib/acts_as_accessible.rb


Changes:

=====================================
app/controllers/my_profile/tasks_controller.rb
=====================================
--- a/app/controllers/my_profile/tasks_controller.rb
+++ b/app/controllers/my_profile/tasks_controller.rb
@@ -82,7 +82,7 @@ class TasksController < MyProfileController
   end
 
   def list_requested
-    @tasks = Task.without_spam.find_all_by_requestor_id(profile.id)
+    @tasks = Task.without_spam.where(requestor_id: profile.id)
   end
 
   def ticket_details


=====================================
app/controllers/public/content_viewer_controller.rb
=====================================
--- a/app/controllers/public/content_viewer_controller.rb
+++ b/app/controllers/public/content_viewer_controller.rb
@@ -173,7 +173,7 @@ class ContentViewerController < ApplicationController
 
   def redirected_page_from_old_path(path)
     unless @page
-      page_from_old_path = profile.articles.find_by old_path: path
+      page_from_old_path = profile.articles.find_by_old_path path
       if page_from_old_path
         redirect_to profile.url.merge(:page => page_from_old_path.explode_path)
         return true


=====================================
app/models/application_record.rb
=====================================
--- a/app/models/application_record.rb
+++ b/app/models/application_record.rb
@@ -2,6 +2,8 @@ class ApplicationRecord < ActiveRecord::Base
 
   self.abstract_class = true
 
+  extend PostgresqlAttachmentFu::ClassMethods
+
   def self.postgresql?
     ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
   end
@@ -25,7 +27,7 @@ class ApplicationRecord < ActiveRecord::Base
   alias :meta_cache_key :cache_key
   def cache_key
     key = [Noosfero::VERSION, meta_cache_key]
-    key.unshift(ActiveRecord::Base.connection.schema_search_path) if ActiveRecord::Base.postgresql?
+    key.unshift(ActiveRecord::Base.connection.schema_search_path) if ApplicationRecord.postgresql?
     key.join('/')
   end
 


=====================================
app/models/article.rb
=====================================
--- a/app/models/article.rb
+++ b/app/models/article.rb
@@ -447,7 +447,7 @@ class Article < ApplicationRecord
 
   def rotate_translations
     unless self.translations.empty?
-      rotate = self.translations.all
+      rotate = Array(self.translations.all)
       root = rotate.shift
       root.update_attribute(:translation_of_id, nil)
       root.translations = rotate


=====================================
app/models/person.rb
=====================================
--- a/app/models/person.rb
+++ b/app/models/person.rb
@@ -16,7 +16,7 @@ class Person < Profile
   scope :members_of, -> resources {
     resources = Array(resources)
     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).where([conditions])
+    select('DISTINCT profiles.*').joins(:role_assignments).where(conditions)
   }
 
   scope :not_members_of, -> resources {


=====================================
app/models/profile.rb
=====================================
--- a/app/models/profile.rb
+++ b/app/models/profile.rb
@@ -55,7 +55,7 @@ class Profile < ApplicationRecord
     end
     private
     def self.find_role(name, env_id)
-      ::Role.find_by key: 'profile_#{name}', environment_id: env_id
+      ::Role.find_by key: "profile_#{name}", environment_id: env_id
     end
   end
 


=====================================
config/initializers/action_tracker.rb
=====================================
--- a/config/initializers/action_tracker.rb
+++ b/config/initializers/action_tracker.rb
@@ -1,5 +1,7 @@
 require 'noosfero/i18n'
 require 'action_tracker_config'
+require 'notify_activity_to_profiles_job'
+require 'activities_counter_cache_job'
 
 # ActionTracker plugin stuff
 


=====================================
config/initializers/dependencies.rb
=====================================
--- a/config/initializers/dependencies.rb
+++ b/config/initializers/dependencies.rb
@@ -25,6 +25,6 @@ require 'set_profile_region_from_city_state'
 require 'authenticated_system'
 require 'needs_profile'
 require 'white_list_filter'
-require 'notify_activity_to_profiles_job'
 require 'profile_suggestions_job'
+require 'scope_tool'
 


=====================================
db/migrate/069_add_enviroment_id_to_role.rb
=====================================
--- a/db/migrate/069_add_enviroment_id_to_role.rb
+++ b/db/migrate/069_add_enviroment_id_to_role.rb
@@ -18,10 +18,10 @@ class AddEnviromentIdToRole < ActiveRecord::Migration
         re = RoleWithEnvironment.new(role.attributes)
         re.environment = env
         re.save
-        RoleAssignment.find_all_by_role_id(role.id).select{|ra| ra.resource && (ra.resource.kind_of?(Profile) ? ra.resource.environment_id : ra.resource.id) == env.id }.each do |ra|
-          ra.role_id = re.id
-          ra.save
-        end
+        RoleAssignment \
+          .where(role_id: role.id)
+          .select{ |ra| ra.resource && (ra.resource.kind_of?(Profile) ? ra.resource.environment_id : ra.resource.id) == env.id }
+          .each{ |ra| ra.role_id = re.id; ra.save }
       end
     end
     roles.each(&:destroy)
@@ -37,7 +37,7 @@ class AddEnviromentIdToRole < ActiveRecord::Migration
         roles_by_name[role.name] ||= roles_by_key[role.key] ||= role
       end
       role = roles_by_name[re.name] ||= Role.create(re.attributes) unless role
-      RoleAssignment.find_all_by_role_id(re.id).each do |ra|
+      RoleAssignment.where(role_id: re.id).each do |ra|
         ra.role_id = role.id
         ra.save
       end


=====================================
db/migrate/20100730141134_set_owner_environment_to_enterprises_environment.rb
=====================================
--- a/db/migrate/20100730141134_set_owner_environment_to_enterprises_environment.rb
+++ b/db/migrate/20100730141134_set_owner_environment_to_enterprises_environment.rb
@@ -1,6 +1,6 @@
 class SetOwnerEnvironmentToEnterprisesEnvironment < ActiveRecord::Migration
   def self.up
-    CreateEnterprise.find_all_by_status(3).each do |t|
+    CreateEnterprise.where(status: 3).each do |t|
       if(Enterprise.find_by(identifier: t.data[:identifier]))
         update("UPDATE profiles SET environment_id = '%s' WHERE identifier = '%s'" %
               [Person.find(t.requestor_id).environment.id, t.data[:identifier]])


=====================================
lib/delayed_attachment_fu.rb
=====================================
--- a/lib/delayed_attachment_fu.rb
+++ b/lib/delayed_attachment_fu.rb
@@ -1,3 +1,5 @@
+require 'create_thumbnails_job'
+
 module DelayedAttachmentFu
 
   module ClassMethods


=====================================
lib/postgresql_attachment_fu.rb
=====================================
--- a/lib/postgresql_attachment_fu.rb
+++ b/lib/postgresql_attachment_fu.rb
@@ -2,18 +2,16 @@ module PostgresqlAttachmentFu
 
   module ClassMethods
     def postgresql_attachment_fu
-      send :include, InstanceMethods
+      include InstanceMethods
     end
   end
 
   module InstanceMethods
     def full_filename(thumbnail = nil)
       file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:path_prefix].to_s
-      file_system_path = File.join(file_system_path, ActiveRecord::Base.connection.schema_search_path) if ActiveRecord::Base.postgresql? and Noosfero::MultiTenancy.on?
+      file_system_path = File.join(file_system_path, ActiveRecord::Base.connection.schema_search_path) if ApplicationRecord.postgresql? and Noosfero::MultiTenancy.on?
       Rails.root.join(file_system_path, *partitioned_path(thumbnail_name_for(thumbnail))).to_s
     end
   end
 
 end
-
-ActiveRecord::Base.send(:extend, PostgresqlAttachmentFu::ClassMethods)


=====================================
plugins/newsletter/controllers/newsletter_plugin_admin_controller.rb
=====================================
--- a/plugins/newsletter/controllers/newsletter_plugin_admin_controller.rb
+++ b/plugins/newsletter/controllers/newsletter_plugin_admin_controller.rb
@@ -26,7 +26,7 @@ class NewsletterPluginAdminController < PluginAdminController
       end
     end
 
-    @blogs = Blog.includes(:profile).find_all_by_id(@newsletter.blog_ids)
+    @blogs = Blog.includes(:profile).where(id: @newsletter.blog_ids)
   end
 
   #TODO: Make this query faster


=====================================
plugins/stoa/lib/ext/profile_suggestion.rb
=====================================
--- a/plugins/stoa/lib/ext/profile_suggestion.rb
+++ b/plugins/stoa/lib/ext/profile_suggestion.rb
@@ -14,7 +14,7 @@ class ProfileSuggestion
     StoaPlugin::UspAlunoTurmaGrad.classrooms_from_person(usp_id).each do |classroom|
       person_attempts += 1
       return unless person.profile_suggestions.count < N_SUGGESTIONS && person_attempts < MAX_ATTEMPTS
-      StoaPlugin::UspAlunoTurmaGrad.find_all_by_codtur(classroom.codtur).each do |same_class|
+      StoaPlugin::UspAlunoTurmaGrad.where(codtur: classroom.codtur).each do |same_class|
         classmate = Person.find_by_usp_id(same_class.codpes)
         unless classmate.nil? || classmate == person || classmate.is_a_friend?(person) || person.already_request_friendship?(classmate)
           suggestion = person.profile_suggestions.find_or_initialize_by_suggestion_id(classmate.id)


=====================================
plugins/stoa/lib/stoa_plugin/usp_aluno_turma_grad.rb
=====================================
--- a/plugins/stoa/lib/stoa_plugin/usp_aluno_turma_grad.rb
+++ b/plugins/stoa/lib/stoa_plugin/usp_aluno_turma_grad.rb
@@ -9,7 +9,7 @@ class StoaPlugin::UspAlunoTurmaGrad < ApplicationRecord
   end
 
   def self.classrooms_from_person(usp_id)
-    StoaPlugin::UspAlunoTurmaGrad.find_all_by_codpes(usp_id)
+    StoaPlugin::UspAlunoTurmaGrad.where(codpes: usp_id)
   end
 
 end


=====================================
test/functional/profile_members_controller_test.rb
=====================================
--- a/test/functional/profile_members_controller_test.rb
+++ b/test/functional/profile_members_controller_test.rb
@@ -225,7 +225,7 @@ class ProfileMembersControllerTest < ActionController::TestCase
     u = create_user('member_wannabe').person
     post :add_member, :profile => ent.identifier, :id => u.id
 
-    assert_equivalent Profile::Roles.all_roles(ent.environment).compact, u.role_assignments.find_all_by_resource_id(ent.id).map(&:role).compact
+    assert_equivalent Profile::Roles.all_roles(ent.environment).compact, u.role_assignments.where(resource_id: ent.id).map(&:role).compact
   end
 
   should 'not add member to community' do


=====================================
test/unit/article_categorization_test.rb
=====================================
--- a/test/unit/article_categorization_test.rb
+++ b/test/unit/article_categorization_test.rb
@@ -32,7 +32,7 @@ class ArticleCategorizationTest < ActiveSupport::TestCase
       ArticleCategorization.add_category_to_article(c2, a)
     end
 
-    assert_equal 2, ArticleCategorization.find_all_by_article_id(a.id).size
+    assert_equal 2, ArticleCategorization.where(article_id: a.id).size
   end
 
   should 'not duplicate entry for category that is parent of two others' do


=====================================
test/unit/article_test.rb
=====================================
--- a/test/unit/article_test.rb
+++ b/test/unit/article_test.rb
@@ -597,7 +597,7 @@ class ArticleTest < ActiveSupport::TestCase
     a.name = 'new-name'
     a.save!
 
-    page = Article.find_by(old_path: old_path)
+    page = Article.find_by_old_path old_path
 
     assert_equal a.path, page.path
   end
@@ -610,7 +610,7 @@ class ArticleTest < ActiveSupport::TestCase
     a1.save!
     a2 = p.articles.create!(:name => 'old-name')
 
-    page = Article.find_by(old_path: old_path)
+    page = Article.find_by_old_path old_path
 
     assert_equal a2.path, page.path
   end
@@ -625,7 +625,7 @@ class ArticleTest < ActiveSupport::TestCase
     a2.name = 'other-new-name'
     a2.save!
 
-    page = Article.find_by(old_path: old_path)
+    page = Article.find_by_old_path old_path
 
     assert_equal a2.path, page.path
   end
@@ -639,7 +639,7 @@ class ArticleTest < ActiveSupport::TestCase
 
     p2 = create_user('another_user').person
 
-    page = p2.articles.find_by(old_path: old_path)
+    page = p2.articles.find_by_old_path old_path
 
     assert_nil page
   end
@@ -988,8 +988,8 @@ class ArticleTest < ActiveSupport::TestCase
     activity = article.activity
 
     process_delayed_job_queue
-    assert_equal 3, ActionTrackerNotification.find_all_by_action_tracker_id(activity.id).count
-    assert_equivalent [p1,p2,community], ActionTrackerNotification.find_all_by_action_tracker_id(activity.id).map(&:profile)
+    assert_equal 3, ActionTrackerNotification.where(action_tracker_id: activity.id).count
+    assert_equivalent [p1,p2,community], ActionTrackerNotification.where(action_tracker_id: activity.id).map(&:profile)
   end
 
   should 'destroy activity when a published article is removed' do
@@ -1090,17 +1090,17 @@ class ArticleTest < ActiveSupport::TestCase
     assert_equal [first_activity], ActionTracker::Record.where(verb: 'create_article')
 
     process_delayed_job_queue
-    assert_equal 2, ActionTrackerNotification.find_all_by_action_tracker_id(first_activity.id).count
+    assert_equal 2, ActionTrackerNotification.where(action_tracker_id: first_activity.id).count
 
     member_2 = fast_create(Person)
     community.add_member(member_2)
 
     article2 = create TinyMceArticle, :name => 'Tracked Article 2', :profile_id => community.id
     second_activity = article2.activity
-    assert_equivalent [first_activity, second_activity], ActionTracker::Record.find_all_by_verb('create_article')
+    assert_equivalent [first_activity, second_activity], ActionTracker::Record.where(verb: 'create_article').all
 
     process_delayed_job_queue
-    assert_equal 3, ActionTrackerNotification.find_all_by_action_tracker_id(second_activity.id).count
+    assert_equal 3, ActionTrackerNotification.where(action_tracker_id: second_activity.id).count
   end
 
   should 'create notifications to friends when creating an article' do
@@ -1122,16 +1122,16 @@ class ArticleTest < ActiveSupport::TestCase
 
     User.current = profile.user
     article = create TinyMceArticle, :name => 'Tracked Article 1', :profile_id => profile.id
-    assert_equal 1, ActionTracker::Record.find_all_by_verb('create_article').count
+    assert_equal 1, ActionTracker::Record.where(verb: 'create_article').count
     process_delayed_job_queue
-    assert_equal 2, ActionTrackerNotification.find_all_by_action_tracker_id(article.activity.id).count
+    assert_equal 2, ActionTrackerNotification.where(action_tracker_id: article.activity.id).count
 
     f2 = fast_create(Person)
     profile.add_friend(f2)
     article2 = create TinyMceArticle, :name => 'Tracked Article 2', :profile_id => profile.id
-    assert_equal 2, ActionTracker::Record.find_all_by_verb('create_article').count
+    assert_equal 2, ActionTracker::Record.where(verb: 'create_article').count
     process_delayed_job_queue
-    assert_equal 3, ActionTrackerNotification.find_all_by_action_tracker_id(article2.activity.id).count
+    assert_equal 3, ActionTrackerNotification.where(action_tracker_id: article2.activity.id).count
   end
 
   should 'destroy activity and notifications of friends when destroying an article' do
@@ -1145,7 +1145,7 @@ class ArticleTest < ActiveSupport::TestCase
     activity = article.activity
 
     process_delayed_job_queue
-    assert_equal 2, ActionTrackerNotification.find_all_by_action_tracker_id(activity.id).count
+    assert_equal 2, ActionTrackerNotification.where(action_tracker_id: activity.id).count
 
     assert_difference 'ActionTrackerNotification.count', -2 do
       article.destroy
@@ -1168,7 +1168,7 @@ class ArticleTest < ActiveSupport::TestCase
     activity = article.activity
 
     process_delayed_job_queue
-    assert_equal 3, ActionTrackerNotification.find_all_by_action_tracker_id(activity.id).count
+    assert_equal 3, ActionTrackerNotification.where(action_tracker_id: activity.id).count
 
     assert_difference 'ActionTrackerNotification.count', -3 do
       article.destroy


=====================================
test/unit/person_test.rb
=====================================
--- a/test/unit/person_test.rb
+++ b/test/unit/person_test.rb
@@ -1005,7 +1005,7 @@ class PersonTest < ActiveSupport::TestCase
 
   should 'track only one action when a person joins a community' do
     ActionTracker::Record.delete_all
-    p = create_user('test_user').person
+    p = (User.current=create_user('test_user')).person
     c = fast_create(Community, :name => "Foo")
     c.add_member(p)
     assert_equal ["Foo"], ActionTracker::Record.where(verb: 'join_community').last.get_resource_name
@@ -1283,7 +1283,7 @@ class PersonTest < ActiveSupport::TestCase
     profile = fast_create(Profile)
     profile.add_admin(admin)
 
-    assert admin.has_permission?('anything', profile), 'Admin does not have every permission!'
+    assert admin.has_permission?('manage_environment_users', profile), 'Admin does not have every permission!'
   end
 
   should 'grant every permission over profile for environment admin' do
@@ -1292,7 +1292,7 @@ class PersonTest < ActiveSupport::TestCase
     environment = profile.environment
     environment.add_admin(admin)
 
-    assert admin.has_permission?('anything', profile), 'Environment admin does not have every permission!'
+    assert admin.has_permission?('manage_environment_users', profile), 'Environment admin does not have every permission!'
   end
 
   should 'allow plugins to extend person\'s permission access' do


=====================================
test/unit/profile_categorization_test.rb
=====================================
--- a/test/unit/profile_categorization_test.rb
+++ b/test/unit/profile_categorization_test.rb
@@ -9,7 +9,7 @@ class ProfileCategorizationTest < ActiveSupport::TestCase
     person.save!
     assert_includes person.categories, cat
     assert_includes cat.people, person
-    assert_equal [cat.id], person.category_ids 
+    assert_equal [cat.id], person.category_ids
   end
 
   should 'create instances for the entire hierarchy' do
@@ -24,7 +24,7 @@ class ProfileCategorizationTest < ActiveSupport::TestCase
       ProfileCategorization.add_category_to_profile(c2, p)
     end
 
-    assert_equal 2, ProfileCategorization.find_all_by_profile_id(p.id).size
+    assert_equal 2, ProfileCategorization.where(profile_id: p.id).size
   end
 
   should 'not duplicate entry for category that is parent of two others' do


=====================================
test/unit/profile_test.rb
=====================================
--- a/test/unit/profile_test.rb
+++ b/test/unit/profile_test.rb
@@ -2133,7 +2133,7 @@ class ProfileTest < ActiveSupport::TestCase
     suggested_person = fast_create(Person)
     suggestion = ProfileSuggestion.create(:person => person, :suggestion => suggested_person, :enabled => true)
 
-    assert_difference 'ProfileSuggestion.find_all_by_suggestion_id(suggested_person.id).count', -1 do
+    assert_difference 'ProfileSuggestion.where(suggestion_id: suggested_person.id).count', -1 do
       suggested_person.destroy
     end
   end


=====================================
test/unit/uploaded_file_test.rb
=====================================
--- a/test/unit/uploaded_file_test.rb
+++ b/test/unit/uploaded_file_test.rb
@@ -329,10 +329,10 @@ class UploadedFileTest < ActiveSupport::TestCase
     gallery = fast_create(Gallery, :profile_id => profile.id)
 
     image1 = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :parent => gallery, :profile => profile)
-    assert_equal 1, ActionTracker::Record.find_all_by_verb('upload_image').count
+    assert_equal 1, ActionTracker::Record.where(verb: 'upload_image').count
 
     image2 = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/other-pic.jpg', 'image/jpg'), :parent => gallery, :profile => profile)
-    assert_equal 1, ActionTracker::Record.find_all_by_verb('upload_image').count
+    assert_equal 1, ActionTracker::Record.where(verb: 'upload_image').count
   end
 
   {


=====================================
vendor/plugins/access_control/lib/acts_as_accessible.rb
=====================================
--- a/vendor/plugins/access_control/lib/acts_as_accessible.rb
+++ b/vendor/plugins/access_control/lib/acts_as_accessible.rb
@@ -32,7 +32,7 @@ module ActsAsAccessible
   end
 
   def roles
-    Role.find_all_by_environment_id(environment.id).select do |r|
+    Role.where(environment_id: environment.id).select do |r|
       r.permissions.any?{ |p| PERMISSIONS[self.class.base_class.name].include?(p) }
     end
   end



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/3d3cf43b785281b9b0b71f8ce7bfaf4e9fe5e512...3b190c3fbcf8fdea82bdc5b029815c5b1654d3d4
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20160221/b07b51a3/attachment-0001.html>


More information about the Noosfero-dev mailing list