diff --git a/Gemfile b/Gemfile index e4c0be2..eaa8a28 100644 --- a/Gemfile +++ b/Gemfile @@ -16,6 +16,7 @@ gem 'bootstrap-sass', '~> 3.3.6' # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks gem 'turbolinks' gem 'jbuilder', '~> 2.0' +gem 'cocoon', '~> 1.2.6' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' diff --git a/Gemfile.lock b/Gemfile.lock index 0e0da76..f6c74a4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -47,6 +47,7 @@ GEM sass (>= 3.3.4) builder (3.2.2) byebug (8.2.1) + cocoon (1.2.6) coffee-rails (4.1.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.1.x) @@ -173,6 +174,7 @@ PLATFORMS DEPENDENCIES bootstrap-sass (~> 3.3.6) byebug + cocoon (~> 1.2.6) database_cleaner (~> 1.5.1) factory_girl_rails (~> 4.5.0) jbuilder (~> 2.0) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 7465856..4dcd517 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -14,4 +14,5 @@ //= require jquery_ujs //= require turbolinks //= require bootstrap-sprockets +//= require cocoon //= require_tree . diff --git a/app/assets/javascripts/recipes.js b/app/assets/javascripts/recipes.js new file mode 100644 index 0000000..88c8273 --- /dev/null +++ b/app/assets/javascripts/recipes.js @@ -0,0 +1,37 @@ +(function($) { + + function reorder($container) { + $container.find("div.nested-fields").each(function(idx, editor) { + var $editor = $(editor); + $editor.find('input.sort-order').val(idx + 1).trigger("changed"); + }) + } + + $(document).on("ready page:load", function() { + + $("#step-list") + .on("cocoon:after-insert", function(e, item) { + reorder($(this)); + }) + .on("cocoon:after-remove", function(e, item) { + reorder($(this)); + }); + + + $("#ingredient-list") + .on("cocoon:after-insert", function(e, item) { + reorder($(this)); + }) + .on("cocoon:after-remove", function(e, item) { + reorder($(this)); + }); + + $("#step-list").on('changed', 'input.sort-order', function() { + var $this = $(this); + var $span = $this.closest(".nested-fields").find(".sort-order-display"); + $span.html($this.val()); + }); + }); + + +})(jQuery); diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 3c02da2..793e66f 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -14,16 +14,26 @@ *= require_self */ - @import "bootstrap-sprockets"; -@import "sandstone/_variables"; +@import "spacelab/_variables"; @import "bootstrap"; -@import "sandstone/_bootswatch"; +@import "spacelab/_bootswatch"; + +$footer_height: 40px; + +html { + position: relative; + min-height: 100%; +} +body { + /* Margin bottom by footer height */ + margin-bottom: $footer_height; +} .footer { position: absolute; bottom: 0; width: 100%; - height: 30px; + height: $footer_height; background-color: #f5f5f5; } \ No newline at end of file diff --git a/app/assets/stylesheets/recipes.scss b/app/assets/stylesheets/recipes.scss index 1e10ffa..631f710 100644 --- a/app/assets/stylesheets/recipes.scss +++ b/app/assets/stylesheets/recipes.scss @@ -1,3 +1,19 @@ // Place all the styles related to the recipes controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ + +@mixin editor { + border: solid 1px grey; +} + +div.ingredient-editor { + @include editor; +} + +div.step-editor { + @include editor; +} + +div#ingredient-list, div#step-list { + padding-bottom: 15px; +} \ No newline at end of file diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb index 34b773e..ba87b24 100644 --- a/app/controllers/recipes_controller.rb +++ b/app/controllers/recipes_controller.rb @@ -69,6 +69,6 @@ class RecipesController < ApplicationController # Never trust parameters from the scary internet, only allow the white list through. def recipe_params - params[:recipe] + params.require(:recipe).permit(:name, :description, :source, :yields, :total_time, :active_time, recipe_ingredients_attributes: [:custom_name, :custom_density, :ingredient_id, :quantity, :units, :sort_order, :id, :_destroy], recipe_steps_attributes: [:step, :sort_order, :id, :_destroy]) end end diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb index 700e9ec..8fe1343 100644 --- a/app/models/ingredient.rb +++ b/app/models/ingredient.rb @@ -1,2 +1,5 @@ class Ingredient < ActiveRecord::Base + + validates :name, presence: true + end diff --git a/app/models/recipe.rb b/app/models/recipe.rb index 7bb7663..7669859 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -1,6 +1,15 @@ class Recipe < ActiveRecord::Base - has_many :recipe_ingredients - has_many :recipe_steps + has_many :recipe_ingredients, -> { order :sort_order }, inverse_of: :recipe, dependent: :destroy + has_many :recipe_steps, -> { order :sort_order }, inverse_of: :recipe, dependent: :destroy + + accepts_nested_attributes_for :recipe_ingredients, allow_destroy: true + accepts_nested_attributes_for :recipe_steps, allow_destroy: true + + validates :name, presence: true + validates :yields, numericality: true, allow_blank: true + validates :total_time, numericality: true, allow_blank: true + validates :active_time, numericality: true, allow_blank: true + end diff --git a/app/models/recipe_ingredient.rb b/app/models/recipe_ingredient.rb index be2530a..9c46ae7 100644 --- a/app/models/recipe_ingredient.rb +++ b/app/models/recipe_ingredient.rb @@ -1,6 +1,8 @@ class RecipeIngredient < ActiveRecord::Base belongs_to :ingredient - belongs_to :recipe + belongs_to :recipe, inverse_of: :recipe_ingredients + + validates :sort_order, presence: true end diff --git a/app/models/recipe_step.rb b/app/models/recipe_step.rb index 1e374bc..8248001 100644 --- a/app/models/recipe_step.rb +++ b/app/models/recipe_step.rb @@ -1,2 +1,8 @@ class RecipeStep < ActiveRecord::Base + + belongs_to :recipe, inverse_of: :recipe_steps + + validates :step, presence: true + validates :sort_order, presence: true + end diff --git a/app/views/layouts/_flash_messages.html.erb b/app/views/layouts/_flash_messages.html.erb index 0af5393..b145386 100644 --- a/app/views/layouts/_flash_messages.html.erb +++ b/app/views/layouts/_flash_messages.html.erb @@ -3,7 +3,7 @@ <% unless flash.empty? %>