Skip to content

Commit

Permalink
Leveling message
Browse files Browse the repository at this point in the history
  • Loading branch information
dkapadia committed Apr 26, 2010
1 parent abc66b3 commit fd96f7f
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 183 deletions.
11 changes: 10 additions & 1 deletion app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -647,12 +647,17 @@ def vote(direction)
if conditional
#flash[:notice] = 'Vote was successfully counted.'
newprompt = Crack::XML.parse(p.body)['prompt']

leveling_message = Visitor.leveling_message(:votes => newprompt['visitor_votes'].to_i,
:ideas => newprompt['visitor_ideas'].to_i)

logger.info "newprompt is #{newprompt.inspect}"
session[:current_prompt_id] = newprompt['id']
session[:appearance_lookup] = newprompt['appearance_id']
#@newprompt = Question.find(params[:id])
render :json => {:votes => 20, :newleft => truncate((newprompt['left_choice_text']), :length => 137),
:newright => truncate((newprompt['right_choice_text']), :length => 137)
:newright => truncate((newprompt['right_choice_text']), :length => 137),
:leveling_message => leveling_message
}.to_json
else
render :json => '{"error" : "Vote failed"}'
Expand Down Expand Up @@ -709,9 +714,13 @@ def add_idea
logger.info "just posted to 'create from abroad', response pending"
newchoice = Crack::XML.parse(p.body)['choice']
logger.info "response is #{newchoice.inspect}"

leveling_message = Visitor.leveling_message(:votes => newchoice['visitor_votes'].to_i,
:ideas => newchoice['visitor_ideas'].to_i)
@question = Question.find(params[:id])
render :json => {:votes => 20,
:choice_status => newchoice['choice_status'],
:leveling_message => leveling_message,
:message => "#{t('items.you_just_submitted')}: #{new_idea_data}"}.to_json
case newchoice['choice_status']
when 'inactive'
Expand Down
25 changes: 25 additions & 0 deletions app/models/visitor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
class Visitor < ActiveRecord::Base
has_many :session_infos

#Default param values to use in leveling message
A = 10
B = 0
C = 0.7
D = 25
E = 0
F = 0.7
LEVEL_ADJECTIVES = ["terrible", "pathetic", "lame", "so-so", "okay, I suppose",
"not bad", "good", "great", "amazing", "mythical"]

def self.leveling_message(params = {:votes => 0, :ideas => 0})
score = self.level_score(params)

message = "Now you have cast " +
"#{params[:votes]} #{params[:votes] == 1 ? "votes".singularize : "votes"} "+
"and added #{params[:ideas]} #{params[:ideas] == 1 ? "ideas".singularize : "ideas"}: "+
LEVEL_ADJECTIVES[(score/10).truncate]


end

def self.level_score(params = {:votes => 0, :ideas => 0})
(A * params[:votes] + B)**C + (D * params[:ideas] + E)**F
end
end
6 changes: 3 additions & 3 deletions app/views/earls/_vote_box_js.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ set_focus_and_blur_events = function() {
$.post('/questions/' + question_id + '/add_idea.js' + '?locale=<%= I18n.locale %>',
'authenticity_token='+encodeURIComponent(AUTH_TOKEN)+'&new_idea='+new_idea,
function(data){
$('.tellmearea').html(data["message"]);
$('.tellmearea').html(data["message"] + " <br /> " + data["leveling_message"]);
$('#new_idea_field').val("");
thankyoutext = '<%=t('vote.submit_idea_thankyou')%>';
$('#new_idea_field').attr('title', thankyoutext);
Expand Down Expand Up @@ -116,14 +116,14 @@ set_focus_and_blur_events = function() {
$('.tellmearea').html("<%= t('vote.vote_other_error') %>").effect("highlight", {color: '#ff0000'}, 1500);
}

loadedTime = new Date() //reset loaded time
loadedTime = new Date(); //reset loaded time

},
success: function(data){
$('.indicator').hide();
$('.leftside').html(data["newleft"]);
$('.rightside').html(data["newright"]);
$('.tellmearea').html("<%= t('vote.you_chose')%> " + winner + " <%=t('vote.over') %> " + loser).effect("highlight", {}, 1500);
$('.tellmearea').html("<%= t('vote.you_chose')%> " + winner + " <%=t('vote.over') %> " + loser + " <br /> " + data["leveling_message"]).effect("highlight", {}, 1500);
current_vote_count = $('#votes_count').html();
$('#votes_count').html(increment(current_vote_count)).effect("highlight", {}, 1500);
$.unblockUI();
Expand Down
10 changes: 7 additions & 3 deletions config/environments/cucumber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
config.action_controller.perform_caching = false

# Disable request forgery protection in test environment
config.action_controller.allow_forgery_protection = false
# THIS IS IMPORTANT
config.action_controller.allow_forgery_protection = true

# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
Expand All @@ -23,7 +24,8 @@

config.gem 'cucumber-rails', :lib => false, :version => '>=0.3.0' unless File.directory?(File.join(Rails.root, 'vendor/plugins/cucumber-rails'))
config.gem 'database_cleaner', :lib => false, :version => '>=0.5.0' unless File.directory?(File.join(Rails.root, 'vendor/plugins/database_cleaner'))
config.gem 'webrat', :lib => false, :version => '>=0.7.0' unless File.directory?(File.join(Rails.root, 'vendor/plugins/webrat'))
#config.gem 'webrat', :lib => false, :version => '>=0.7.0' unless File.directory?(File.join(Rails.root, 'vendor/plugins/webrat'))
config.gem 'capybara', :lib => false, :version => '>=0.3.0' unless File.directory?(File.join(Rails.root, 'vendor/plugins/capybara'))
config.gem 'rspec', :lib => false, :version => '>=1.3.0' unless File.directory?(File.join(Rails.root, 'vendor/plugins/rspec'))
config.gem 'rspec-rails', :lib => false, :version => '>=1.3.2' unless File.directory?(File.join(Rails.root, 'vendor/plugins/rspec-rails'))

Expand All @@ -38,4 +40,6 @@
PAIRWISE_PASSWORD = "wheatthins"

require 'redis-store'
Abingo.cache = ActiveSupport::Cache::RedisStore.new
#use a different redis store for testing
Abingo.cache = ActiveSupport::Cache::RedisStore.new "localhost:6379/2"
Abingo.cache.clear
3 changes: 1 addition & 2 deletions features/idea_marketplaces/create_question.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@focus
@demo
Feature: Creating Idea marketplaces
In order to collect information on preferences
As a question admin
Expand Down Expand Up @@ -47,7 +47,6 @@ Feature: Creating Idea marketplaces
Then I should see "errors prohibited this website"
And I should not see "Congratulations."

@superfocus
Scenario Outline: User does not fill in all fields
Given I am on the question create page
When I fill in all fields with valid data except "<field>"
Expand Down
10 changes: 6 additions & 4 deletions features/step_definitions/idea_marketplace_steps.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Given /^an idea marketplace exists with url '(.*)'$/ do |url|
Factory.create(:earl, :name => url)
Given "I am on the question create page"
When "I fill in all fields with valid data except \"question_url\""
And "I fill in \"question_url\" with \"#{url}\""
And "I press \"Create\""

Capybara.reset_sessions!
end
When /^I fill in all fields with valid data except "([^\"]*)"$/ do |field_id|
valid_data = Hash[
Expand All @@ -10,9 +15,6 @@
"question_password" => "password"
]

if field_id == "question_email"

end

valid_data.each do |k, v|
if k == field_id
Expand Down
11 changes: 11 additions & 0 deletions features/step_definitions/voting_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
When /^I click on the left choice$/ do
When "I follow \"leftside\""
Capybara.default_wait_time = 10
end

When /^I upload an idea titled '(.*)'$/ do |ideatext|
When "I fill in \"new_idea_field\" with \"#{ideatext}\""
find("#submit_btn").click

end

Loading

0 comments on commit fd96f7f

Please sign in to comment.