Commit 9c85b57161c818001e5f71e9417d3f312e8e964d
1 parent
94247db4
Exists in
master
and in
1 other branch
fix spacing in Question.get_optional_information
Showing
1 changed file
with
57 additions
and
58 deletions
Show diff stats
app/models/question.rb
| @@ -141,93 +141,92 @@ class Question < ActiveRecord::Base | @@ -141,93 +141,92 @@ class Question < ActiveRecord::Base | ||
| 141 | weights | 141 | weights |
| 142 | end | 142 | end |
| 143 | 143 | ||
| 144 | - def get_optional_information(params) | 144 | + def get_optional_information(params) |
| 145 | 145 | ||
| 146 | - return {} if params.nil? | 146 | + return {} if params.nil? |
| 147 | 147 | ||
| 148 | - result = {} | ||
| 149 | - visitor_identifier = params[:visitor_identifier] | ||
| 150 | - current_user = self.site | 148 | + result = {} |
| 149 | + visitor_identifier = params[:visitor_identifier] | ||
| 150 | + current_user = self.site | ||
| 151 | 151 | ||
| 152 | - if params[:with_prompt] | 152 | + if params[:with_prompt] |
| 153 | 153 | ||
| 154 | - if params[:with_appearance] && visitor_identifier.present? | ||
| 155 | - visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) | 154 | + if params[:with_appearance] && visitor_identifier.present? |
| 155 | + visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) | ||
| 156 | 156 | ||
| 157 | - last_appearance = get_first_unanswered_appearance(visitor) | 157 | + last_appearance = get_first_unanswered_appearance(visitor) |
| 158 | 158 | ||
| 159 | - if last_appearance.nil? | 159 | + if last_appearance.nil? |
| 160 | @prompt = choose_prompt(:algorithm => params[:algorithm]) | 160 | @prompt = choose_prompt(:algorithm => params[:algorithm]) |
| 161 | @appearance = current_user.record_appearance(visitor, @prompt) | 161 | @appearance = current_user.record_appearance(visitor, @prompt) |
| 162 | - else | ||
| 163 | - #only display a new prompt and new appearance if the old prompt has not been voted on | ||
| 164 | - @appearance = last_appearance | 162 | + else |
| 163 | + #only display a new prompt and new appearance if the old prompt has not been voted on | ||
| 164 | + @appearance = last_appearance | ||
| 165 | @prompt= @appearance.prompt | 165 | @prompt= @appearance.prompt |
| 166 | - end | 166 | + end |
| 167 | 167 | ||
| 168 | - if params[:future_prompts] | ||
| 169 | - num_future = params[:future_prompts][:number].to_i rescue 1 | ||
| 170 | - num_future.times do |number| | ||
| 171 | - offset = number + 1 | ||
| 172 | - last_appearance = get_first_unanswered_appearance(visitor, offset) | ||
| 173 | - if last_appearance.nil? | ||
| 174 | - @future_prompt = choose_prompt(:algorithm => params[:algorithm]) | ||
| 175 | - @future_appearance = current_user.record_appearance(visitor, @future_prompt) | ||
| 176 | - else | ||
| 177 | - @future_appearance = last_appearance | ||
| 178 | - @future_prompt= @future_appearance.prompt | ||
| 179 | - end | 168 | + if params[:future_prompts] |
| 169 | + num_future = params[:future_prompts][:number].to_i rescue 1 | ||
| 170 | + num_future.times do |number| | ||
| 171 | + offset = number + 1 | ||
| 172 | + last_appearance = get_first_unanswered_appearance(visitor, offset) | ||
| 173 | + if last_appearance.nil? | ||
| 174 | + @future_prompt = choose_prompt(:algorithm => params[:algorithm]) | ||
| 175 | + @future_appearance = current_user.record_appearance(visitor, @future_prompt) | ||
| 176 | + else | ||
| 177 | + @future_appearance = last_appearance | ||
| 178 | + @future_prompt= @future_appearance.prompt | ||
| 179 | + end | ||
| 180 | + | ||
| 181 | + result.merge!({"future_appearance_id_#{offset}".to_sym => @future_appearance.lookup}) | ||
| 182 | + result.merge!({"future_prompt_id_#{offset}".to_sym => @future_prompt.id}) | ||
| 183 | + | ||
| 184 | + ["left", "right"].each do |side| | ||
| 185 | + ["text", "id"].each do |param| | ||
| 186 | + choice = (side == "left") ? @future_prompt.left_choice : @future_prompt.right_choice | ||
| 187 | + param_val = (param == "text") ? choice.data : choice.id | ||
| 180 | 188 | ||
| 181 | - result.merge!({"future_appearance_id_#{offset}".to_sym => @future_appearance.lookup}) | ||
| 182 | - result.merge!({"future_prompt_id_#{offset}".to_sym => @future_prompt.id}) | 189 | + result.merge!({"future_#{side}_choice_#{param}_#{offset}".to_sym => param_val}) |
| 190 | + end | ||
| 191 | + end | ||
| 183 | 192 | ||
| 184 | - ["left", "right"].each do |side| | ||
| 185 | - ["text", "id"].each do |param| | ||
| 186 | - choice = (side == "left") ? @future_prompt.left_choice : @future_prompt.right_choice | ||
| 187 | - param_val = (param == "text") ? choice.data : choice.id | ||
| 188 | - | ||
| 189 | - result.merge!({"future_#{side}_choice_#{param}_#{offset}".to_sym => param_val}) | ||
| 190 | end | 193 | end |
| 191 | - end | ||
| 192 | - | ||
| 193 | - end | ||
| 194 | 194 | ||
| 195 | - end | 195 | + end |
| 196 | 196 | ||
| 197 | - result.merge!({:appearance_id => @appearance.lookup}) | ||
| 198 | - else | ||
| 199 | - # throw some error | ||
| 200 | - end | 197 | + result.merge!({:appearance_id => @appearance.lookup}) |
| 198 | + else | ||
| 199 | + # throw some error | ||
| 200 | + end | ||
| 201 | 201 | ||
| 202 | - if !@prompt | ||
| 203 | - @prompt = choose_prompt(:algorithm => params[:algorithm]) | ||
| 204 | - end | ||
| 205 | - result.merge!({:picked_prompt_id => @prompt.id}) | ||
| 206 | - end | 202 | + if !@prompt |
| 203 | + @prompt = choose_prompt(:algorithm => params[:algorithm]) | ||
| 204 | + end | ||
| 205 | + result.merge!({:picked_prompt_id => @prompt.id}) | ||
| 206 | + end | ||
| 207 | 207 | ||
| 208 | - if params[:with_visitor_stats] | 208 | + if params[:with_visitor_stats] |
| 209 | visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) | 209 | visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) |
| 210 | result.merge!(:visitor_votes => visitor.votes.count(:conditions => {:question_id => self.id})) | 210 | result.merge!(:visitor_votes => visitor.votes.count(:conditions => {:question_id => self.id})) |
| 211 | result.merge!(:visitor_ideas => visitor.choices.count) | 211 | result.merge!(:visitor_ideas => visitor.choices.count) |
| 212 | - end | 212 | + end |
| 213 | 213 | ||
| 214 | - # this might get cpu intensive if used too often. If so, store the calculated value in redis | ||
| 215 | - # and expire after X minutes | ||
| 216 | - if params[:with_average_votes] | 214 | + # this might get cpu intensive if used too often. If so, store the calculated value in redis |
| 215 | + # and expire after X minutes | ||
| 216 | + if params[:with_average_votes] | ||
| 217 | votes_by_visitors = self.votes.count(:group => 'voter_id') | 217 | votes_by_visitors = self.votes.count(:group => 'voter_id') |
| 218 | 218 | ||
| 219 | if votes_by_visitors.size > 0 | 219 | if votes_by_visitors.size > 0 |
| 220 | - average = votes_by_visitors.inject(0){|total, (k,v)| total = total + v}.to_f / votes_by_visitors.size.to_f | 220 | + average = votes_by_visitors.inject(0){|total, (k,v)| total = total + v}.to_f / votes_by_visitors.size.to_f |
| 221 | else | 221 | else |
| 222 | - average = 0.0 | 222 | + average = 0.0 |
| 223 | end | 223 | end |
| 224 | 224 | ||
| 225 | result.merge!(:average_votes => average.round) # round to 2 decimals | 225 | result.merge!(:average_votes => average.round) # round to 2 decimals |
| 226 | - end | ||
| 227 | - | ||
| 228 | - return result | 226 | + end |
| 229 | 227 | ||
| 230 | - end | 228 | + return result |
| 229 | + end | ||
| 231 | 230 | ||
| 232 | #passing precomputed sum saves us a traversal through the array | 231 | #passing precomputed sum saves us a traversal through the array |
| 233 | def normalize!(weighted, sum=nil) | 232 | def normalize!(weighted, sum=nil) |