ballot_box 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -21,7 +21,23 @@ Set voteable model:
21
21
 
22
22
  class Post < ActiveRecord::Base
23
23
  ballot_box :counter_cache => true
24
- # ballot_box :counter_cache => :rating, :strategies => [:authenticated]
24
+ end
25
+
26
+ Set votes sum column:
27
+
28
+ ballot_box :counter_cache => :rating, :strategies => [:authenticated]
29
+
30
+ Set place (position) column:
31
+
32
+ ballot_box :place => true, :counter_cache => true
33
+
34
+ or update place scope conditions:
35
+
36
+ ballot_box :counter_cache => :rating,
37
+ :place => "place"
38
+
39
+ def ballot_box_place_scope
40
+ self.class.unscoped.order("rating DESC").where(:is_visible => true)
25
41
  end
26
42
 
27
43
  View (just send post request to configure route):
@@ -7,13 +7,17 @@ module BallotBox
7
7
  module SingletonMethods
8
8
  #
9
9
  # ballot_box :counter_cache => true,
10
- # :strategies => [:authenticated]
10
+ # :strategies => [:authenticated],
11
+ # :place => { :column => "place", :order => "votes_count DESC" }
11
12
  #
12
13
  def ballot_box(options = {})
13
14
  extend ClassMethods
14
15
  include InstanceMethods
15
16
 
16
- options = { :strategies => [:authenticated] }.merge(options)
17
+ options = {
18
+ :strategies => [:authenticated],
19
+ :place => false
20
+ }.merge(options)
17
21
 
18
22
  class_attribute :ballot_box_options, :instance_writer => false
19
23
  self.ballot_box_options = options
@@ -55,6 +59,16 @@ module BallotBox
55
59
  end
56
60
  end
57
61
 
62
+ def ballot_box_place_column
63
+ if ballot_box_options[:place] == true
64
+ "place"
65
+ elsif ballot_box_options[:place]
66
+ ballot_box_options[:place]
67
+ else
68
+ false
69
+ end
70
+ end
71
+
58
72
  def ballot_box_strategies
59
73
  @@ballot_box_strategies ||= ballot_box_options[:strategies].map { |st| BallotBox.load_strategy(st) }
60
74
  end
@@ -66,6 +80,14 @@ module BallotBox
66
80
  @ballot_box_cached_column ||= self.class.ballot_box_cached_column
67
81
  end
68
82
 
83
+ def ballot_box_place_column
84
+ @ballot_box_place_column ||= self.class.ballot_box_place_column
85
+ end
86
+
87
+ def ballot_box_place_scope
88
+ self.class.unscoped.order("#{ballot_box_cached_column} DESC")
89
+ end
90
+
69
91
  def ballot_box_valid?(vote)
70
92
  self.class.ballot_box_strategies.map { |st| st.new(self, vote) }.map(&:valid?).all?
71
93
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module BallotBox
3
- VERSION = "0.1.0".freeze
3
+ VERSION = "0.1.1".freeze
4
4
  end
@@ -25,7 +25,7 @@ module BallotBox
25
25
  after_save :update_cached_columns
26
26
  after_destroy :update_cached_columns
27
27
 
28
- attr_accessible :request
28
+ attr_accessible :request, :ip_address, :user_agent
29
29
 
30
30
  composed_of :ip,
31
31
  :class_name => 'IPAddr',
@@ -123,11 +123,32 @@ module BallotBox
123
123
  end
124
124
 
125
125
  def update_cached_columns
126
+ update_votes_count
127
+ update_place
128
+ end
129
+
130
+ def update_votes_count
126
131
  if voteable && voteable.ballot_box_cached_column
127
132
  count = voteable.votes.select("SUM(value)")
128
133
  voteable.class.update_all("#{voteable.ballot_box_cached_column} = (#{count.to_sql})", ["id = ?", voteable.id])
129
134
  end
130
135
  end
136
+
137
+ def update_place
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)
150
+ end
151
+ end
131
152
  end
132
153
  end
133
154
  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: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
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-05-18 00:00:00 +03:00
19
+ date: 2011-06-01 00:00:00 +03:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency