diff --git a/app/assets/javascripts/recipes.js b/app/assets/javascripts/recipes.js index b53ab14..cad7acf 100644 --- a/app/assets/javascripts/recipes.js +++ b/app/assets/javascripts/recipes.js @@ -1,25 +1,72 @@ (function($) { function reorder($container) { + console.log($container); $container.find("div.nested-fields").each(function(idx, editor) { var $editor = $(editor); - $editor.find('input.sort-order').val(idx + 1).trigger("changed"); + $editor.find('input.sort_order').val(idx + 1).trigger("changed"); }) } function initializeIngredientEditor($container, ingredientSearchEngine) { - $container.find(".ingredient-typeahead").typeahead({ + // $container is either an element that contains many editors, or a single editor. + var $editors = $container.find(".ingredient-typeahead").closest(".nested-fields"); - }, - { - name: 'ingredients', - source: ingredientSearchEngine, - display: function(datum) { - return datum.name; + $editors.each(function(idx, elem) { + var $editor = $(elem); + var $ingredientId = $editor.find("input.ingredient_id"); + var $customDensity = $editor.find("input.custom_density"); + var $group = $editor.find("div.typeahead-group"); + + $editor.find(".ingredient-typeahead").typeahead({}, + { + name: 'ingredients', + source: ingredientSearchEngine, + display: function(datum) { + return datum.name; + } + }); + + if ($ingredientId.val().length) { + $customDensity.prop('disabled', true); + $group.addClass("has-success"); } }); } + function ingredientItemPicked($typeahead, datum) { + var $container = $typeahead.closest(".nested-fields"); + var $ingredientId = $container.find("input.ingredient_id"); + var $customDensity = $container.find("input.custom_density"); + var $group = $container.find("div.typeahead-group"); + + $ingredientId.val(datum.id); + $typeahead.typeahead('val', datum.name); + $customDensity.val(datum.density).prop('disabled', true); + $group.addClass("has-success"); + } + + function ingredientNameChange($typeahead, ingredientSearchEngine) { + var $container = $typeahead.closest(".nested-fields"); + var $ingredientId = $container.find("input.ingredient_id"); + var $customDensity = $container.find("input.custom_density"); + var $group = $container.find("div.typeahead-group"); + + var id = $ingredientId.val(); + var value = $typeahead.typeahead('val'); + + if (id && id.length) { + var found = ingredientSearchEngine.get([id]); + if (found && found[0] && found[0].name != value) { + // User has chosen something custom + $ingredientId.val(''); + $customDensity.val('').prop('disabled', false); + + $group.removeClass("has-success"); + } + } + } + $(document).on("ready page:load", function() { var ingredientSearchEngine = new Bloodhound({ @@ -66,11 +113,23 @@ $ingredientList .on("cocoon:after-insert", function(e, item) { - reorder($(this)); + reorder($ingredientList); initializeIngredientEditor(item, ingredientSearchEngine); }) .on("cocoon:after-remove", function(e, item) { - reorder($(this)); + reorder($ingredientList); + }) + .on("typeahead:change", function(evt, value) { + console.log("changed"); + ingredientNameChange($(evt.target), ingredientSearchEngine); + }) + .on("typeahead:select", function(evt, value) { + console.log("selected"); + ingredientItemPicked($(evt.target), value); + }) + .on("typeahead:autocomplete", function(evt, value) { + console.log("autocomplete"); + ingredientItemPicked($(evt.target), value); }); }); diff --git a/app/assets/stylesheets/recipes.scss b/app/assets/stylesheets/recipes.scss index 84856d2..55878ba 100644 --- a/app/assets/stylesheets/recipes.scss +++ b/app/assets/stylesheets/recipes.scss @@ -3,7 +3,6 @@ // You can use Sass (SCSS) here: http://sass-lang.com/ @mixin editor { - @extend .panel-body; } div.ingredient-editor { diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index 5de893c..dec2aef 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -1,5 +1,5 @@ class IngredientsController < ApplicationController - before_action :set_ingredient, only: [:show, :edit, :update, :destroy] + before_action :set_ingredient, only: [:edit, :update, :destroy] # GET /ingredients # GET /ingredients.json @@ -7,11 +7,6 @@ class IngredientsController < ApplicationController @ingredients = Ingredient.all.order(:name) end - # GET /ingredients/1 - # GET /ingredients/1.json - def show - end - # GET /ingredients/new def new @ingredient = Ingredient.new diff --git a/app/models/recipe_ingredient.rb b/app/models/recipe_ingredient.rb index c157510..7b58012 100644 --- a/app/models/recipe_ingredient.rb +++ b/app/models/recipe_ingredient.rb @@ -6,4 +6,20 @@ class RecipeIngredient < ActiveRecord::Base validates :sort_order, presence: true validates :custom_density, density: true, allow_blank: true + def custom_name + if self.ingredient_id.present? + self.ingredient.name + else + super + end + end + + def custom_density + if self.ingredient_id.present? + self.ingredient.density + else + super + end + end + end diff --git a/app/views/ingredients/edit.html.erb b/app/views/ingredients/edit.html.erb index 039d4ab..e600543 100644 --- a/app/views/ingredients/edit.html.erb +++ b/app/views/ingredients/edit.html.erb @@ -5,7 +5,6 @@ <%= render 'form' %> - <%= link_to 'Show', @ingredient, class: 'btn btn-primary' %> | <%= link_to 'Back', ingredients_path, class: 'btn btn-primary' %> diff --git a/app/views/ingredients/index.html.erb b/app/views/ingredients/index.html.erb index 70447e7..f1811cb 100644 --- a/app/views/ingredients/index.html.erb +++ b/app/views/ingredients/index.html.erb @@ -16,10 +16,8 @@
<% @ingredients.each do |ingredient| %><%= @ingredient.notes %>
- - <%= link_to 'Edit', edit_ingredient_path(@ingredient), class: 'btn btn-default' %> - <%= link_to 'Back', ingredients_path, class: 'btn btn-default' %> - -