more conversion away form jbuilder
This commit is contained in:
parent
08df218a00
commit
7dfa978c26
@ -6,6 +6,7 @@ module Admin
|
||||
|
||||
def index
|
||||
@users = User.order(:full_name)
|
||||
render json: UserSerializer.for(@users)
|
||||
end
|
||||
|
||||
def show
|
||||
|
@ -60,6 +60,7 @@ class CalculatorController < ApplicationController
|
||||
|
||||
def ingredient_search
|
||||
@foods = Food.has_density.search(params[:query]).order(:name)
|
||||
render json: @foods.map { |f| {id: f.id, name: f.name, density: f.density} }
|
||||
end
|
||||
|
||||
end
|
@ -1,6 +1,6 @@
|
||||
class FoodsController < ApplicationController
|
||||
|
||||
before_action :set_food, only: [:show, :edit, :update, :destroy]
|
||||
before_action :set_food, only: [:show, :update, :destroy]
|
||||
|
||||
before_action :ensure_valid_user, except: [:index, :show]
|
||||
|
||||
@ -11,20 +11,11 @@ class FoodsController < ApplicationController
|
||||
if params[:name].present?
|
||||
@foods = @foods.matches_tokens(:name, params[:name].split.take(4))
|
||||
end
|
||||
render json: FoodSummarySerializer.for(@foods, collection_name: 'foods')
|
||||
end
|
||||
|
||||
def show
|
||||
|
||||
end
|
||||
|
||||
# GET /foods/new
|
||||
def new
|
||||
@food = Food.new
|
||||
end
|
||||
|
||||
# GET /foods/1/edit
|
||||
def edit
|
||||
ensure_owner @food
|
||||
render json: FoodSerializer.for(@food)
|
||||
end
|
||||
|
||||
# POST /foods
|
||||
@ -92,27 +83,13 @@ class FoodsController < ApplicationController
|
||||
@food.set_usda_food(UsdaFood.find_by_ndbn(@food.ndbn))
|
||||
end
|
||||
|
||||
render :show
|
||||
end
|
||||
|
||||
def convert
|
||||
@conversion = Conversion.new(conversion_params)
|
||||
|
||||
if @conversion.valid?
|
||||
@output_quantity = @conversion.output_quantity
|
||||
@conversion = Conversion.new
|
||||
else
|
||||
@output_quantity = ''
|
||||
end
|
||||
render json: FoodSerializer.for(@food)
|
||||
end
|
||||
|
||||
def usda_food_search
|
||||
@foods = UsdaFood.search(params[:query]).limit(250).order(:long_description)
|
||||
|
||||
respond_to do |format|
|
||||
format.html { render :layout => false }
|
||||
format.json { }
|
||||
end
|
||||
render json: UsdaFoodSerializer.for(@foods)
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -4,6 +4,16 @@ class IngredientsController < ApplicationController
|
||||
@ingredients = Food.search(params[:query]).order(:name).to_a
|
||||
@ingredients.concat(Recipe.is_ingredient.search_by_name(params[:query]).order(:name).to_a)
|
||||
@ingredients.sort { |a, b| a.name <=> b.name }
|
||||
end
|
||||
|
||||
json = @ingredients.map do |i|
|
||||
{
|
||||
id: i.ingredient_id,
|
||||
ingredient_id: i.ingredient_id,
|
||||
name: i.name,
|
||||
density: i.density
|
||||
}
|
||||
end
|
||||
|
||||
end
|
||||
render json: json
|
||||
end
|
||||
end
|
||||
|
@ -8,14 +8,17 @@ class LogsController < ApplicationController
|
||||
|
||||
def index
|
||||
@logs = Log.for_user(current_user).order(date: :desc).page(params[:page]).per(params[:per])
|
||||
render json: LogSummarySerializer.for(@logs, collection_name: 'logs')
|
||||
end
|
||||
|
||||
def show
|
||||
ensure_owner(@log)
|
||||
render json: LogSerializer.for(@log)
|
||||
end
|
||||
|
||||
def edit
|
||||
ensure_owner(@log)
|
||||
render json: LogSerializer.for(@log)
|
||||
end
|
||||
|
||||
def update
|
||||
|
@ -1,8 +1,8 @@
|
||||
class RecipesController < ApplicationController
|
||||
|
||||
before_action :set_recipe, only: [:show, :edit, :update, :destroy]
|
||||
before_action :set_recipe, only: [:show, :update, :destroy]
|
||||
|
||||
before_action :ensure_valid_user, except: [:show, :scale, :index]
|
||||
before_action :ensure_valid_user, except: [:show, :index]
|
||||
|
||||
# GET /recipes
|
||||
def index
|
||||
|
@ -6,11 +6,12 @@ class TagsController < ApplicationController
|
||||
|
||||
def prefetch
|
||||
@tags = Tag.all.order(:name)
|
||||
render :search
|
||||
render json: TagSerializer.for(@tags)
|
||||
end
|
||||
|
||||
def search
|
||||
@tags = Tag.search(params[:query]).order(:name)
|
||||
render json: TagSerializer.for(@tags)
|
||||
end
|
||||
|
||||
end
|
@ -4,6 +4,11 @@ class UsersController < ApplicationController
|
||||
skip_before_action :verify_authenticity_token, only: [:verify_login]
|
||||
|
||||
def show
|
||||
if current_user
|
||||
render json: UserSerializer.for(current_user)
|
||||
else
|
||||
render json: nil
|
||||
end
|
||||
end
|
||||
|
||||
def login
|
||||
@ -26,7 +31,7 @@ class UsersController < ApplicationController
|
||||
if user = User.authenticate(params[:username], params[:password])
|
||||
set_current_user(user)
|
||||
format.html { redirect_to root_path, notice: "Welcome, #{user.display_name}" }
|
||||
format.json { render json: { success: true, user: { id: user.id, name: user.display_name, admin: user.admin? } } }
|
||||
format.json { render json: { success: true, user: UserSerializer.for(current_user).serialize } }
|
||||
else
|
||||
format.html { flash[:error] = "Invalid credentials"; render :login }
|
||||
format.json { render json: { success: false, message: 'Invalid Credentials', user: nil } }
|
||||
|
@ -3,11 +3,11 @@
|
||||
<h1 class="title">About</h1>
|
||||
|
||||
<p>
|
||||
A Recipe manager. Source available from my Git repository at <a href="https://source.elbert.us/dan/parsley">https://source.elbert.us</a>.
|
||||
A Recipe manager. Source available at <a href="https://source.elbert.us/dan/parsley">https://source.elbert.us</a>.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Parsley is released under the MIT License. All code © Dan Elbert 2018.
|
||||
Parsley is released under the MIT License. All code © Dan Elbert 2020.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
|
@ -1,5 +1,6 @@
|
||||
class ApplicationSerializer
|
||||
class CollectionSerializer < ApplicationSerializer
|
||||
|
||||
def initialize(items, serializer, opts = {})
|
||||
super(items, opts)
|
||||
@collection_name = opts[:collection_name]
|
||||
@ -38,8 +39,12 @@ class ApplicationSerializer
|
||||
@options = opts
|
||||
end
|
||||
|
||||
def item_properties(*keys)
|
||||
Hash[keys.map { |k| [k.to_sym, @item.send(k.to_sym)] }]
|
||||
end
|
||||
|
||||
def serialize
|
||||
@item.as_json
|
||||
@item.as_json
|
||||
end
|
||||
|
||||
def as_json(*args)
|
||||
@ -51,4 +56,4 @@ class ApplicationSerializer
|
||||
self.as_json(*args).to_json
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
56
app/serializers/food_serializer.rb
Normal file
56
app/serializers/food_serializer.rb
Normal file
@ -0,0 +1,56 @@
|
||||
class FoodSerializer < ApplicationSerializer
|
||||
|
||||
def serialize
|
||||
{
|
||||
**item_properties(:id,
|
||||
:name,
|
||||
:ndbn,
|
||||
:usda_food_name,
|
||||
:notes,
|
||||
:density,
|
||||
:water,
|
||||
:ash,
|
||||
:protein,
|
||||
:kcal,
|
||||
:fiber,
|
||||
:sugar,
|
||||
:carbohydrates,
|
||||
:calcium,
|
||||
:iron,
|
||||
:magnesium,
|
||||
:phosphorus,
|
||||
:potassium,
|
||||
:sodium,
|
||||
:zinc,
|
||||
:copper,
|
||||
:manganese,
|
||||
:vit_c,
|
||||
:vit_b6,
|
||||
:vit_b12,
|
||||
:vit_a,
|
||||
:vit_e,
|
||||
:vit_d,
|
||||
:vit_k,
|
||||
:cholesterol,
|
||||
:lipids),
|
||||
|
||||
ndbn_units: self.ndbn_units,
|
||||
food_units: item.food_units.map { |u| { id: u.id, name: u.name, gram_weight: u.gram_weight, _destroy: false } }
|
||||
}
|
||||
end
|
||||
|
||||
def ndbn_units
|
||||
if item.ndbn.present?
|
||||
item.usda_food.usda_food_weights.map do |fw|
|
||||
{
|
||||
amount: fw.amount,
|
||||
description: fw.description,
|
||||
gram_weight: fw.gram_weight
|
||||
}
|
||||
end
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
23
app/serializers/food_summary_serializer.rb
Normal file
23
app/serializers/food_summary_serializer.rb
Normal file
@ -0,0 +1,23 @@
|
||||
class FoodSummarySerializer < ApplicationSerializer
|
||||
|
||||
def serialize
|
||||
{
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
ndbn: item.ndbn,
|
||||
kcal: item.kcal,
|
||||
usda: item.ndbn.present?,
|
||||
density: pretty_density
|
||||
}
|
||||
end
|
||||
|
||||
def pretty_density
|
||||
if item.density.present?
|
||||
value = UnitConversion::parse(item.density)
|
||||
value.convert('oz/cup').change_formatter(UnitConversion::DecimalFormatter.new).pretty_value
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
end
|
13
app/serializers/log_serializer.rb
Normal file
13
app/serializers/log_serializer.rb
Normal file
@ -0,0 +1,13 @@
|
||||
class LogSerializer < ApplicationSerializer
|
||||
|
||||
def serialize
|
||||
{
|
||||
id: item.id,
|
||||
date: item.date,
|
||||
rating: item.rating,
|
||||
notes: item.notes,
|
||||
recipe: RecipeSerializer.for(item.recipe).serialize
|
||||
}
|
||||
end
|
||||
|
||||
end
|
16
app/serializers/log_summary_serializer.rb
Normal file
16
app/serializers/log_summary_serializer.rb
Normal file
@ -0,0 +1,16 @@
|
||||
class LogSummarySerializer < ApplicationSerializer
|
||||
|
||||
def serialize
|
||||
{
|
||||
id: item.id,
|
||||
date: item.date,
|
||||
rating: item.rating,
|
||||
notes: item.notes,
|
||||
recipe: {
|
||||
id: item.recipe.id,
|
||||
name: item.recipe.name
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
end
|
7
app/serializers/tag_serializer.rb
Normal file
7
app/serializers/tag_serializer.rb
Normal file
@ -0,0 +1,7 @@
|
||||
class TagSerializer < ApplicationSerializer
|
||||
def serialize
|
||||
{
|
||||
**item_properties(:id, :name)
|
||||
}
|
||||
end
|
||||
end
|
8
app/serializers/usda_food_serializer.rb
Normal file
8
app/serializers/usda_food_serializer.rb
Normal file
@ -0,0 +1,8 @@
|
||||
class UsdaFoodSerializer < ApplicationSerializer
|
||||
def serialize
|
||||
{
|
||||
**item_properties(:ndbn, :kcal, :carbohydrates, :lipid, :protein),
|
||||
name: item.long_description
|
||||
}
|
||||
end
|
||||
end
|
16
app/serializers/user_serializer.rb
Normal file
16
app/serializers/user_serializer.rb
Normal file
@ -0,0 +1,16 @@
|
||||
class UserSerializer < ApplicationSerializer
|
||||
|
||||
def serialize
|
||||
{
|
||||
id: item.id,
|
||||
username: item.username,
|
||||
email: item.email,
|
||||
full_name: item.full_name,
|
||||
name: item.display_name,
|
||||
admin: item.admin?,
|
||||
created_at: item.created_at,
|
||||
updated_at: item.updated_at
|
||||
}
|
||||
end
|
||||
|
||||
end
|
@ -1,5 +0,0 @@
|
||||
|
||||
json.array! @users do |u|
|
||||
json.extract! u, :id, :username, :full_name, :email, :admin
|
||||
json.name u.display_name
|
||||
end
|
@ -1,6 +0,0 @@
|
||||
|
||||
json.array! @foods do |i|
|
||||
|
||||
json.extract! i, :id, :name, :density
|
||||
|
||||
end
|
@ -1,4 +0,0 @@
|
||||
|
||||
json.success !@output_quantity.blank?
|
||||
json.output_quantity @output_quantity
|
||||
json.form_html render(partial: 'recipes/editor/conversion_form', formats: [:html])
|
@ -1,20 +0,0 @@
|
||||
|
||||
json.cache_root!(@foods, version: @foods.cache_version) do
|
||||
|
||||
json.extract! @foods, :total_count, :total_pages, :current_page
|
||||
json.page_size @foods.limit_value
|
||||
|
||||
json.foods @foods do |i|
|
||||
json.extract! i, :id, :name, :ndbn, :kcal
|
||||
json.usda i.ndbn.present?
|
||||
|
||||
if i.density.present?
|
||||
value = UnitConversion::parse(i.density)
|
||||
json.density value.convert('oz/cup').change_formatter(UnitConversion::DecimalFormatter.new).pretty_value
|
||||
else
|
||||
json.density nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
@ -1,6 +0,0 @@
|
||||
|
||||
json.array! @foods do |i|
|
||||
|
||||
json.extract! i, :id, :name, :density, :notes
|
||||
|
||||
end
|
@ -1,46 +0,0 @@
|
||||
|
||||
json.extract! @food,
|
||||
:id,
|
||||
:name,
|
||||
:ndbn,
|
||||
:usda_food_name,
|
||||
:notes,
|
||||
:density,
|
||||
:water,
|
||||
:ash,
|
||||
:protein,
|
||||
:kcal,
|
||||
:fiber,
|
||||
:sugar,
|
||||
:carbohydrates,
|
||||
:calcium,
|
||||
:iron,
|
||||
:magnesium,
|
||||
:phosphorus,
|
||||
:potassium,
|
||||
:sodium,
|
||||
:zinc,
|
||||
:copper,
|
||||
:manganese,
|
||||
:vit_c,
|
||||
:vit_b6,
|
||||
:vit_b12,
|
||||
:vit_a,
|
||||
:vit_e,
|
||||
:vit_d,
|
||||
:vit_k,
|
||||
:cholesterol,
|
||||
:lipids
|
||||
|
||||
if @food.ndbn.present?
|
||||
json.ndbn_units @food.usda_food.usda_food_weights do |fw|
|
||||
json.extract! fw, :amount, :description, :gram_weight
|
||||
end
|
||||
else
|
||||
json.ndbn_units []
|
||||
end
|
||||
|
||||
json.food_units @food.food_units do |iu|
|
||||
json.extract! iu, :id, :name, :gram_weight
|
||||
json._destroy false
|
||||
end
|
@ -1,8 +0,0 @@
|
||||
|
||||
json.array! @foods do |f|
|
||||
|
||||
json.extract! f, :ndbn, :kcal, :carbohydrates, :lipid, :protein
|
||||
json.name f.long_description
|
||||
|
||||
|
||||
end
|
@ -1,7 +0,0 @@
|
||||
|
||||
json.array! @ingredients do |i|
|
||||
|
||||
json.extract! i, :ingredient_id, :name, :density
|
||||
json.id i.ingredient_id
|
||||
|
||||
end
|
@ -1,16 +0,0 @@
|
||||
|
||||
json.cache_root!(@logs, version: @logs.cache_version) do
|
||||
|
||||
json.extract! @logs, :total_count, :total_pages, :current_page
|
||||
json.page_size @logs.limit_value
|
||||
|
||||
json.logs @logs do |l|
|
||||
json.extract! l, :id, :date, :rating, :notes
|
||||
|
||||
json.recipe do
|
||||
json.id l.recipe.id
|
||||
json.name l.recipe.name
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -1,6 +0,0 @@
|
||||
|
||||
json.extract! @log, :id, :date, :rating, :notes
|
||||
|
||||
json.recipe do
|
||||
json.partial! 'recipes/recipe', recipe: @log.recipe
|
||||
end
|
@ -1,34 +0,0 @@
|
||||
|
||||
|
||||
json.extract! recipe, :id, :name, :rating, :yields, :total_time, :active_time, :source, :is_ingredient, :created_at, :updated_at, :step_text, :converted_scale, :converted_system, :converted_unit
|
||||
|
||||
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_id, :display_name, :name, :quantity, :units, :preparation, :sort_order
|
||||
|
||||
json.ingredient do
|
||||
if ri.food.nil? && ri.recipe_as_ingredient.nil?
|
||||
json.null!
|
||||
elsif ri.food
|
||||
json.extract! ri.food, :name, :density, :notes
|
||||
else
|
||||
json.extract! ri.recipe_as_ingredient, :name
|
||||
end
|
||||
end
|
||||
|
||||
json._destroy false
|
||||
end
|
||||
|
||||
json.nutrition_data do
|
||||
json.errors recipe.nutrition_data.errors
|
||||
|
||||
json.nutrients NutritionData::NUTRIENTS.select { |_, v| v.present? } do |name, label|
|
||||
json.name name
|
||||
json.label label
|
||||
json.value recipe.nutrition_data.send(name)
|
||||
end
|
||||
|
||||
end
|
@ -1,13 +0,0 @@
|
||||
|
||||
json.cache_root!(@recipes.cache_key, version: @recipes.cache_version) do
|
||||
|
||||
json.extract! @recipes, :total_count, :total_pages, :current_page
|
||||
json.page_size @recipes.limit_value
|
||||
|
||||
json.recipes @recipes do |r|
|
||||
json.extract! r, :id, :name, :rating, :yields, :total_time, :active_time, :created_at, :updated_at
|
||||
|
||||
json.tags r.tag_names
|
||||
end
|
||||
|
||||
end
|
@ -1,5 +0,0 @@
|
||||
json.cache_root!(@recipe, version: @recipe.cache_version) do
|
||||
|
||||
json.partial! 'recipe', recipe: @recipe
|
||||
|
||||
end
|
@ -1,8 +0,0 @@
|
||||
|
||||
if current_user
|
||||
json.extract! current_user, :id, :username, :email, :full_name, :admin
|
||||
|
||||
json.name current_user.display_name
|
||||
else
|
||||
json.nil!
|
||||
end
|
Loading…
Reference in New Issue
Block a user