w
This commit is contained in:
parent
2fd83a5d3d
commit
b802542869
@ -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
|
||||
|
9
app/controllers/ingredients_controller.rb
Normal file
9
app/controllers/ingredients_controller.rb
Normal file
@ -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
|
@ -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.
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
|
15
app/views/ingredients/search.json.jbuilder
Normal file
15
app/views/ingredients/search.json.jbuilder
Normal file
@ -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
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user