From 08df218a002652e621edc55de255a5fd910d10f6 Mon Sep 17 00:00:00 2001 From: Dan Elbert Date: Thu, 6 Aug 2020 21:23:31 -0500 Subject: [PATCH] begin removing jbuilder --- app/controllers/recipes_controller.rb | 6 +- app/javascript/components/AppSearchText.vue | 5 +- app/javascript/components/TheRecipeList.vue | 2 +- app/serializers/application_serializer.rb | 54 +++++++++++++++ app/serializers/recipe_serializer.rb | 69 ++++++++++++++++++++ app/serializers/recipe_summary_serializer.rb | 17 +++++ 6 files changed, 148 insertions(+), 5 deletions(-) create mode 100644 app/serializers/application_serializer.rb create mode 100644 app/serializers/recipe_serializer.rb create mode 100644 app/serializers/recipe_summary_serializer.rb diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb index 55902bb..964c35f 100644 --- a/app/controllers/recipes_controller.rb +++ b/app/controllers/recipes_controller.rb @@ -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 diff --git a/app/javascript/components/AppSearchText.vue b/app/javascript/components/AppSearchText.vue index fe1eba0..2c5b7c3 100644 --- a/app/javascript/components/AppSearchText.vue +++ b/app/javascript/components/AppSearchText.vue @@ -19,8 +19,9 @@ export default { }, value: { - required: true, - type: Array + required: false, + type: String, + default: "" } }, diff --git a/app/javascript/components/TheRecipeList.vue b/app/javascript/components/TheRecipeList.vue index c07283b..da5188b 100644 --- a/app/javascript/components/TheRecipeList.vue +++ b/app/javascript/components/TheRecipeList.vue @@ -50,7 +50,7 @@ - diff --git a/app/serializers/application_serializer.rb b/app/serializers/application_serializer.rb new file mode 100644 index 0000000..56439d4 --- /dev/null +++ b/app/serializers/application_serializer.rb @@ -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 \ No newline at end of file diff --git a/app/serializers/recipe_serializer.rb b/app/serializers/recipe_serializer.rb new file mode 100644 index 0000000..c9adc98 --- /dev/null +++ b/app/serializers/recipe_serializer.rb @@ -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 \ No newline at end of file diff --git a/app/serializers/recipe_summary_serializer.rb b/app/serializers/recipe_summary_serializer.rb new file mode 100644 index 0000000..224fd1a --- /dev/null +++ b/app/serializers/recipe_summary_serializer.rb @@ -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 \ No newline at end of file