Module: BallotBox::Base::ClassMethods

Defined in:
lib/ballot_box/base.rb

Instance Method Summary collapse

Instance Method Details

#ballot_box_cached_columnObject


59
60
61
62
63
64
65
66
67
# File 'lib/ballot_box/base.rb', line 59

def ballot_box_cached_column
  if ballot_box_options[:counter_cache] == true
    "votes_count"
  elsif ballot_box_options[:counter_cache]
    ballot_box_options[:counter_cache]
  else
    false
  end
end

#ballot_box_place_columnObject


69
70
71
72
73
74
75
76
77
# File 'lib/ballot_box/base.rb', line 69

def ballot_box_place_column
  if ballot_box_options[:place] == true
    "place"
  elsif ballot_box_options[:place]
    ballot_box_options[:place]
  else
    false
  end
end

#ballot_box_place_scopeObject


97
98
99
# File 'lib/ballot_box/base.rb', line 97

def ballot_box_place_scope
  unscoped.order("#{quoted_table_name}.#{ballot_box_cached_column} DESC")
end

#ballot_box_strategiesObject


79
80
81
# File 'lib/ballot_box/base.rb', line 79

def ballot_box_strategies
  @@ballot_box_strategies ||= ballot_box_options[:strategies].map { |st| BallotBox.load_strategy(st) }
end

#ballot_box_update_place!(record = nil) ⇒ Object


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ballot_box/base.rb', line 101

def ballot_box_update_place!(record = nil)
  relation = ballot_box_place_scope
  
  if ballot_box_options[:scope]
    scope_columns = Array.wrap(ballot_box_options[:scope])
    
    unless record.nil?
      scope_columns.each do |scope_item|
        scope_value = record.send(scope_item)
        relation = relation.where(scope_item => scope_value)
      end
    else
      unscoped.select(scope_columns).group(scope_columns).each do |record|
        ballot_box_update_place!(record)
      end
      
      return
    end
  end
  
  ballot_box_update_place_by_relation(relation)
end

#ballot_box_update_place_by_relation(relation) ⇒ Object


124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ballot_box/base.rb', line 124

def ballot_box_update_place_by_relation(relation)
  table = quoted_table_name
  
  relation = relation.select("@row := @row + 1 AS row, #{table}.id").from("#{table}, (SELECT @row := 0) r")
  
  query = %(UPDATE #{table} AS a
    INNER JOIN (
      #{relation.to_sql}
    ) AS b ON a.id = b.id
    SET a.#{ballot_box_place_column} = b.row;)
  
  connection.execute(query)
end

#ballot_box_update_votes!Object


83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ballot_box/base.rb', line 83

def ballot_box_update_votes!
  votes_table = BallotBox::Vote.quoted_table_name
  
  query = %(UPDATE #{quoted_table_name} a, 
      (SELECT SUM(value) AS summa, voteable_id, voteable_type 
       FROM #{votes_table} 
       WHERE voteable_type = '#{name}'
       GROUP BY voteable_id, voteable_type) b 
    SET a.#{ballot_box_cached_column} = b.summa
    WHERE a.id = b.voteable_id AND b.voteable_type = '#{name}')
  
  connection.execute(query)
end

#define_ballot_box_callbacks(*callbacks) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ballot_box/base.rb', line 45

def define_ballot_box_callbacks(*callbacks)
  define_callbacks *[callbacks, {:terminator => "result == false"}].flatten
  callbacks.each do |callback|
    eval <<-end_callbacks
      def before_#{callback}(*args, &blk)
        set_callback(:#{callback}, :before, *args, &blk)
      end
      def after_#{callback}(*args, &blk)
        set_callback(:#{callback}, :after, *args, &blk)
      end
    end_callbacks
  end
end