diff --git a/.ruby-version b/.ruby-version index fad066f..6550da6 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.5.0 \ No newline at end of file +2.4.3 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 59b886a..f540915 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM phusion/passenger-ruby25:latest +FROM phusion/passenger-ruby24:latest # Use baseimage-docker's init process. CMD ["/sbin/my_init"] diff --git a/Gemfile b/Gemfile index 23b69a7..696cb58 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,9 @@ gem 'pg', '~> 0.21.0' gem 'webpacker', '3.4.1' gem 'bootsnap', '>= 1.1.0', require: false -gem 'jbuilder', '~> 2.7' + +#gem 'jbuilder', '~> 2.7' +gem 'jbuilder', git: 'https://github.com/rails/jbuilder', branch: 'master' gem 'oj', '~> 3.5.0' @@ -13,6 +15,8 @@ gem 'kaminari', '~> 1.1.1' gem 'unitwise', '~> 2.2.0' gem 'redcarpet', '~> 3.4.0' +gem 'dalli', '~> 2.7.6' + # Use ActiveModel has_secure_password gem 'bcrypt', '~> 3.1.11' diff --git a/Gemfile.lock b/Gemfile.lock index 30485f1..019a2cd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,12 @@ +GIT + remote: https://github.com/rails/jbuilder + revision: a0dc0bd42737aaef3dd622d8338a816aed03c8ff + branch: master + specs: + jbuilder (2.7.0) + activesupport (>= 4.2.0) + multi_json (>= 1.2) + GEM remote: https://rubygems.org/ specs: @@ -50,6 +59,7 @@ GEM coderay (1.1.2) concurrent-ruby (1.0.5) crass (1.0.3) + dalli (2.7.7) database_cleaner (1.6.2) diff-lcs (1.3) erubi (1.7.1) @@ -78,9 +88,6 @@ GEM rspec (>= 2.99.0, < 4.0) i18n (1.0.0) concurrent-ruby (~> 1.0) - jbuilder (2.7.0) - activesupport (>= 4.2.0) - multi_json (>= 1.2) kaminari (1.1.1) activesupport (>= 4.1.0) kaminari-actionview (= 1.1.1) @@ -224,11 +231,12 @@ PLATFORMS DEPENDENCIES bcrypt (~> 3.1.11) bootsnap (>= 1.1.0) + dalli (~> 2.7.6) database_cleaner (~> 1.6.2) factory_bot_rails (~> 4.8.2) guard (~> 2.14.0) guard-rspec - jbuilder (~> 2.7) + jbuilder! kaminari (~> 1.1.1) oj (~> 3.5.0) pg (~> 0.21.0) diff --git a/app/models/recipe_ingredient.rb b/app/models/recipe_ingredient.rb index 31f51dc..d5bb4dc 100644 --- a/app/models/recipe_ingredient.rb +++ b/app/models/recipe_ingredient.rb @@ -1,7 +1,7 @@ class RecipeIngredient < ApplicationRecord belongs_to :ingredient, optional: true - belongs_to :recipe, inverse_of: :recipe_ingredients + belongs_to :recipe, inverse_of: :recipe_ingredients, touch: true validates :sort_order, presence: true diff --git a/app/views/ingredients/index.json.jbuilder b/app/views/ingredients/index.json.jbuilder index ed3a250..266b0b5 100644 --- a/app/views/ingredients/index.json.jbuilder +++ b/app/views/ingredients/index.json.jbuilder @@ -1,17 +1,20 @@ -json.extract! @ingredients, :total_count, :total_pages, :current_page -json.page_size @ingredients.limit_value +json.cache_root! [Ingredient.all, @ingredients] do -json.ingredients @ingredients do |i| - json.extract! i, :id, :name, :ndbn, :kcal - json.usda i.ndbn.present? + json.extract! @ingredients, :total_count, :total_pages, :current_page + json.page_size @ingredients.limit_value + + json.ingredients @ingredients 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 - 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 \ No newline at end of file diff --git a/app/views/recipes/index.json.jbuilder b/app/views/recipes/index.json.jbuilder index 10d8688..a415406 100644 --- a/app/views/recipes/index.json.jbuilder +++ b/app/views/recipes/index.json.jbuilder @@ -1,10 +1,13 @@ -json.extract! @recipes, :total_count, :total_pages, :current_page -json.page_size @recipes.limit_value +json.cache_root! [Recipe.all, @recipes] do -json.recipes @recipes do |r| - json.extract! r, :id, :name, :rating, :yields, :total_time, :active_time, :created_at, :updated_at + json.extract! @recipes, :total_count, :total_pages, :current_page + json.page_size @recipes.limit_value - json.tags r.tag_names -end + 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 \ No newline at end of file diff --git a/app/views/recipes/show.json.jbuilder b/app/views/recipes/show.json.jbuilder index 71c4c7b..af16b43 100644 --- a/app/views/recipes/show.json.jbuilder +++ b/app/views/recipes/show.json.jbuilder @@ -1,31 +1,34 @@ +json.cache_root! [@recipe] do -json.extract! @recipe, :id, :name, :rating, :yields, :total_time, :active_time, :created_at, :updated_at, :step_text + json.extract! @recipe, :id, :name, :rating, :yields, :total_time, :active_time, :created_at, :updated_at, :step_text -json.rendered_steps MarkdownProcessor.render(@recipe.step_text) + json.rendered_steps MarkdownProcessor.render(@recipe.step_text) -json.tags @recipe.tag_names + 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.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.ingredient.nil? - json.null! - else - json.extract! ri.ingredient, :id, :name, :density, :notes + json.ingredient do + if ri.ingredient.nil? + json.null! + else + json.extract! ri.ingredient, :id, :name, :density, :notes + 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 - 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 \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index 9b6f7d0..a0a37b5 100644 --- a/config/application.rb +++ b/config/application.rb @@ -24,6 +24,10 @@ module Parsley # -- all .rb files in that directory are automatically loaded. config.load_defaults 5.2 + if ENV['RAILS_USE_MEMCACHE'] == 'true' + config.cache_store = :mem_cache_store, ENV['RAILS_MEMCACHE_HOST'] || 'memcache' + end + # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. config.time_zone = 'Central Time (US & Canada)' diff --git a/config/environments/development.rb b/config/environments/development.rb index 57cdba7..ae6ca1c 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -14,11 +14,16 @@ Rails.application.configure do # Show full error reports. config.consider_all_requests_local = true + memcache_enabled = ENV['RAILS_USE_MEMCACHE'] == 'true' + # Enable/disable caching. By default caching is disabled. - if Rails.root.join('tmp/caching-dev.txt').exist? + if Rails.root.join('tmp/caching-dev.txt').exist? || memcache_enabled config.action_controller.perform_caching = true - config.cache_store = :memory_store + unless memcache_enabled + config.cache_store = :memory_store + end + config.public_file_server.headers = { 'Cache-Control' => 'public, max-age=172800' } diff --git a/docker-compose.yml b/docker-compose.yml index 9db182f..292c0ee 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,12 +8,21 @@ services: - POSTGRES_USER=parsley volumes: - sql-data:/var/lib/postgresql/data + + memcache: + image: memcached:1.5.4-alpine + command: memcached -m 512 + web: build: . + environment: + - RAILS_USE_MEMCACHE=true + - RAILS_LOG_TO_STDOUT=true ports: - "3000:80" links: - postgres + volumes: sql-data: driver: local \ No newline at end of file diff --git a/docker/nginx_env.conf b/docker/nginx_env.conf index 4d813f0..4d8e93c 100644 --- a/docker/nginx_env.conf +++ b/docker/nginx_env.conf @@ -1,3 +1,6 @@ +env RAILS_LOG_TO_STDOUT; +env RAILS_USE_MEMCACHE; +env RAILS_MEMCACHE_HOST; env PARSLEY_DB_HOST; env PARSLEY_DB_USER; env PARSLEY_DB_PASSWORD;