diff --git a/app/assets/javascripts/ingredients.js b/app/assets/javascripts/ingredients.js new file mode 100644 index 0000000..aa49ca3 --- /dev/null +++ b/app/assets/javascripts/ingredients.js @@ -0,0 +1,43 @@ +(function($) { + + var usdaFoodSearchEngine = new Bloodhound({ + initialize: false, + datumTokenizer: function(datum) { + return Bloodhound.tokenizers.whitespace(datum.name); + }, + queryTokenizer: Bloodhound.tokenizers.whitespace, + identify: function(datum) { return datum.ndbn; }, + sorter: function(a, b) { + if (a.name < b.name) { + return -1; + } else if (b.name < a.name) { + return 1; + } else { + return 0; + } + }, + remote: { + url: '/ingredients/usda_food_search.json?query=%QUERY', + wildcard: '%QUERY' + } + }); + + $(document).on("ready page:load", function() { + var $ingredientForm = $("#ingredient_form"); + + if ($ingredientForm.length) { + usdaFoodSearchEngine.initialize(false); + } + + $ingredientForm.find(".ndbn_typeahead").typeahead_selector({ + + },{ + name: 'usdaFoods', + source: usdaFoodSearchEngine, + display: function(datum) { + return datum.name; + } + }); + }); + +})(jQuery); \ No newline at end of file diff --git a/app/assets/javascripts/recipes.js b/app/assets/javascripts/recipes.js index f529e56..99394d9 100644 --- a/app/assets/javascripts/recipes.js +++ b/app/assets/javascripts/recipes.js @@ -1,5 +1,31 @@ (function($) { + var ingredientSearchEngine = new Bloodhound({ + initialize: false, + datumTokenizer: function(datum) { + return Bloodhound.tokenizers.whitespace(datum.name); + }, + queryTokenizer: Bloodhound.tokenizers.whitespace, + identify: function(datum) { return datum.id; }, + sorter: function(a, b) { + if (a.name < b.name) { + return -1; + } else if (b.name < a.name) { + return 1; + } else { + return 0; + } + }, + prefetch: { + url: '/ingredients/prefetch.json', + cache: false + }, + remote: { + url: '/ingredients/search.json?query=%QUERY', + wildcard: '%QUERY' + } + }); + function reorder($container) { $container.find("div.nested-fields").each(function(idx, editor) { var $editor = $(editor); @@ -104,33 +130,13 @@ $(document).on("ready page:load", function() { - var ingredientSearchEngine = new Bloodhound({ - datumTokenizer: function(datum) { - return Bloodhound.tokenizers.whitespace(datum.name); - }, - queryTokenizer: Bloodhound.tokenizers.whitespace, - identify: function(datum) { return datum.id; }, - sorter: function(a, b) { - if (a.name < b.name) { - return -1; - } else if (b.name < a.name) { - return 1; - } else { - return 0; - } - }, - prefetch: { - url: '/ingredients/prefetch.json', - cache: false - }, - remote: { - url: '/ingredients/search.json?query=%QUERY', - wildcard: '%QUERY' - } - }); - + var $ingredientList = $("#ingredient-list"); var $stepList = $("#step-list"); + if ($ingredientList.length) { + ingredientSearchEngine.initialize(false); + } + initializeStepEditor($stepList); $stepList @@ -147,7 +153,6 @@ $span.html($this.val()); }); - var $ingredientList = $("#ingredient-list"); initializeIngredientEditor($ingredientList, ingredientSearchEngine); diff --git a/app/assets/javascripts/typeahead_selector.js b/app/assets/javascripts/typeahead_selector.js new file mode 100644 index 0000000..16a83c4 --- /dev/null +++ b/app/assets/javascripts/typeahead_selector.js @@ -0,0 +1,70 @@ +(function($) { + + var pluginName = "typeahead_selector"; + + var defaultOptions = { + }; + + var methods = { + initialize: function (opts, sources) { + + return this.each(function() { + var options = $.extend({}, defaultOptions, opts); + var $this = $(this); + $this.typeahead(opts, sources); + + $this + .on("typeahead:change", function(evt, value) { + privateMethods.change($this, value); + }) + .on("typeahead:select", function(evt, value) { + privateMethods.select($this, value); + }) + .on("typeahead:autocomplete", function(evt, value) { + privateMethods.autocomplete($this, value); + }); + }); + }, + + val: function() { + if (this.length) { + return $(this[0]).data('typeahead_selected'); + } else { + return null; + } + } + }; + + var privateMethods = { + change: function($this, value) { + var item = $this.data('typeahead_pending'); + if (item) { + $this.data('typeahead_pending', null); + $this.data('typeahead_selected', item); + $this.trigger("typeahead_selector:selected", item); + } else { + $this.data('typeahead_selected', null); + $this.trigger("typeahead_selector:invalid", value); + } + }, + + select: function($this, item) { + $this.data('typeahead_pending', item); + }, + + autocomplete: function($this, item) { + $this.data('typeahead_pending', item); + } + }; + + $.fn[pluginName] = function (method) { + if (methods[method]) { + return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); + } else if (typeof method === 'object' || ! method) { + return methods.initialize.apply(this, arguments); + } else { + $.error('Method ' + method + ' does not exist on jQuery.' + pluginName); + } + }; + +})(jQuery); \ No newline at end of file diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index 2afab75..f42198f 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -80,6 +80,10 @@ class IngredientsController < ApplicationController end end + def usda_food_search + @foods = UsdaFood.where("short_description LIKE ?", "%#{params[:query]}%").limit(50) + end + private # Use callbacks to share common setup or constraints between actions. def set_ingredient diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb index ff2852e..00121a6 100644 --- a/app/models/ingredient.rb +++ b/app/models/ingredient.rb @@ -5,6 +5,13 @@ class Ingredient < ActiveRecord::Base def set_usda_food(food) self.ndbn = food.ndbn + self.water = food.water + self.protein = food.protein + self.lipids = food.lipid + self.ash = food.ash + self.kcal = food.kcal + self.fiber = food.fiber + self.sugar = food.sugar self.density = calculate_density(food.gram_weight_1, food.gram_weight_desc_1) || calculate_density(food.gram_weight_2, food.gram_weight_desc_2) end diff --git a/app/views/ingredients/_form.html.erb b/app/views/ingredients/_form.html.erb index 9235d50..4632122 100644 --- a/app/views/ingredients/_form.html.erb +++ b/app/views/ingredients/_form.html.erb @@ -1,6 +1,6 @@ -<%= form_for(@ingredient) do |f| %> +<%= form_for(@ingredient, html: {id: 'ingredient_form'}) do |f| %> <%= render partial: 'shared/error_list', locals: {model: @ingredient} %> @@ -9,6 +9,47 @@ <%= f.text_field :name, class: 'form-control', autofocus: true %> +