ballot_box 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -36,10 +36,18 @@ or update place scope conditions:
36
36
  ballot_box :counter_cache => :rating,
37
37
  :place => "place"
38
38
 
39
- def ballot_box_place_scope
40
- self.class.unscoped.order("rating DESC").where(:is_visible => true)
39
+ def self.ballot_box_place_scope
40
+ unscoped.order("rating DESC").where(:is_visible => true)
41
41
  end
42
42
 
43
+ Update votes directly:
44
+
45
+ Post.ballot_box_update_votes!
46
+
47
+ Update place directly:
48
+
49
+ Post.ballot_box_update_place!
50
+
43
51
  View (just send post request to configure route):
44
52
 
45
53
  link_to 'Vote', "/posts/votes?id=#{@post.id}", :remote => true, :method => :post
@@ -72,6 +72,37 @@ module BallotBox
72
72
  def ballot_box_strategies
73
73
  @@ballot_box_strategies ||= ballot_box_options[:strategies].map { |st| BallotBox.load_strategy(st) }
74
74
  end
75
+
76
+ def ballot_box_update_votes!
77
+ votes_table = BallotBox::Vote.quoted_table_name
78
+
79
+ query = %(UPDATE #{quoted_table_name} a,
80
+ (SELECT SUM(value) AS summa, voteable_id, voteable_type
81
+ FROM #{votes_table}
82
+ WHERE voteable_type = '#{name}'
83
+ GROUP BY voteable_id, voteable_type) b
84
+ SET a.#{ballot_box_cached_column} = b.summa
85
+ WHERE a.id = b.voteable_id AND b.voteable_type = '#{name}')
86
+
87
+ connection.execute(query)
88
+ end
89
+
90
+ def ballot_box_place_scope
91
+ unscoped.order("#{ballot_box_cached_column} DESC")
92
+ end
93
+
94
+ def ballot_box_update_place!
95
+ table = quoted_table_name
96
+ subquery = ballot_box_place_scope.select("@row := @row + 1 AS row, #{table}.id").from("#{table}, (SELECT @row := 0) r")
97
+
98
+ query = %(UPDATE #{table} AS a
99
+ INNER JOIN (
100
+ #{subquery.to_sql}
101
+ ) AS b ON a.id = b.id
102
+ SET a.#{ballot_box_place_column} = b.row;)
103
+
104
+ connection.execute(query)
105
+ end
75
106
  end
76
107
 
77
108
  module InstanceMethods
@@ -83,10 +114,6 @@ module BallotBox
83
114
  def ballot_box_place_column
84
115
  @ballot_box_place_column ||= self.class.ballot_box_place_column
85
116
  end
86
-
87
- def ballot_box_place_scope
88
- self.class.unscoped.order("#{ballot_box_cached_column} DESC")
89
- end
90
117
 
91
118
  def ballot_box_valid?(vote)
92
119
  self.class.ballot_box_strategies.map { |st| st.new(self, vote) }.map(&:valid?).all?
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module BallotBox
3
- VERSION = "0.1.1".freeze
3
+ VERSION = "0.1.2".freeze
4
4
  end
@@ -136,17 +136,7 @@ module BallotBox
136
136
 
137
137
  def update_place
138
138
  if voteable && voteable.ballot_box_place_column
139
- table = voteable.class.quoted_table_name
140
- subquery = voteable.ballot_box_place_scope
141
- subquery = subquery.select("@row := @row + 1 AS row, #{table}.id").from("#{table}, (SELECT @row := 0) r")
142
-
143
- query = %(UPDATE #{table} AS a
144
- INNER JOIN (
145
- #{subquery.to_sql}
146
- ) AS b ON a.id = b.id
147
- SET a.#{voteable.ballot_box_place_column} = b.row;)
148
-
149
- voteable.class.connection.execute(query)
139
+ voteable.class.ballot_box_update_place!
150
140
  end
151
141
  end
152
142
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ballot_box
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Igor Galeta
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-06-01 00:00:00 +03:00
19
+ date: 2011-06-08 00:00:00 +03:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency