Commit 3c57da65848bab5ec368918d734594d0e1935c49

Authored by Braulio Bhavamitra
1 parent 7cb73057

rails4: fix a bunch of unit tests

@@ -46,6 +46,7 @@ group :test do @@ -46,6 +46,7 @@ group :test do
46 gem 'rspec', '~> 2.14.0' 46 gem 'rspec', '~> 2.14.0'
47 gem 'rspec-rails', '~> 2.14.1' 47 gem 'rspec-rails', '~> 2.14.1'
48 gem 'mocha', '~> 1.1.0', :require => false 48 gem 'mocha', '~> 1.1.0', :require => false
  49 + gem 'spring'
49 gem 'test-unit' if RUBY_VERSION >= '2.2.0' 50 gem 'test-unit' if RUBY_VERSION >= '2.2.0'
50 gem 'minitest' 51 gem 'minitest'
51 gem 'minitest-reporters' 52 gem 'minitest-reporters'
app/helpers/folder_helper.rb
@@ -6,8 +6,7 @@ module FolderHelper @@ -6,8 +6,7 @@ module FolderHelper
6 configure[:recursive] ||= false 6 configure[:recursive] ||= false
7 configure[:list_type] ||= :folder 7 configure[:list_type] ||= :folder
8 if !configure[:contents].blank? 8 if !configure[:contents].blank?
9 - configure[:contents] = configure[:contents].paginate(  
10 - :order => "name ASC", 9 + configure[:contents] = configure[:contents].order('name ASC').paginate(
11 :per_page => 30, 10 :per_page => 30,
12 :page => params[:npage] 11 :page => params[:npage]
13 ) 12 )
app/models/add_member.rb
@@ -8,7 +8,7 @@ class AddMember < Task @@ -8,7 +8,7 @@ class AddMember < Task
8 alias :organization :target 8 alias :organization :target
9 alias :organization= :target= 9 alias :organization= :target=
10 10
11 - settings_items :roles 11 + settings_items :roles, type: Array
12 12
13 after_create do |task| 13 after_create do |task|
14 remove_from_suggestion_list(task) 14 remove_from_suggestion_list(task)
app/models/blog_archives_block.rb
@@ -33,10 +33,10 @@ class BlogArchivesBlock < Block @@ -33,10 +33,10 @@ class BlogArchivesBlock < Block
33 results = '' 33 results = ''
34 posts = visible_posts(args[:person]) 34 posts = visible_posts(args[:person])
35 posts.except(:order).count(:all, :group => 'EXTRACT(YEAR FROM published_at)').sort_by {|year, count| -year.to_i}.each do |year, count| 35 posts.except(:order).count(:all, :group => 'EXTRACT(YEAR FROM published_at)').sort_by {|year, count| -year.to_i}.each do |year, count|
36 - results << content_tag('li', content_tag('strong', "#{year} (#{count})"))  
37 - results << "<ul class='#{year}-archive'>"  
38 - posts.except(:order).where('EXTRACT(YEAR FROM published_at)=?', year).group('EXTRACT(MONTH FROM published_at)').count.sort_by {|month, count| -month.to_i}.each do |month, count|  
39 - results << content_tag('li', link_to("#{month_name(month.to_i)} (#{count})", owner_blog.url.merge(:year => year, :month => month))) 36 + results << content_tag('li', content_tag('strong', "#{year.to_i} (#{count})"))
  37 + results << "<ul class='#{year.to_i}-archive'>"
  38 + posts.except(:order).where('EXTRACT(YEAR FROM published_at)=?', year.to_i).group('EXTRACT(MONTH FROM published_at)').count.sort_by {|month, count| -month.to_i}.each do |month, count|
  39 + results << content_tag('li', link_to("#{month_name(month.to_i)} (#{count})", owner_blog.url.merge(year: year.to_i, month: month.to_i)))
40 end 40 end
41 results << "</ul>" 41 results << "</ul>"
42 end 42 end
app/models/category.rb
@@ -53,19 +53,19 @@ class Category &lt; ActiveRecord::Base @@ -53,19 +53,19 @@ class Category &lt; ActiveRecord::Base
53 } 53 }
54 54
55 def recent_people(limit = 10) 55 def recent_people(limit = 10)
56 - self.people.paginate(:order => 'created_at DESC, id DESC', :page => 1, :per_page => limit) 56 + self.people.reorder('created_at DESC, id DESC').paginate(page: 1, per_page: limit)
57 end 57 end
58 58
59 def recent_enterprises(limit = 10) 59 def recent_enterprises(limit = 10)
60 - self.enterprises.paginate(:order => 'created_at DESC, id DESC', :page => 1, :per_page => limit) 60 + self.enterprises.reorder('created_at DESC, id DESC').paginate(page: 1, per_page: limit)
61 end 61 end
62 62
63 def recent_communities(limit = 10) 63 def recent_communities(limit = 10)
64 - self.communities.paginate(:order => 'created_at DESC, id DESC', :page => 1, :per_page => limit) 64 + self.communities.reorder('created_at DESC, id DESC').paginate(page: 1, per_page: limit)
65 end 65 end
66 66
67 def recent_products(limit = 10) 67 def recent_products(limit = 10)
68 - self.products.paginate(:order => 'created_at DESC, id DESC', :page => 1, :per_page => limit) 68 + self.products.reorder('created_at DESC, id DESC').paginate(page: 1, per_page: limit)
69 end 69 end
70 70
71 def recent_articles(limit = 10) 71 def recent_articles(limit = 10)
@@ -73,7 +73,7 @@ class Category &lt; ActiveRecord::Base @@ -73,7 +73,7 @@ class Category &lt; ActiveRecord::Base
73 end 73 end
74 74
75 def recent_comments(limit = 10) 75 def recent_comments(limit = 10)
76 - comments.paginate(:order => 'created_at DESC, comments.id DESC', :page => 1, :per_page => limit) 76 + self.comments.reorder('created_at DESC, comments.id DESC').paginate(page: 1, per_page: limit)
77 end 77 end
78 78
79 def most_commented_articles(limit = 10) 79 def most_commented_articles(limit = 10)
@@ -81,7 +81,7 @@ class Category &lt; ActiveRecord::Base @@ -81,7 +81,7 @@ class Category &lt; ActiveRecord::Base
81 end 81 end
82 82
83 def upcoming_events(limit = 10) 83 def upcoming_events(limit = 10)
84 - self.events.where('start_date >= ?', Date.today).order('start_date').paginate(:page => 1, :per_page => limit) 84 + self.events.where('start_date >= ?', Date.today).reorder('start_date').paginate(:page => 1, :per_page => limit)
85 end 85 end
86 86
87 def display_in_menu? 87 def display_in_menu?
@@ -90,8 +90,8 @@ class Category &lt; ActiveRecord::Base @@ -90,8 +90,8 @@ class Category &lt; ActiveRecord::Base
90 90
91 def children_for_menu 91 def children_for_menu
92 results = [] 92 results = []
93 - pending = children.where :display_in_menu => true  
94 - while !pending.empty? 93 + pending = children.where(display_in_menu: true).all
  94 + while pending.present?
95 cat = pending.shift 95 cat = pending.shift
96 results << cat 96 results << cat
97 pending += cat.children.where :display_in_menu => true 97 pending += cat.children.where :display_in_menu => true
app/models/environment.rb
@@ -942,7 +942,7 @@ class Environment &lt; ActiveRecord::Base @@ -942,7 +942,7 @@ class Environment &lt; ActiveRecord::Base
942 end 942 end
943 943
944 def highlighted_products_with_image(options = {}) 944 def highlighted_products_with_image(options = {})
945 - Product.where(highlighted: true, profile_id: self.enterprises.where(select: :id)).joins(:image) 945 + self.products.where(highlighted: true).joins(:image)
946 end 946 end
947 947
948 settings_items :home_cache_in_minutes, :type => :integer, :default => 5 948 settings_items :home_cache_in_minutes, :type => :integer, :default => 5
app/models/event.rb
@@ -12,14 +12,14 @@ class Event &lt; Article @@ -12,14 +12,14 @@ class Event &lt; Article
12 settings_items :address, :type => :string 12 settings_items :address, :type => :string
13 13
14 def link=(value) 14 def link=(value)
15 - self.setting[:link] = maybe_add_http(value) 15 + self.setting[:link] = maybe_add_http(URI.escape value.to_s)
16 end 16 end
17 17
18 def link 18 def link
19 maybe_add_http(self.setting[:link]) 19 maybe_add_http(self.setting[:link])
20 end 20 end
21 21
22 - xss_terminate :only => [ :name, :body, :link, :address ], :with => 'white_list', :on => 'validation' 22 + xss_terminate :only => [ :name, :body, :address ], :with => 'white_list', :on => 'validation'
23 23
24 def initialize(*args) 24 def initialize(*args)
25 super(*args) 25 super(*args)
app/models/featured_products_block.rb
@@ -11,7 +11,7 @@ class FeaturedProductsBlock &lt; Block @@ -11,7 +11,7 @@ class FeaturedProductsBlock &lt; Block
11 if block.owner.kind_of?(Environment) && block.product_ids.blank? 11 if block.owner.kind_of?(Environment) && block.product_ids.blank?
12 total = block.owner.products.count 12 total = block.owner.products.count
13 offset = rand([(total - block.groups_of * 3) + 1, 1].max) 13 offset = rand([(total - block.groups_of * 3) + 1, 1].max)
14 - block.product_ids = block.owner.highlighted_products_with_image(:offset => offset, :limit => block.groups_of * 3).map(&:id) 14 + block.product_ids = block.owner.highlighted_products_with_image.offset(offset).limit(block.groups_of * 3).map(&:id)
15 end 15 end
16 block.groups_of = block.groups_of.to_i 16 block.groups_of = block.groups_of.to_i
17 end 17 end
app/models/profile_suggestion.rb
@@ -136,7 +136,7 @@ class ProfileSuggestion &lt; ActiveRecord::Base @@ -136,7 +136,7 @@ class ProfileSuggestion &lt; ActiveRecord::Base
136 suggestion.send("#{rule}=", value) 136 suggestion.send("#{rule}=", value)
137 connections.each do |connection_id| 137 connections.each do |connection_id|
138 next if SuggestionConnection.where(:suggestion_id => suggestion.id, :connection_id => connection_id, :connection_type => options[:connection]).present? 138 next if SuggestionConnection.where(:suggestion_id => suggestion.id, :connection_id => connection_id, :connection_type => options[:connection]).present?
139 - SuggestionConnection.create!(:suggestion => suggestion, :connection_id => connection_id, :connection_type => options[:connection]) 139 + SuggestionConnection.create!(:suggestion => suggestion, :connection_id => connection_id, :connection_type => options[:connection])
140 end 140 end
141 suggestion.score += value * options[:weight] 141 suggestion.score += value * options[:weight]
142 end 142 end
app/models/suggestion_connection.rb
1 class SuggestionConnection < ActiveRecord::Base 1 class SuggestionConnection < ActiveRecord::Base
2 - attr_accessible :suggestion, :connection_type, :connection_id 2 + attr_accessible :suggestion, :suggestion_id, :connection_type, :connection_id
3 3
4 belongs_to :suggestion, :class_name => 'ProfileSuggestion', :foreign_key => 'suggestion_id' 4 belongs_to :suggestion, :class_name => 'ProfileSuggestion', :foreign_key => 'suggestion_id'
5 belongs_to :connection, :polymorphic => true 5 belongs_to :connection, :polymorphic => true
config/initializers/dependencies.rb
  1 +require 'pp'
  2 +
1 # third-party libraries 3 # third-party libraries
2 require 'will_paginate' 4 require 'will_paginate'
3 require 'will_paginate/array' 5 require 'will_paginate/array'
lib/acts_as_having_settings.rb
@@ -8,12 +8,12 @@ module ActiveRecord @@ -8,12 +8,12 @@ module ActiveRecord
8 end 8 end
9 class Array < Value 9 class Array < Value
10 def cast_value value 10 def cast_value value
11 - Array(value) 11 + ::Array.wrap(value)
12 end 12 end
13 end 13 end
14 class Hash < Value 14 class Hash < Value
15 def cast_value value 15 def cast_value value
16 - Hash[value] 16 + ::Hash[value]
17 end 17 end
18 end 18 end
19 end 19 end
test/test_helper.rb
@@ -139,18 +139,16 @@ class ActiveSupport::TestCase @@ -139,18 +139,16 @@ class ActiveSupport::TestCase
139 assert !text.index('<'), "Text '#{text}' expected to be sanitized" 139 assert !text.index('<'), "Text '#{text}' expected to be sanitized"
140 end 140 end
141 141
142 - def find_tag_in_string text, options  
143 - doc = Nokogiri::HTML.fragment text  
144 - tag = doc.css("#{options[:tag]}#{options[:attributes].map{ |a, v| "[#{a}=\"#{v}\"]" }.join}").first  
145 - tag  
146 - end  
147 -  
148 def assert_tag_in_string(text, options) 142 def assert_tag_in_string(text, options)
149 - assert find_tag_in_string(text, options), "expected tag #{options.inspect}, but not found in #{text.inspect}" 143 + doc = HTML::Document.new(text, false, false)
  144 + tag = doc.find(options)
  145 + assert tag, "expected tag #{options.inspect}, but not found in #{text.inspect}"
150 end 146 end
151 147
152 def assert_no_tag_in_string(text, options) 148 def assert_no_tag_in_string(text, options)
153 - assert !find_tag_in_string(text, options), "expected no tag #{options.inspect}, but tag found in #{text.inspect}" 149 + doc = HTML::Document.new(text, false, false)
  150 + tag = doc.find(options)
  151 + assert !tag, "expected no tag #{options.inspect}, but tag found in #{text.inspect}"
154 end 152 end
155 153
156 def assert_order(reference, original) 154 def assert_order(reference, original)
test/unit/catalog_helper_test.rb
@@ -6,7 +6,12 @@ class CatalogHelperTest &lt; ActiveSupport::TestCase @@ -6,7 +6,12 @@ class CatalogHelperTest &lt; ActiveSupport::TestCase
6 include ActionView::Helpers::TextHelper 6 include ActionView::Helpers::TextHelper
7 include ActionView::Helpers::UrlHelper 7 include ActionView::Helpers::UrlHelper
8 include ActionView::Helpers::TagHelper 8 include ActionView::Helpers::TagHelper
  9 +
9 include ::Rails::Dom::Testing::Assertions::SelectorAssertions 10 include ::Rails::Dom::Testing::Assertions::SelectorAssertions
  11 + # see http://blog.cynthiakiser.com/blog/2014/12/26/upgrading-from-rails-4-dot-1-8-to-4-dot-2-0/
  12 + def document_root_element
  13 + @doc.root
  14 + end
10 15
11 def url_for(opts) 16 def url_for(opts)
12 #{:controller => 'catalog', :action => 'index', :level => category.id} 17 #{:controller => 'catalog', :action => 'index', :level => category.id}
@@ -44,7 +49,7 @@ class CatalogHelperTest &lt; ActiveSupport::TestCase @@ -44,7 +49,7 @@ class CatalogHelperTest &lt; ActiveSupport::TestCase
44 49
45 html = category_with_sub_list @products 50 html = category_with_sub_list @products
46 51
47 - doc = HTML::Document.new "<body>#{html}</body>" 52 + @doc = doc = HTML::Document.new "<body>#{html}</body>"
48 assert_select doc.root, 'div' do |divs| 53 assert_select doc.root, 'div' do |divs|
49 assert_select divs[0], "a[href=catalog-index-level=#{@products.id}]" 54 assert_select divs[0], "a[href=catalog-index-level=#{@products.id}]"
50 assert_select divs[0], '.count', {:text=>'3'} 55 assert_select divs[0], '.count', {:text=>'3'}
test/unit/change_password_test.rb
@@ -17,8 +17,7 @@ class ChangePasswordTest &lt; ActiveSupport::TestCase @@ -17,8 +17,7 @@ class ChangePasswordTest &lt; ActiveSupport::TestCase
17 change.password = 'right' 17 change.password = 'right'
18 change.password_confirmation = 'wrong' 18 change.password_confirmation = 'wrong'
19 assert !change.valid? 19 assert !change.valid?
20 - assert change.errors[:password.to_s].present?  
21 - 20 + assert change.errors[:password_confirmation].present?
22 21
23 change.password_confirmation = 'right' 22 change.password_confirmation = 'right'
24 assert change.valid? 23 assert change.valid?
test/unit/event_test.rb
@@ -285,11 +285,11 @@ class EventTest &lt; ActiveSupport::TestCase @@ -285,11 +285,11 @@ class EventTest &lt; ActiveSupport::TestCase
285 should 'filter fields with white_list filter' do 285 should 'filter fields with white_list filter' do
286 event = Event.new 286 event = Event.new
287 event.body = "<h1> Description </h1>" 287 event.body = "<h1> Description </h1>"
288 - event.address = "<strong> Address <strong>" 288 + event.address = "<strong> Address </strong>"
289 event.valid? 289 event.valid?
290 290
291 assert_equal "<h1> Description </h1>", event.body 291 assert_equal "<h1> Description </h1>", event.body
292 - assert_equal "<strong> Address <strong>", event.address 292 + assert_equal "<strong> Address </strong>", event.address
293 end 293 end
294 294
295 should 'not filter & on link field' do 295 should 'not filter & on link field' do
@@ -306,8 +306,8 @@ class EventTest &lt; ActiveSupport::TestCase @@ -306,8 +306,8 @@ class EventTest &lt; ActiveSupport::TestCase
306 event.address = "<strong>><< Address <strong>" 306 event.address = "<strong>><< Address <strong>"
307 event.valid? 307 event.valid?
308 308
309 - assert_no_match /[<>]/, event.body  
310 - assert_no_match /[<>]/, event.address 309 + assert_match /<h1>&gt;\/h1&gt;<\/h1>/, event.body
  310 + assert_match /<strong>&gt;<\/strong>/, event.address
311 end 311 end
312 312
313 should 'not sanitize html comments' do 313 should 'not sanitize html comments' do
@@ -316,8 +316,8 @@ class EventTest &lt; ActiveSupport::TestCase @@ -316,8 +316,8 @@ class EventTest &lt; ActiveSupport::TestCase
316 event.address = '<p><!-- <asdf> << aasdfa >>> --> <h1> Wellformed html code </h1>' 316 event.address = '<p><!-- <asdf> << aasdfa >>> --> <h1> Wellformed html code </h1>'
317 event.valid? 317 event.valid?
318 318
319 - assert_match /<!-- .* --> <h1> Wellformed html code <\/h1>/, event.body  
320 - assert_match /<!-- .* --> <h1> Wellformed html code <\/h1>/, event.address 319 + assert_match /<p><!-- .* --> <\/p><h1> Wellformed html code <\/h1>/, event.body
  320 + assert_match /<p><!-- .* --> <\/p><h1> Wellformed html code <\/h1>/, event.address
321 end 321 end
322 322
323 should 'be translatable' do 323 should 'be translatable' do
test/unit/folder_test.rb
@@ -129,7 +129,7 @@ class FolderTest &lt; ActiveSupport::TestCase @@ -129,7 +129,7 @@ class FolderTest &lt; ActiveSupport::TestCase
129 folder.body = '<p><!-- <asdf> << aasdfa >>> --> <h1> Wellformed html code </h1>' 129 folder.body = '<p><!-- <asdf> << aasdfa >>> --> <h1> Wellformed html code </h1>'
130 folder.valid? 130 folder.valid?
131 131
132 - assert_match /<!-- .* --> <h1> Wellformed html code <\/h1>/, folder.body 132 + assert_match /<p><!-- .* --> <\/p><h1> Wellformed html code <\/h1>/, folder.body
133 end 133 end
134 134
135 should 'escape malformed html tags' do 135 should 'escape malformed html tags' do
@@ -137,7 +137,7 @@ class FolderTest &lt; ActiveSupport::TestCase @@ -137,7 +137,7 @@ class FolderTest &lt; ActiveSupport::TestCase
137 folder.body = "<h1<< Description >>/h1>" 137 folder.body = "<h1<< Description >>/h1>"
138 folder.valid? 138 folder.valid?
139 139
140 - assert_no_match /[<>]/, folder.body 140 + assert_match /<h1>&gt;\/h1&gt;<\/h1>/, folder.body
141 end 141 end
142 142
143 should 'not have a blog as parent' do 143 should 'not have a blog as parent' do
test/unit/gallery_test.rb
@@ -130,7 +130,7 @@ class GalleryTest &lt; ActiveSupport::TestCase @@ -130,7 +130,7 @@ class GalleryTest &lt; ActiveSupport::TestCase
130 gallery.body = '<p><!-- <asdf> << aasdfa >>> --> <h1> Wellformed html code </h1>' 130 gallery.body = '<p><!-- <asdf> << aasdfa >>> --> <h1> Wellformed html code </h1>'
131 gallery.valid? 131 gallery.valid?
132 132
133 - assert_match /<!-- .* --> <h1> Wellformed html code <\/h1>/, gallery.body 133 + assert_match /<p><!-- .* --> <\/p><h1> Wellformed html code <\/h1>/, gallery.body
134 end 134 end
135 135
136 should 'escape malformed html tags' do 136 should 'escape malformed html tags' do
@@ -138,7 +138,7 @@ class GalleryTest &lt; ActiveSupport::TestCase @@ -138,7 +138,7 @@ class GalleryTest &lt; ActiveSupport::TestCase
138 gallery.body = "<h1<< Description >>/h1>" 138 gallery.body = "<h1<< Description >>/h1>"
139 gallery.valid? 139 gallery.valid?
140 140
141 - assert_no_match /[<>]/, gallery.body 141 + assert_match /<h1>&gt;\/h1&gt;<\/h1>/, gallery.body
142 end 142 end
143 143
144 should 'accept uploads' do 144 should 'accept uploads' do
test/unit/macros_helper_test.rb
@@ -102,8 +102,7 @@ class MacrosHelperTest &lt; ActionView::TestCase @@ -102,8 +102,7 @@ class MacrosHelperTest &lt; ActionView::TestCase
102 {:js_files => 'macro.js' } 102 {:js_files => 'macro.js' }
103 end 103 end
104 end 104 end
105 - ActionView::Helpers::AssetTagHelper::JavascriptIncludeTag.any_instance.stubs('asset_file_path!')  
106 - assert_equal "<script src=\"#{Plugin1.public_path('macro.js')}\" type=\"text/javascript\"></script>", include_macro_js_files 105 + assert_equal "<script src=\"#{Plugin1.public_path('macro.js')}\"></script>", include_macro_js_files
107 end 106 end
108 107
109 should 'get macro css files' do 108 should 'get macro css files' do
test/unit/profile_suggestion_test.rb
1 # encoding: UTF-8 1 # encoding: UTF-8
2 -require File.dirname(__FILE__) + '/../test_helper' 2 +require 'test_helper'
3 3
4 class ProfileSuggestionTest < ActiveSupport::TestCase 4 class ProfileSuggestionTest < ActiveSupport::TestCase
5 5
vendor/plugins/kandadaboggu-vote_fu/lib/acts_as_voteable.rb
@@ -8,13 +8,13 @@ module Juixe @@ -8,13 +8,13 @@ module Juixe
8 end 8 end
9 9
10 module ClassMethods 10 module ClassMethods
11 - # 11 + #
12 # Options: 12 # Options:
13 - # :vote_counter 13 + # :vote_counter
14 # Model stores the sum of votes in the vote counter column when the value is true. This requires a column named `vote_total` in the table corresponding to `voteable` model. 14 # Model stores the sum of votes in the vote counter column when the value is true. This requires a column named `vote_total` in the table corresponding to `voteable` model.
15 - # You can also specify a custom vote counter column by providing a column name instead of a true/false value to this option (e.g., :vote_counter => :my_custom_counter.) 15 + # You can also specify a custom vote counter column by providing a column name instead of a true/false value to this option (e.g., :vote_counter => :my_custom_counter.)
16 # Note: Specifying a counter will add it to that model‘s list of readonly attributes using attr_readonly. 16 # Note: Specifying a counter will add it to that model‘s list of readonly attributes using attr_readonly.
17 - # 17 + #
18 def acts_as_voteable options={} 18 def acts_as_voteable options={}
19 has_many :votes, :as => :voteable, :dependent => :destroy 19 has_many :votes, :as => :voteable, :dependent => :destroy
20 include Juixe::Acts::Voteable::InstanceMethods 20 include Juixe::Acts::Voteable::InstanceMethods
@@ -28,15 +28,15 @@ module Juixe @@ -28,15 +28,15 @@ module Juixe
28 def self.vote_counter_column # def self.vote_counter_column 28 def self.vote_counter_column # def self.vote_counter_column
29 :"#{counter_column_name}" # :vote_total 29 :"#{counter_column_name}" # :vote_total
30 end # end 30 end # end
31 - def vote_counter_column  
32 - self.class.vote_counter_column  
33 - end 31 + def vote_counter_column
  32 + self.class.vote_counter_column
  33 + end
34 EOS 34 EOS
35 - 35 +
36 define_method(:reload_vote_counter) {reload(:select => vote_counter_column.to_s)} 36 define_method(:reload_vote_counter) {reload(:select => vote_counter_column.to_s)}
37 attr_readonly counter_column_name 37 attr_readonly counter_column_name
38 end 38 end
39 - end 39 + end
40 end 40 end
41 41
42 # This module contains class methods Vote class 42 # This module contains class methods Vote class
@@ -49,13 +49,13 @@ module Juixe @@ -49,13 +49,13 @@ module Juixe
49 49
50 def update_vote_counters direction 50 def update_vote_counters direction
51 klass, vtbl = self.voteable.class, self.voteable 51 klass, vtbl = self.voteable.class, self.voteable
52 - klass.update_counters(vtbl.id, vtbl.vote_counter_column.to_sym => (self.vote * direction) ) if self.vote_counters.any?{|c| c == klass} 52 + klass.update_counters(vtbl.id, vtbl.vote_counter_column.to_sym => (self.vote * direction) ) if self.vote_counters.any?{|c| c == klass}
53 end 53 end
54 end 54 end
55 - 55 +
56 # This module contains class methods 56 # This module contains class methods
57 module SingletonMethods 57 module SingletonMethods
58 - 58 +
59 # Calculate the vote counts for all voteables of my type. 59 # Calculate the vote counts for all voteables of my type.
60 # Options: 60 # Options:
61 # :start_at - Restrict the votes to those created after a certain time 61 # :start_at - Restrict the votes to those created after a certain time
@@ -63,7 +63,7 @@ module Juixe @@ -63,7 +63,7 @@ module Juixe
63 # :conditions - A piece of SQL conditions to add to the query 63 # :conditions - A piece of SQL conditions to add to the query
64 # :limit - The maximum number of voteables to return 64 # :limit - The maximum number of voteables to return
65 # :order - A piece of SQL to order by. Two calculated columns `count`, and `total` 65 # :order - A piece of SQL to order by. Two calculated columns `count`, and `total`
66 - # are available for sorting apart from other columns. Defaults to `total DESC`. 66 + # are available for sorting apart from other columns. Defaults to `total DESC`.
67 # Eg: :order => 'count desc' 67 # Eg: :order => 'count desc'
68 # :order => 'total desc' 68 # :order => 'total desc'
69 # :order => 'post.created_at desc' 69 # :order => 'post.created_at desc'
@@ -76,7 +76,7 @@ module Juixe @@ -76,7 +76,7 @@ module Juixe
76 end 76 end
77 77
78 def options_for_tally (options = {}) 78 def options_for_tally (options = {})
79 - options.assert_valid_keys :start_at, :end_at, :conditions, :at_least, :at_most, :order, :limit, :at_least_total, :at_most_total 79 + options.assert_valid_keys :start_at, :end_at, :conditions, :at_least, :at_most, :order, :limit, :at_least_total, :at_most_total
80 80
81 scope = scope(:find) 81 scope = scope(:find)
82 start_at = sanitize_sql(["#{Vote.table_name}.created_at >= ?", options.delete(:start_at)]) if options[:start_at] 82 start_at = sanitize_sql(["#{Vote.table_name}.created_at >= ?", options.delete(:start_at)]) if options[:start_at]
@@ -85,7 +85,7 @@ module Juixe @@ -85,7 +85,7 @@ module Juixe
85 if respond_to?(:vote_counter_column) 85 if respond_to?(:vote_counter_column)
86 # use the counter cache column if present. 86 # use the counter cache column if present.
87 total_col = "#{table_name}.#{vote_counter_column}" 87 total_col = "#{table_name}.#{vote_counter_column}"
88 - at_least_total = sanitize_sql(["#{total_col} >= ?", options.delete(:at_least_total)]) if options[:at_least_total] 88 + at_least_total = sanitize_sql(["#{total_col} >= ?", options.delete(:at_least_total)]) if options[:at_least_total]
89 at_most_total = sanitize_sql(["#{total_col} <= ?", options.delete(:at_most_total)]) if options[:at_most_total] 89 at_most_total = sanitize_sql(["#{total_col} <= ?", options.delete(:at_most_total)]) if options[:at_most_total]
90 end 90 end
91 conditions = [ 91 conditions = [
@@ -104,7 +104,7 @@ module Juixe @@ -104,7 +104,7 @@ module Juixe
104 joins << scope[:joins] if scope && scope[:joins] 104 joins << scope[:joins] if scope && scope[:joins]
105 at_least = sanitize_sql(["COUNT(#{Vote.table_name}.id) >= ?", options.delete(:at_least)]) if options[:at_least] 105 at_least = sanitize_sql(["COUNT(#{Vote.table_name}.id) >= ?", options.delete(:at_least)]) if options[:at_least]
106 at_most = sanitize_sql(["COUNT(#{Vote.table_name}.id) <= ?", options.delete(:at_most)]) if options[:at_most] 106 at_most = sanitize_sql(["COUNT(#{Vote.table_name}.id) <= ?", options.delete(:at_most)]) if options[:at_most]
107 - at_least_total = at_most_total = nil # reset the values 107 + at_least_total = at_most_total = nil # reset the values
108 unless respond_to?(:vote_counter_column) 108 unless respond_to?(:vote_counter_column)
109 # aggregate the votes when counter cache is absent. 109 # aggregate the votes when counter cache is absent.
110 total_col = "SUM(#{Vote.table_name}.vote)" 110 total_col = "SUM(#{Vote.table_name}.vote)"
@@ -114,26 +114,26 @@ module Juixe @@ -114,26 +114,26 @@ module Juixe
114 having = [at_least, at_most, at_least_total, at_most_total].compact.join(' AND ') 114 having = [at_least, at_most, at_least_total, at_most_total].compact.join(' AND ')
115 group_by = "#{Vote.table_name}.voteable_id HAVING COUNT(#{Vote.table_name}.id) > 0" 115 group_by = "#{Vote.table_name}.voteable_id HAVING COUNT(#{Vote.table_name}.id) > 0"
116 group_by << " AND #{having}" unless having.blank? 116 group_by << " AND #{having}" unless having.blank?
117 -  
118 - { :select => "#{table_name}.*, COUNT(#{Vote.table_name}.id) AS count, #{total_col} AS total", 117 +
  118 + { :select => "#{table_name}.*, COUNT(#{Vote.table_name}.id) AS count, #{total_col} AS total",
119 :joins => joins.join(" "), 119 :joins => joins.join(" "),
120 :conditions => conditions, 120 :conditions => conditions,
121 :group => group_by 121 :group => group_by
122 - }.update(options) 122 + }.update(options)
123 end 123 end
124 - 124 +
125 end 125 end
126 - 126 +
127 # This module contains instance methods 127 # This module contains instance methods
128 module InstanceMethods 128 module InstanceMethods
129 def votes_for 129 def votes_for
130 - self.votes.count(:conditions => {:vote => 1}) 130 + self.votes.where(vote: 1).count
131 end 131 end
132 - 132 +
133 def votes_against 133 def votes_against
134 - self.votes.count(:conditions => {:vote => -1}) 134 + self.votes.where(vote: -1).count
135 end 135 end
136 - 136 +
137 # Same as voteable.votes.size 137 # Same as voteable.votes.size
138 def votes_count 138 def votes_count
139 self.votes.size 139 self.votes.size
@@ -142,11 +142,11 @@ module Juixe @@ -142,11 +142,11 @@ module Juixe
142 def votes_total 142 def votes_total
143 respond_to?(:vote_counter_column) ? send(self.vote_counter_column) : self.votes.sum(:vote) 143 respond_to?(:vote_counter_column) ? send(self.vote_counter_column) : self.votes.sum(:vote)
144 end 144 end
145 - 145 +
146 def voters_who_voted 146 def voters_who_voted
147 self.votes.collect(&:voter) 147 self.votes.collect(&:voter)
148 end 148 end
149 - 149 +
150 def voted_by?(voter, for_or_against = "all") 150 def voted_by?(voter, for_or_against = "all")
151 options = (for_or_against == "all") ? {} : {:vote => (for_or_against ? 1 : -1)} 151 options = (for_or_against == "all") ? {} : {:vote => (for_or_against ? 1 : -1)}
152 self.votes.exists?({:voter_id => voter.id, :voter_type => voter.class.base_class.name}.merge(options)) 152 self.votes.exists?({:voter_id => voter.id, :voter_type => voter.class.base_class.name}.merge(options))
vendor/plugins/kandadaboggu-vote_fu/lib/acts_as_voter.rb
@@ -9,28 +9,28 @@ module PeteOnRails @@ -9,28 +9,28 @@ module PeteOnRails
9 9
10 module ClassMethods 10 module ClassMethods
11 def acts_as_voter 11 def acts_as_voter
12 - has_many :votes, :as => :voter, :dependent => :nullify # If a voting entity is deleted, keep the votes. 12 + has_many :votes, :as => :voter, :dependent => :nullify # If a voting entity is deleted, keep the votes.
13 include PeteOnRails::Acts::Voter::InstanceMethods 13 include PeteOnRails::Acts::Voter::InstanceMethods
14 extend PeteOnRails::Acts::Voter::SingletonMethods 14 extend PeteOnRails::Acts::Voter::SingletonMethods
15 end 15 end
16 end 16 end
17 - 17 +
18 # This module contains class methods 18 # This module contains class methods
19 module SingletonMethods 19 module SingletonMethods
20 end 20 end
21 - 21 +
22 # This module contains instance methods 22 # This module contains instance methods
23 module InstanceMethods 23 module InstanceMethods
24 - 24 +
25 # Usage user.vote_count(true) # All +1 votes 25 # Usage user.vote_count(true) # All +1 votes
26 # user.vote_count(false) # All -1 votes 26 # user.vote_count(false) # All -1 votes
27 # user.vote_count() # All votes 27 # user.vote_count() # All votes
28 # 28 #
29 def vote_count(for_or_against = "all") 29 def vote_count(for_or_against = "all")
30 return self.votes.size if for_or_against == "all" 30 return self.votes.size if for_or_against == "all"
31 - self.votes.count(:conditions => {:vote => (for_or_against ? 1 : -1)}) 31 + self.votes.where(vote: if for_or_against then 1 else -1 end).count
32 end 32 end
33 - 33 +
34 def voted_for?(voteable) 34 def voted_for?(voteable)
35 voteable.voted_by?(self, true) 35 voteable.voted_by?(self, true)
36 end 36 end
@@ -42,11 +42,11 @@ module PeteOnRails @@ -42,11 +42,11 @@ module PeteOnRails
42 def voted_on?(voteable) 42 def voted_on?(voteable)
43 voteable.voted_by?(self) 43 voteable.voted_by?(self)
44 end 44 end
45 - 45 +
46 def vote_for(voteable) 46 def vote_for(voteable)
47 self.vote(voteable, 1) 47 self.vote(voteable, 1)
48 end 48 end
49 - 49 +
50 def vote_against(voteable) 50 def vote_against(voteable)
51 self.vote(voteable, -1) 51 self.vote(voteable, -1)
52 end 52 end
@@ -56,10 +56,10 @@ module PeteOnRails @@ -56,10 +56,10 @@ module PeteOnRails
56 voteable.reload_vote_counter if !v.new_record? and voteable.respond_to?(:reload_vote_counter) 56 voteable.reload_vote_counter if !v.new_record? and voteable.respond_to?(:reload_vote_counter)
57 end.errors.empty? 57 end.errors.empty?
58 end 58 end
59 - 59 +
60 end 60 end
61 - 61 +
62 end 62 end
63 - 63 +
64 end 64 end
65 -end  
66 \ No newline at end of file 65 \ No newline at end of file
  66 +end
vendor/plugins/kandadaboggu-vote_fu/lib/models/vote.rb
1 class Vote < ActiveRecord::Base 1 class Vote < ActiveRecord::Base
2 2
3 - scope :for_voter, lambda { |*args| {:conditions => ["voter_id = ? AND voter_type = ?", args.first.id, args.first.class.base_class.name]} }  
4 - scope :for_voteable, lambda { |*args| {:conditions => ["voteable_id = ? AND voteable_type = ?", args.first.id, args.first.class.base_class.name]} }  
5 - scope :recent, lambda { |*args| {:conditions => ["created_at > ?", (args.first || 2.weeks.ago).to_s(:db)]} }  
6 - scope :descending, :order => "created_at DESC" 3 + scope :for_voter, -> (*args) {
  4 + where "voter_id = ? AND voter_type = ?", args.first.id, args.first.class.base_class.name
  5 + }
  6 + scope :for_voteable, -> (*args) {
  7 + where "voteable_id = ? AND voteable_type = ?", args.first.id, args.first.class.base_class.name
  8 + }
  9 + scope :recent, -> (*args) {
  10 + where "created_at > ?", (args.first || 2.weeks.ago).to_s(:db)
  11 + }
  12 + scope :descending, -> { order "created_at DESC" }
7 13
8 # NOTE: Votes belong to the "voteable" interface, and also to voters 14 # NOTE: Votes belong to the "voteable" interface, and also to voters
9 belongs_to :voteable, :polymorphic => true 15 belongs_to :voteable, :polymorphic => true
10 belongs_to :voter, :polymorphic => true 16 belongs_to :voter, :polymorphic => true
11 - 17 +
12 attr_accessible :vote, :voter, :voteable 18 attr_accessible :vote, :voter, :voteable
13 19
14 - # Uncomment this to limit users to a single vote on each item. 20 + # Uncomment this to limit users to a single vote on each item.
15 #validates_uniqueness_of :voteable_id, :scope => [:voteable_type, :voter_type, :voter_id] 21 #validates_uniqueness_of :voteable_id, :scope => [:voteable_type, :voter_type, :voter_id]
16 22
17 end 23 end