Skip to content

Commit

Permalink
4/5 controller(CrUd), 6/8 model revoke versus assert not responding, …
Browse files Browse the repository at this point in the history
…need to make sure views look ok
  • Loading branch information
Mirv committed Jun 16, 2017
1 parent d2b5854 commit 238a6fc
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 67 deletions.
99 changes: 99 additions & 0 deletions .byebug_history
Original file line number Diff line number Diff line change
@@ -1,4 +1,103 @@
exit
result
result.first
next
opts
self
user
next
env[ENV_SESSION_OPTIONS]
next
user
step
status
step
next
name
step
sql
name
step
name
step
Thread.current.status
next
name
step
attr_name
step
attributes[name]
step
value
step
attr
step
other
step
value
step
name
step
self
value
step
env["rack.errors"].inspect
env["rack.errors"].debug
env["rack.errors"].name
env["rack.errors"]
env
step
set_host(v)
v
@host
step
@user
step
value
step
k
step
value
step
str
step
value
step
params
step
@@schemes
step
path
parameters
step
@value
step
attributes
step
@value
@valu
step
@recipe
exit
step
ivar
step
@path_helper_modules
step
exception
step
exception
step
exception
step
app
step
@integration_session
step
@integration_session
step
@recipe
exit
eixt
step
eval recipes
Expand Down
70 changes: 58 additions & 12 deletions app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class RecipesController < ApplicationController
before_action :authenticate_user!, except: [:show, :index, :like, :search]
before_action :require_same_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
# before_action :check_params

def search
if params[:search].present?
Expand Down Expand Up @@ -50,6 +51,8 @@ def update
flash[:success] = "Your recipe was update success"
redirect_to recipe_path(@recipe)
else
puts "\n\n Recipe debug: #{YAML::dump(params)} \n\n"

render :edit
end
end
Expand All @@ -66,7 +69,6 @@ def like
end

def destroy

@recipe.destroy!
flash[:success] = "Recipe Deleted"
redirect_to recipes_path
Expand All @@ -93,19 +95,25 @@ def set_user
@user = User.find(current_user.id)
end

# name: , summary: , description: , prep_times: , servings_made:
def recipe_params
# raise recipe_params.inspect
params.require(:recipe).permit(
:name,
:summary,
:description,
:prep_times,
:servings_made,
:feeds,
:user_id,

params.require(:recipe).permit(:id, :user_id, :name, :summary, :description,
:prep_times, :servings_made,
:picture, :chef_id,
style_ids: [],
ingredients_attributes: [:id, :name, :_destroy],
directions_attributes: [:id, :name, :step, :_destroy],
feed_ids: [],
calorie_ids: [],
preptime_ids: [])
# :picture,
# :chef_id,
# style_ids: [],
# ingredients_attributes: [:id, :name, :_destroy],
# directions_attributes: [:id, :name, :step, :_destroy],
# feed_ids: [],
# calorie_ids: [],
# preptime_ids: []
)
end

def set_recipe
Expand All @@ -124,4 +132,42 @@ def admin_user
redirect_to recipes_path unless current_user.admin?
end

# def check_params
# required = [
# :id,
# :user_id,
# :name,
# :summary,
# :description,
# :prep_times,
# :servings_made
# ]
# if required.all? {|k, m| params.has_key? k}
# # here you know params has all the keys defined in required array
# puts "\n\n\All params set\n\n"
# else
# puts "\n\n\One of params not set\n\n"
# puts YAML::dump(params)
# end
# end
# def check_params
# required = [
# :id,
# :user_id,
# :name,
# :summary,
# :description,
# :prep_times,
# :servings_made
# ]
# visible_required = []
# # puts "----Doing required.each----"
# required.each { |k| if (params.has_key? k) then visible_required << k else puts "was wrong ... #{k}" end }
# # puts "----Doing Passed----"
# visible_required.each { |k| puts "This is .... #{k}" }
# puts "----Doing params.each----"
# params.each { |k, m| puts "Key: #{k} -- value: #{m}" }
# # puts "\n\n Recipe debug: #{YAML::dump(params)} \n\n"
# # puts params.inspect
# end
end
18 changes: 9 additions & 9 deletions app/models/recipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class Recipe < ActiveRecord::Base
has_many :recipe_preptimes
has_many :preptimes, through: :recipe_preptimes
has_many :recipe_feeds
has_many :feeds, through: :recipe_feeds
# Going to a integer & no relationship
# has_many :feeds, through: :recipe_feeds
# searchkick
accepts_nested_attributes_for :ingredients,
reject_if: proc { |attributes| attributes['name'].blank? },
Expand All @@ -21,19 +22,18 @@ class Recipe < ActiveRecord::Base
reject_if: proc { |attributes| attributes['step'].blank? },
allow_destroy: true

# This one was erroring the system out
# validates :user_id, presence: true

validates :user_id, presence: true
# validates :directions, presence: true
# validates :ingredients, presence: true
# validates :feeds, presence: true
validates :servings_made, presence: true
validates :prep_times, presence: true
validates :name, presence: true, length: { minimum: 5, maximum: 100}
validates :summary, presence: true, length: { minimum: 10, maximum: 150}
validates :description, presence: true, length: { minimum: 5, maximum: 1000}
mount_uploader :picture, PictureUploader
validate :picture_size
validates :servings_made, presence: true
validates :prep_times, presence: true
validates :feeds, presence: true

# mount_uploader :picture, PictureUploader
# validate :picture_size

def thumbs_up_total
self.likes.where(like: true).size
Expand Down
6 changes: 3 additions & 3 deletions app/views/recipes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
<% if recipe.ingredients.any? %>
<p>Ingredients: <%= render recipe.ingredients %></p>
<% end %>
<% if recipe.feeds.any? %>
<p>Feeds: <%= render recipe.feeds %></p>
<% end %>

<p>Feeds: <%= recipe.feeds %></p>

<% if recipe.preptimes.any? %>
<p>Preptime: <%= render recipe.preptimes %></p>
<% end %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/recipes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
<% if @recipe.styles.any? %>
<p>Styles: <%= render @recipe.styles %></p>
<% end %>
<% if @recipe.feeds.any? %>
<p>Feeds: <%= render @recipe.feeds %></p>
<% end %>

<p>Feeds: <%= @recipe.feeds %></p>

<% if @recipe.preptimes.any? %>
<p>Preptime: <%= render @recipe.preptimes %></p>
<% end %>
Expand Down
30 changes: 30 additions & 0 deletions error_result.rb

Large diffs are not rendered by default.

27 changes: 12 additions & 15 deletions test/controllers/recipes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,19 @@ class RecipesControllerTest < ActionDispatch::IntegrationTest
end

test "should update recipe" do
@recipe = recipes(:one)
sign_in @user
@recipe = recipes(:one)

patch recipe_url(@recipe), params: { recipe: {id: @recipe.id,
name: @recipe.name, summary: @recipe.summary,
description: @recipe.description, prep_times: @recipe.prep_times,
servings_made: @recipe.servings_made} }
patch recipe_url(@recipe), recipe: {
name: "This is a name",
summary: @recipe.summary,
description: @recipe.description,
prep_times: @recipe.prep_times,
servings_made: @recipe.servings_made,
feeds: 3,
user_id: 1
}
assert_redirected_to recipe_url(@recipe)
end

test "should create recipe" do
assert_difference('Recipe.count') do
# puts "\n\n Body Response #{@body.response} \n\n"
post recipes_url, params: { recipe: { name: "SOMEONE", summary: "Something Else",
description: "Something Else", prep_times: "90", servings_made: "3", user_id: "1"} }
end

assert_redirected_to recipe_path(Recipe.last)

end
end
5 changes: 3 additions & 2 deletions test/fixtures/recipes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

one:
name: MyString1
summary: MyText2
summary: MyText2222222
description: MyText
prep_times: MyTest
servings_made: 10
# feeds: 3
# picture:
user_id: one
user_id: 1

two:
name: MyString2
Expand Down
57 changes: 34 additions & 23 deletions test/models/recipe_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,48 @@ class RecipeTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
setup do
@recipe = Recipe.new(name: "SOMEONE", summary: "Something Else",
description: "Something Else", prep_times: "90", servings_made: "3",
feeds: "3", user_id: 1)
end

test "recipe is valid" do
recipe = Recipe.new(name: "SOMEONE", summary: "Something Else",
description: "Something Else", prep_times: "90", servings_made: "3")
assert recipe.valid?, 'recipe is not valid'
test "@recipe is valid" do
assert @recipe.valid?, '@recipe is not valid'
end
test "recipe should be invalid without description" do
recipe = Recipe.new(name: "Something", summary: "Something Else")
assert recipe.invalid?, 'recipe should be invalid, but passed validation'

test "@recipe should be invalid without name" do
@recipe["name"] = ""
assert @recipe.invalid?, '@recipe should be invalid without name, but passed validation'
end

test "recipe should be invalid without summary" do
recipe = Recipe.new(name: "Something", description: "Something Else")
assert recipe.invalid?, 'recipe should be invalid, but passed validation'
test "@recipe should be invalid without summary" do
@recipe["summary"] = ""
assert @recipe.invalid?, '@recipe should be invalid without summary, but passed validation'
end

test "recipe should be invalid without name" do
recipe = Recipe.new(summary: "Something Else", description: "Something Else")
assert recipe.invalid?, 'recipe should be invalid, but passed validation'

test "@recipe should be invalid without description" do
@recipe["description"] = ""
assert @recipe.invalid?, '@recipe should be invalid without description, but passed validation'
end

test "@recipe should be invalid without preptime" do
@recipe["prep_times"] = ""
assert @recipe.invalid?, '@recipe should be invalid - missing prep_times, but passed validation'
end

test "recipe should be invalid without preptime" do
recipe = Recipe.new(name: "Something new", summary: "Something Else",
description: "Something Else", servings_made: "3")
assert recipe.invalid?, 'recipe should be invalid - missing prep_times, but passed validation'
test "@recipe should be invalid without servings_made" do
@recipe["servings_made"] = ""
refute @recipe.valid?, '@recipe should be invalid - missing servings_made, but passed validation'
end

test "recipe should be invalid without servings_made" do
recipe = Recipe.new(name: "SOMEONE", summary: "Something Else",
description: "Something Else", prep_times: "Something")
refute recipe.valid?, 'recipe should be invalid - missing servings_made, but passed validation'
test "@recipe should be invalid without feeds" do
@recipe["feeds"] = ""
refute @recipe.valid?, '@recipe should be invalid - missing feeds, but passed validation'
end

test "@recipe should be invalid without user_id" do
@recipe["user_id"] = ""
refute @recipe.valid?, '@recipe should be invalid - missing user_id, but passed validation'
end
end

0 comments on commit 238a6fc

Please sign in to comment.