begin removing jbuilder
This commit is contained in:
parent
f18c5a021c
commit
08df218a00
@ -8,6 +8,7 @@ class RecipesController < ApplicationController
|
||||
def index
|
||||
@criteria = ViewModels::RecipeCriteria.new(criteria_params)
|
||||
@recipes = Recipe.for_criteria(@criteria).includes(:tags)
|
||||
render json: RecipeSummarySerializer.for(@recipes, collection_name: 'recipes')
|
||||
end
|
||||
|
||||
# GET /recipes/1
|
||||
@ -38,6 +39,7 @@ class RecipesController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
render json: RecipeSerializer.for(@recipe)
|
||||
end
|
||||
|
||||
# POST /recipes
|
||||
@ -55,8 +57,8 @@ class RecipesController < ApplicationController
|
||||
# PATCH/PUT /recipes/1
|
||||
def update
|
||||
ensure_owner(@recipe) do
|
||||
if @recipe.update(recipe_params)
|
||||
@recipe.touch
|
||||
# Merge in updated_at to force the record to be dirty (in case only tags were changed)
|
||||
if @recipe.update(recipe_params.merge(updated_at: Time.now))
|
||||
render json: { success: true }
|
||||
else
|
||||
render json: @recipe.errors, status: :unprocessable_entity
|
||||
|
@ -19,8 +19,9 @@ export default {
|
||||
},
|
||||
|
||||
value: {
|
||||
required: true,
|
||||
type: Array
|
||||
required: false,
|
||||
type: String,
|
||||
default: ""
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -50,7 +50,7 @@
|
||||
<td><app-date-time :date-time="r.created_at" :show-time="false"></app-date-time></td>
|
||||
<td>
|
||||
<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>
|
||||
</button>
|
||||
|
||||
|
54
app/serializers/application_serializer.rb
Normal file
54
app/serializers/application_serializer.rb
Normal 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
|
69
app/serializers/recipe_serializer.rb
Normal file
69
app/serializers/recipe_serializer.rb
Normal 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
|
17
app/serializers/recipe_summary_serializer.rb
Normal file
17
app/serializers/recipe_summary_serializer.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user