begin removing jbuilder

This commit is contained in:
Dan Elbert 2020-08-06 21:23:31 -05:00
parent f18c5a021c
commit 08df218a00
6 changed files with 148 additions and 5 deletions

View File

@ -8,6 +8,7 @@ class RecipesController < ApplicationController
def index def index
@criteria = ViewModels::RecipeCriteria.new(criteria_params) @criteria = ViewModels::RecipeCriteria.new(criteria_params)
@recipes = Recipe.for_criteria(@criteria).includes(:tags) @recipes = Recipe.for_criteria(@criteria).includes(:tags)
render json: RecipeSummarySerializer.for(@recipes, collection_name: 'recipes')
end end
# GET /recipes/1 # GET /recipes/1
@ -38,6 +39,7 @@ class RecipesController < ApplicationController
end end
end end
render json: RecipeSerializer.for(@recipe)
end end
# POST /recipes # POST /recipes
@ -55,8 +57,8 @@ class RecipesController < ApplicationController
# PATCH/PUT /recipes/1 # PATCH/PUT /recipes/1
def update def update
ensure_owner(@recipe) do ensure_owner(@recipe) do
if @recipe.update(recipe_params) # Merge in updated_at to force the record to be dirty (in case only tags were changed)
@recipe.touch if @recipe.update(recipe_params.merge(updated_at: Time.now))
render json: { success: true } render json: { success: true }
else else
render json: @recipe.errors, status: :unprocessable_entity render json: @recipe.errors, status: :unprocessable_entity

View File

@ -19,8 +19,9 @@ export default {
}, },
value: { value: {
required: true, required: false,
type: Array type: String,
default: ""
} }
}, },

View File

@ -50,7 +50,7 @@
<td><app-date-time :date-time="r.created_at" :show-time="false"></app-date-time></td> <td><app-date-time :date-time="r.created_at" :show-time="false"></app-date-time></td>
<td> <td>
<app-dropdown hover v-if="isLoggedIn" class="is-right"> <app-dropdown hover v-if="isLoggedIn" class="is-right">
<button slot="button" class="button"> <button slot="button" class="button is-small">
<app-icon icon="menu"></app-icon> <app-icon icon="menu"></app-icon>
</button> </button>

View File

@ -0,0 +1,54 @@
class ApplicationSerializer
class CollectionSerializer < ApplicationSerializer
def initialize(items, serializer, opts = {})
super(items, opts)
@collection_name = opts[:collection_name]
@serializer = serializer
end
def serialize
list = @item.map { |i| @serializer.for(i).serialize }
if @collection_name && item.respond_to?(:total_pages)
{
total_count: item.total_count,
total_pages: item.total_pages,
current_page: item.current_page,
page_size: item.limit_value,
@collection_name.to_sym => list
}
else
list
end
end
end
def self.for(data, opts = {})
if data.respond_to?(:each)
CollectionSerializer.new(data, self, opts)
else
new(data)
end
end
attr_reader :item
def initialize(item, opts = {})
@item = item
@options = opts
end
def serialize
@item.as_json
end
def as_json(*args)
serialize
end
def to_json(*args)
Rails.cache.fetch(@item.cache_key, version: @item.cache_version) do
self.as_json(*args).to_json
end
end
end

View File

@ -0,0 +1,69 @@
class RecipeSerializer < ApplicationSerializer
def serialize
{
id: item.id,
name: item.name,
rating: item.rating,
yields: item.yields,
total_time: item.total_time,
active_time: item.active_time,
source: item.source,
is_ingredient: item.is_ingredient,
created_at: item.created_at,
updated_at: item.updated_at,
step_text: item.step_text,
converted_scale: item.converted_scale,
converted_system: item.converted_system,
converted_unit: item.converted_unit,
rendered_steps: MarkdownProcessor.render(item.step_text),
tags: item.tag_names,
ingredients: item.recipe_ingredients.map do |ri|
{
id: ri.id,
ingredient_id: ri.ingredient_id,
display_name: ri.display_name,
name: ri.name,
quantity: ri.quantity,
units: ri.units,
preparation: ri.preparation,
sort_order: ri.sort_order,
ingredient: serialize_ingredient(ri),
_destroy: false
}
end,
nutrition_data: {
errors: item.nutrition_data.errors,
nutrients: NutritionData::NUTRIENTS.select { |_, v| v.present? }.map do |name, label|
{
name: name,
label: label,
value: item.nutrition_data.send(name)
}
end
}
}
end
def serialize_ingredient(ri)
if ri.food.nil? && ri.recipe_as_ingredient.nil?
nil
elsif ri.food
{
name: ri.food.name,
density: ri.food.density,
notes: ri.food.notes
}
else
{
name: ri.recipe_as_ingredient.name
}
end
end
end

View File

@ -0,0 +1,17 @@
class RecipeSummarySerializer < ApplicationSerializer
def serialize
{
id: item.id,
name: item.name,
rating: item.rating,
yields: item.yields,
total_time: item.total_time,
active_time: item.active_time,
created_at: item.created_at,
updated_at: item.updated_at,
tags: item.tag_names
}
end
end