From b8025428694e9346fcee3cee588eeb557af7484c Mon Sep 17 00:00:00 2001 From: Dan Elbert Date: Wed, 12 Sep 2018 09:43:50 -0500 Subject: [PATCH] w --- app/controllers/foods_controller.rb | 6 +----- app/controllers/ingredients_controller.rb | 9 +++++++++ app/controllers/recipes_controller.rb | 2 +- app/javascript/lib/VuexResponsiveSync.js | 23 +++++++++++++--------- app/models/recipe.rb | 10 ++++++++++ app/views/ingredients/search.json.jbuilder | 15 ++++++++++++++ app/views/recipes/_recipe.json.jbuilder | 6 +++--- config/routes.rb | 7 +++++-- 8 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 app/controllers/ingredients_controller.rb create mode 100644 app/views/ingredients/search.json.jbuilder diff --git a/app/controllers/foods_controller.rb b/app/controllers/foods_controller.rb index 19cc9ed..2abbde6 100644 --- a/app/controllers/foods_controller.rb +++ b/app/controllers/foods_controller.rb @@ -95,10 +95,6 @@ class FoodsController < ApplicationController render :show end - def search - @foods = Food.search(params[:query]).order(:name) - end - def convert @conversion = Conversion.new(conversion_params) @@ -127,7 +123,7 @@ class FoodsController < ApplicationController # Never trust parameters from the scary internet, only allow the white list through. def food_params - params.require(:food).permit(:name, :notes, :ndbn, :density, :water, :protein, :lipids, :carbohydrates, :kcal, :fiber, :sugar, :calcium, :sodium, :vit_k, :ash, :iron, :magnesium, :phosphorus, :potassium, :zinc, :copper, :manganese, :vit_c, :vit_b6, :vit_b12, :vit_a, :vit_e, :vit_d, :cholesterol, :ingredient_units_attributes => [:name, :gram_weight, :id, :_destroy]) + params.require(:food).permit(:name, :notes, :ndbn, :density, :water, :protein, :lipids, :carbohydrates, :kcal, :fiber, :sugar, :calcium, :sodium, :vit_k, :ash, :iron, :magnesium, :phosphorus, :potassium, :zinc, :copper, :manganese, :vit_c, :vit_b6, :vit_b12, :vit_a, :vit_e, :vit_d, :cholesterol, :food_units_attributes => [:name, :gram_weight, :id, :_destroy]) end def conversion_params diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb new file mode 100644 index 0000000..4f02103 --- /dev/null +++ b/app/controllers/ingredients_controller.rb @@ -0,0 +1,9 @@ +class IngredientsController < ApplicationController + + def search + @ingredients = Food.search(params[:query]).order(:name).to_a + @ingredients.concat(Recipe.search_by_name(params[:query]).order(:name).to_a) + @ingredients.sort { |a, b| a.name <=> b.name } + end + +end \ No newline at end of file diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb index 86b1631..2be59a0 100644 --- a/app/controllers/recipes_controller.rb +++ b/app/controllers/recipes_controller.rb @@ -81,7 +81,7 @@ class RecipesController < ApplicationController private # Use callbacks to share common setup or constraints between actions. def set_recipe - @recipe = Recipe.includes(recipe_ingredients: {ingredient: :ingredient_units }).find(params[:id]) + @recipe = Recipe.includes(recipe_ingredients: {food: :food_units }).find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. diff --git a/app/javascript/lib/VuexResponsiveSync.js b/app/javascript/lib/VuexResponsiveSync.js index 13c14f4..01bcf96 100644 --- a/app/javascript/lib/VuexResponsiveSync.js +++ b/app/javascript/lib/VuexResponsiveSync.js @@ -5,16 +5,21 @@ const defaultOptions = { }; // Hard coded values taken directly from Bulma css +const mobileBp = 768; +const desktopBp = 1024; +const widscreenBp = 1216; +const fullHdBp = 1408; + const mediaQueries = { - mobile: "screen and (max-width: 768px)", - tablet: "screen and (min-width: 769px)", - tabletOnly: "screen and (min-width: 769px) and (max-width: 1023px)", - touch: "screen and (max-width: 1023px)", - desktop: "screen and (min-width: 1024px)", - desktopOnly: "screen and (min-width: 1024px) and (max-width: 1215px)", - widescreen: "screen and (min-width: 1216px)", - widescreenOnly: "screen and (min-width: 1216px) and (max-width: 1407px)", - fullhd: "screen and (min-width: 1408px)" + mobile: `screen and (max-width: ${mobileBp}px)`, + tablet: `screen and (min-width: ${mobileBp + 1}px)`, + tabletOnly: `screen and (min-width: ${mobileBp + 1}px) and (max-width: ${desktopBp - 1}px)`, + touch: `screen and (max-width: ${desktopBp - 1}px)`, + desktop: `screen and (min-width: ${desktopBp}px)`, + desktopOnly: `screen and (min-width: ${desktopBp}px) and (max-width: ${widscreenBp - 1}px)`, + widescreen: `screen and (min-width: ${widscreenBp}px)`, + widescreenOnly: `screen and (min-width: ${widscreenBp}px) and (max-width: ${fullHdBp - 1}px)`, + fullhd: `screen and (min-width: ${fullHdBp}px)` }; export default function(store, options) { diff --git a/app/models/recipe.rb b/app/models/recipe.rb index 96b7e82..ad2f516 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -187,6 +187,16 @@ class Recipe < ApplicationRecord query.page(criteria.page).per(criteria.per) end + def self.search_by_name(query) + tokens = query.to_s.split(' ') + + if tokens.empty? + Recipe.none + else + Recipe.matches_tokens(:name, tokens) + end + end + private def calculate_nutrition_data diff --git a/app/views/ingredients/search.json.jbuilder b/app/views/ingredients/search.json.jbuilder new file mode 100644 index 0000000..9093115 --- /dev/null +++ b/app/views/ingredients/search.json.jbuilder @@ -0,0 +1,15 @@ + +json.array! @ingredients do |i| + + json.extract! i, :name + + case i + when Recipe + json.id "R#{i.id}" + when Food + json.id "F#{i.id}" + else + json.id nil + end + +end \ No newline at end of file diff --git a/app/views/recipes/_recipe.json.jbuilder b/app/views/recipes/_recipe.json.jbuilder index be04ac8..6c361fc 100644 --- a/app/views/recipes/_recipe.json.jbuilder +++ b/app/views/recipes/_recipe.json.jbuilder @@ -7,10 +7,10 @@ json.rendered_steps MarkdownProcessor.render(recipe.step_text) json.tags recipe.tag_names json.ingredients recipe.recipe_ingredients do |ri| - json.extract! ri, :id, :ingredient_detail_id, :display_name, :name, :quantity, :units, :preparation, :sort_order + json.extract! ri, :id, :ingredient_id, :display_name, :name, :quantity, :units, :preparation, :sort_order - json.ingredient_detail do - if ri.food.nil? && ri.ingredient_as_recipe.nil? + json.ingredient do + if ri.food.nil? && ri.recipe_as_ingredient.nil? json.null! elsif ri.food json.extract! ri.food, :name, :density, :notes diff --git a/config/routes.rb b/config/routes.rb index 57e0365..36d3000 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,14 +15,13 @@ Rails.application.routes.draw do get :usda_food_search constraints format: 'json' do - get :search get :prefetch get :convert end end end - match '/ingredients(/:id)/select_ndbn' => 'ingredients#select_ndbn', via: [:post, :patch, :put] + match '/foods(/:id)/select_ndbn' => 'foods#select_ndbn', via: [:post, :patch, :put] resources :tags, only: [:index] do collection do @@ -57,6 +56,10 @@ Rails.application.routes.draw do get '/ingredient_search' => :ingredient_search end + scope '/ingredients', controller: :ingredients, as: :ingredients do + get '/search' => :search + end + namespace 'admin' do resources :users, except: [:new, :create] end