Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
Modifying malt bill works.
Browse files Browse the repository at this point in the history
  • Loading branch information
amolenaar committed Jun 26, 2012
1 parent fd48661 commit 506ae69
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 59 deletions.
16 changes: 11 additions & 5 deletions public/index.html
Expand Up @@ -54,13 +54,17 @@ <h1>BrewGear</h1>
<option value="bitter">Bitter</option>
</select>
</fieldset>
<fieldset>
<label for="targetVolume">Doel volume (l)</label>
<input type="number" name="targetVolume" id="targetVolume" value=""/>
</fieldset>
<fieldset>
<label for="plannedOg">Gepland OG</label>
<input type="text" name="plannedOg" id="plannedOg" value="${planned_og}"/>
<input type="number" min="0" name="plannedOg" id="plannedOg" value="${planned_og}"/>
</fieldset>
<fieldset>
<label for="plannedOg">Gepland FG</label>
<input type="text" name="plannedFg" id="plannedFg" value="${planned_fg}"/>
<input type="number" min="0" name="plannedFg" id="plannedFg" value="${planned_fg}"/>
</fieldset>
</form>
<ul data-role="listview" data-inset="true">
Expand Down Expand Up @@ -88,10 +92,12 @@ <h1>Nieuw recept</h1>
<option value="bitter">Bitter</option>
</select>

<label for="targetVolume">Doel volume (l)</label>
<input type="number" name="targetVolume" id="targetVolume" value=""/>
<label for="plannedOg">Gepland OG</label>
<input type="text" name="plannedOg" id="plannedOg" value=""/>
<input type="number" name="plannedOg" id="plannedOg" value=""/>
<label for="plannedOg">Gepland FG</label>
<input type="text" name="plannedFg" id="plannedFg" value=""/>
<input type="number" name="plannedFg" id="plannedFg" value=""/>
<button name="save" type="submit">Opslaan</button>
<a data-role="button" data-rel="back" data-icon="back">Sluiten</a>
</form>
Expand Down Expand Up @@ -122,7 +128,7 @@ <h3>Hadrianus tripel</h3>
</ul>
</div>
<script class="template" type="text/x-jquery-tmpl">
<li><a href="${hash}"><span class="name">${name}</span><span class="color">${color} EBC</span><span class="weight">${amount} g</span></a></li>
<li><a href="${hash}"><span class="name">${name}</span><span class="color">${color} EBC</span><span class="weight">${amount} g / ${percentage} %</span></a></li>
</script>
</section>

Expand Down
43 changes: 27 additions & 16 deletions public/scripts/controller.coffee
Expand Up @@ -135,17 +135,18 @@ class BrewGear.Controller.Recipe extends BaseRecipeController
batch: form.batch.value
name: form.name.value
style: @getStyle form.style.value
plannedOg: form.plannedOg.value
plannedFg: form.plannedFg.value
plannedOg: parseFloat form.plannedOg.value
plannedFg: parseFloat form.plannedFg.value
targetVolume: parseFloat form.targetVolume.value
@log "updated #{@model}"

back: =>
super
@update() if @modify

submit: (event) =>
super
@update()
super
# Delay a little and go to the details screen
setTimeout =>
window.location.hash = "/recipes/#{@id}"
Expand All @@ -159,6 +160,7 @@ class BrewGear.Controller.Recipe extends BaseRecipeController
form.name.value = @model.name or ''
form.plannedOg.value = @model.plannedOg or ''
form.plannedFg.value = @model.plannedFg or ''
form.targetVolume.value = @model.targetVolume or 10
form.style.value = @model.style?.name or ''
@renderBeerStyle()
batch = @model.batch
Expand Down Expand Up @@ -191,10 +193,12 @@ class BrewGear.Controller.Fermentables extends BaseRecipeController
@list.empty()
console.log ' model: ' + @model.fermentables
for i, fermentable of @model.fermentables
ctx = new BrewGear.Logic.MaltPercentage @model, fermentable
@list.append @template.tmpl
name: fermentable.source.name
color: fermentable.color
amount: fermentable.amount
color: fermentable.source.ebc
amount: Math.round fermentable.amount
percentage: Math.round ctx.percentage()
hash: "#/recipes/#{@model.batch}/fermentables/#{i}"
@newFermentableLink.attr('href', "#/recipes/#{@model.batch}/fermentables")
@list.listview 'refresh'
Expand All @@ -208,7 +212,12 @@ class BrewGear.Controller.Fermentable extends BaseRecipeController
constructor: ->
super
@delegateEvent BrewGear.Model.Fermentable, ev, @renderFermentable for ev in ['refresh', 'change']
@modify = true if @index
@modify = true if @index?

refresh: =>
@recipe = BrewGear.Model.Recipe.findByAttribute('batch', @id)
@model = @recipe.fermentables[@index] if @index
@render() if @recipe

getMalt: (name) =>
BrewGear.Model.Fermentable.findByAttribute('name', name)
Expand All @@ -217,8 +226,10 @@ class BrewGear.Controller.Fermentable extends BaseRecipeController
if @isModified
form = @form.get(0)
fermentable =
source: @getMalt form.name.value
percentage: form.percentage.value
source: @getMalt form.source.value
console.log "new fermentable", fermentable
ctx = new BrewGear.Logic.MaltPercentage @recipe, fermentable
ctx.amountInPercentage form.percentage.value
if @modify
@recipe.fermentables[@index] = fermentable
else
Expand All @@ -233,19 +244,19 @@ class BrewGear.Controller.Fermentable extends BaseRecipeController
@update() if @modify

submit: =>
super
@update()

refresh: =>
@recipe = BrewGear.Model.Recipe.findByAttribute('batch', @id)
@model = @recipe.fermentables[@index] if @index
@render() if @model
@recipe.save()
super

render: =>
@renderFermentable()
form = @form.get(0)
form.source.value = @model.source.name or ''
form.percentage.value = @model.amount or ''
form.source.value = @model?.source.name or ''
if @modify
ctx = new BrewGear.Logic.MaltPercentage @recipe, @model
form.percentage.value = ctx.percentage()
else
form.percentage.value = ''

renderFermentable: =>
@source.empty()
Expand Down
14 changes: 12 additions & 2 deletions public/scripts/logic.coffee
Expand Up @@ -10,7 +10,7 @@ sg_to_brix = (sg) ->
-0.0002112789 * sg * sg + 0.6907289 * sg - 479.4467


class BrewGear.Logic.GristPercentage
class BrewGear.Logic.MaltPercentage

constructor: (@recipe, @grain) ->

Expand All @@ -22,16 +22,26 @@ class BrewGear.Logic.GristPercentage
plannedGU: ->
(@recipe.plannedOg - 1) * 1000

percentage: ->
extract_potential = POINTS_G_L * @grain.source.yield
total_gravity = @plannedGU() * @recipe.targetVolume

(100 * @grain.amount * extract_potential *
(if @grain.addDuring == 'mash' then efficiency else 1)) / total_gravity

amountInPercentage: (percentage) ->
"""
Percentage is in the range 0-100.
Provide amount to be added in grammes.
"""
console.log 'amountInPercentage', percentage
percentage = parseFloat(percentage) / 100
extract_potential = POINTS_G_L * @grain.source.yield
total_gravity = @plannedGU() * @recipe.targetVolume

@grain.amount = ((percentage / 100) * total_gravity) / (extract_potential *
@grain.amount = (percentage * total_gravity) / (extract_potential *
(if @grain.addDuring == 'mash' then efficiency else 1))
console.log 'amountInPercentage', @grain, percentage, extract_potential, total_gravity


# vim:sw=4:et:ai
10 changes: 2 additions & 8 deletions public/scripts/use.coffee
Expand Up @@ -3,21 +3,15 @@
loaded = [ 'use' ]
(mod) ->
retval = null
LiveJS?.addResource url
if mod not in loaded
loaded.push mod
url = mod + '.coffee'
jQuery.ajax
url: url
success: (result) ->
console.log "use: Loading #{mod} module"
try
retval = CoffeeScript.run(result)
catch err
console.log '** ERROR ** loading script on url', url
console.log err
return
finally
LiveJS?.addResource url
retval = CoffeeScript.run(result)
error: (jqXHR, textStatus, errorThrown) ->
console.log "use: Loading of module #{mod} failed: ", textStatus
async: false
Expand Down
7 changes: 2 additions & 5 deletions public/specs/grist.spec.coffee
Expand Up @@ -34,18 +34,15 @@ describe 'Modifying the grist', ->

it 'should allow grist to change', ->
f = recipe.fermentables[0]
ctx = new BrewGear.Logic.GristPercentage(recipe, f)
expect(f.amount).toBe(1230)
ctx = new BrewGear.Logic.MaltPercentage(recipe, f)
ctx.amountInPercentage(60)
expect(f.amount).toBeCloseTo(1940.5, 1)
expect(recipe.fermentables[1].amount).toBe(0)

it 'should provide a valid result once all fermentables are adjusted correctly', ->
f = recipe.fermentables[1]
ctx = new BrewGear.Logic.GristPercentage recipe, f
ctx = new BrewGear.Logic.MaltPercentage recipe, f
ctx.amountInPercentage(40)
expect(f.amount).toBeCloseTo(1293.7, 1)
expect(ctx.percentage()).toBe(40)
expect(1940.5 / (1940.5 + 1293.7)).toBeCloseTo(0.60)

# vim:sw=4:et:ai
24 changes: 1 addition & 23 deletions recipes/1
@@ -1,23 +1 @@
{
"id": "1",
"name": "MyBeer",
"batch": "34",
"style": {
"name": "Blond"
},
"fermentables": [{
"source": {
"name": "Pale malt"
},
"color": 12,
"amount": 1234
}, {
"source": {
"name": "Cara 120"
},
"color": 120,
"amount": 200
}],
"hops": [
]
}
{"name":"MyBeer","batch":"34","style":{"name":"Blond","description":"Hooggegist, meestal met nagisting op fles","gravity":[1053,1068],"alcohol":[6,7.4],"attenuation":[80,85],"ibu":[15,30],"co2g":[0.39,0.74],"co2v":[2,3.8],"ph":[4.1,4.4],"class":"C","id":"c-40"},"plannedOg":1.06,"plannedFg":1.01,"fermentables":[{"source":{"name":"Tarwemout donker","yield":0.8007,"moisture":0.04,"ebc":31,"category":"Dingemans","id":"c-3"},"amount":2520.433689086088}],"hops":[],"targetVolume":20,"id":"1"}

0 comments on commit 506ae69

Please sign in to comment.