Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Choose duration for tomato #291

Closed
wants to merge 11 commits into from
1 change: 1 addition & 0 deletions app/assets/javascripts/TT.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ var TT = function() {
show([settings.formId]);

$("#" + settings.formId + " input[type=text]").focus();
$("#tomato_duration").val(tomatoDuration);
}

var stateSignIn = function() {
Expand Down
2 changes: 2 additions & 0 deletions app/assets/javascripts/TT_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ soundManager.setup({

function startCallback(event) {
if('idle' == TT.getStatus()) {
chosenDuration = document.getElementById("duration_time_input").value;
tomatoDuration = parseInt(chosenDuration);
TT.start(tomatoDuration, currentUser ? TT.stateNewForm : TT.stateSignIn);
event.preventDefault();
}
Expand Down
11 changes: 11 additions & 0 deletions app/assets/stylesheets/welcome.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@
tr.highlight {
background-color: $state-success-bg;
}
.choose_duration {
width: 25rem;
margin: 0 auto;
}
.choose_duration__input {
height: 7rem;
}
.choose_duration__output {
display: inline-block;
}

2 changes: 1 addition & 1 deletion app/controllers/concerns/tomatoes_params.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module TomatoesParams
def resource_params
params.permit(tomato: [:tag_list]).require(:tomato)
params.permit(tomato: [:tag_list, :duration]).require(:tomato)
end
end
2 changes: 1 addition & 1 deletion app/helpers/tomatoes_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module TomatoesHelper
def tomato_duration(tomato)
"#{(tomato.created_at - Tomato::DURATION).strftime('%I:%M %p')} - #{tomato.created_at.strftime('%I:%M %p')}"
"#{(tomato.created_at - tomato.duration.minutes).strftime('%I:%M %p')} - #{tomato.created_at.strftime('%I:%M %p')}"
end
end
10 changes: 7 additions & 3 deletions app/models/tomato.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ class Tomato
after_destroy :decrement_score

DURATION = Rails.env.development? ? 25 : 25 * 60 # pomodoro default duration in seconds
BREAK_DURATION = Rails.env.development? ? 5 : 5 * 60 # pomodoro default break duration in seconds
DURATION_MIN = 25 # pomodoro default duration in minutes
DURATION_MAX = 60 # pomodoro default duration in minutes
BREAK_DURATION = Rails.env.development? ? 5 : 5 * 60 # pomodoro default break duration in seconds

field :duration, type: Integer, default: DURATION_MIN

include ActionView::Helpers::TextHelper
include ApplicationHelper
Expand Down Expand Up @@ -64,9 +68,9 @@ def projects
private

def must_not_overlap
last_tomato = user.tomatoes.after(Time.zone.now - DURATION.seconds).order_by([[:created_at, :desc]]).first
last_tomato = user.tomatoes.after(Time.zone.now - duration.minutes).order_by([[:created_at, :desc]]).first
return unless last_tomato
limit = (DURATION.seconds - (Time.zone.now - last_tomato.created_at)).seconds
limit = (duration.minutes - (Time.zone.now - last_tomato.created_at)).seconds
errors.add(:base, I18n.t('errors.messages.must_not_overlap', limit: humanize(limit)))
end

Expand Down
1 change: 1 addition & 0 deletions app/views/tomatoes/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<% remote = false if local_assigns[:remote].nil? %>

<%= form_for(@tomato, remote: remote) do |f| %>
<%= f.hidden_field :duration %>
<% if @tomato.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@tomato.errors.count, "error") %> prohibited this tomato from being saved:</h2>
Expand Down
2 changes: 1 addition & 1 deletion app/views/tomatoes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<dl class="dl-horizontal">
<dt><%= t('mongoid.attributes.tomato.started_at') %></dt>
<dd><%= l(@tomato.created_at - Tomato::DURATION, format: :long) %></dd>
<dd><%= l(@tomato.created_at - @tomato.duration.minutes, format: :long) %></dd>
<dt><%= t('tomato.duration') %></dt>
<dd><%= tomato_duration(@tomato) %></dd>
<dt><%= t('mongoid.attributes.tomato.tags') %></dt>
Expand Down
15 changes: 15 additions & 0 deletions app/views/welcome/_tomatoes_timer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,19 @@
<div id="start_container" class="center-block">
<%= link_to t('timer.start'), '#', id: 'start', class: 'btn btn-default btn-lg' %>
<p id="start_hint" class="help-block"><%= t('timer.or_press_space') %></p>
<div class="form-group choose_duration">
<label for="duration_time_input"><%= t('tomato.duration') %></label>
<input name="duration_time"
id="duration_time_input"
class="form-control choose_duration__input"
type="range"
min="<%= Tomato::DURATION_MIN %>"
max="<%= Tomato::DURATION_MAX %>"
value="<%= Tomato::DURATION_MIN %>"
oninput="duration_time_output.value = duration_time_input.value">
<output class="choose_duration__output" name="duration_time_output" id="duration_time_output">
<%= Tomato::DURATION_MIN %>
</output>
<span><%= t('minutes') %></span>
</div>
</div>
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ en:
overall_leaderboard: Overall leaderboard
empty_leaderboard: No scores in this leaderboard
leaderboards: Leaderboards
minutes: minutes
daily: Daily
weekly: Weekly
monthly: Monthly
Expand Down
1 change: 1 addition & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ es:
overall_leaderboard: Tabla de posiciones general
empty_leaderboard: No existen registros en esta tabla de posiciones
leaderboards: Tablas de posiciones
minutes: minutos
daily: Diaria
weekly: Semanal
monthly: Mensual
Expand Down
1 change: 1 addition & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ fr:
overall_leaderboard: Classement général
empty_leaderboard: Il n'y a pas de données dans ce classement
leaderboards: Classements
minutes: minutes
daily: Aujourd'hui
weekly: Cette semaine
monthly: Ce mois-ci
Expand Down
1 change: 1 addition & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pt-BR:
overall_leaderboard: Placar de todo o tempo
empty_leaderboard: Placar vazio
leaderboards: Classificações
minutes: minutos
daily: Diário
weekly: Semanal
monthly: Mensal
Expand Down
2 changes: 1 addition & 1 deletion test/functional/tomatoes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TomatoesControllerTest < ActionController::TestCase
name: 'name',
email: '[email protected]'
)
@tomato = @user.tomatoes.create(tag_list: 'one, two', created_at: Time.zone.now - 1.day)
@tomato = @user.tomatoes.create(tag_list: 'one, two', created_at: Time.zone.now - 1.day, duration: 25)

@controller.stubs(:current_user).returns(@user)
end
Expand Down
2 changes: 1 addition & 1 deletion test/support/leaderboard_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module LeaderboardControllerTest
name: 'name',
email: '[email protected]'
)
@tomato = @user.tomatoes.create!(tag_list: 'one, two')
@tomato = @user.tomatoes.create!(tag_list: 'one, two', duration: 25)
end

test 'should get show' do
Expand Down
5 changes: 3 additions & 2 deletions test/unit/helpers/tomatoes_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
class TomatoesHelperTest < ActionView::TestCase
test 'tomato_duration should return a formatted string' do
@created_at = Time.new(2011, 7, 24, 15, 10).in_time_zone
@ended_at = @created_at - Tomato::DURATION
@tomato = Tomato.new(created_at: @created_at)
duration = 5
@ended_at = @created_at - duration.minutes
@tomato = Tomato.new(created_at: @created_at, duration: duration)

assert tomato_duration(@tomato) == "#{@ended_at.strftime('%I:%M %p')} - #{@created_at.strftime('%I:%M %p')}"
end
Expand Down
4 changes: 2 additions & 2 deletions test/unit/tomato_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class TomatoTest < ActiveSupport::TestCase

test 'must_not_overlap B' do
user = User.create!
user.tomatoes.create!(created_at: Time.zone.now - Tomato::DURATION.seconds + 5.seconds)
user.tomatoes.create!(created_at: Time.zone.now - Tomato::DURATION_MIN.minutes + 5.seconds)
assert !user.tomatoes.build.valid?
end

test 'must_not_overlap C' do
user = User.create!
user.tomatoes.create!(created_at: Time.zone.now - Tomato::DURATION.seconds - 1.second)
user.tomatoes.create!(created_at: Time.zone.now - Tomato::DURATION_MIN.minutes - 1.second)

assert user.tomatoes.build.valid?
end
Expand Down