Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ gem 'simple_form'
gem 'bootstrap-sass', '~> 3.3.6'
gem 'devise'
gem 'paperclip'
gem 'omniauth-github'

# Use jquery as the JavaScript library
gem 'jquery-rails'
Expand Down
24 changes: 22 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ GEM
climate_control (0.2.0)
cocaine (0.5.8)
climate_control (>= 0.0.3, < 1.0)
cocoon (1.2.10)
coffee-rails (4.2.2)
coffee-script (>= 2.2.0)
railties (>= 4.0.0)
Expand All @@ -68,9 +67,12 @@ GEM
warden (~> 1.2.3)
erubis (2.7.0)
execjs (2.7.0)
faraday (0.12.2)
multipart-post (>= 1.2, < 3)
ffi (1.9.18)
globalid (0.4.0)
activesupport (>= 4.2.0)
hashie (3.5.6)
i18n (0.8.6)
jbuilder (2.7.0)
activesupport (>= 4.2.0)
Expand All @@ -79,6 +81,7 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
jwt (1.5.6)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -94,9 +97,26 @@ GEM
mini_portile2 (2.2.0)
minitest (5.10.3)
multi_json (1.12.1)
multi_xml (0.6.0)
multipart-post (2.0.0)
nio4r (2.1.0)
nokogiri (1.8.0)
mini_portile2 (~> 2.2.0)
oauth2 (1.4.0)
faraday (>= 0.8, < 0.13)
jwt (~> 1.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
omniauth (1.6.1)
hashie (>= 3.4.6, < 3.6.0)
rack (>= 1.6.2, < 3)
omniauth-github (1.3.0)
omniauth (~> 1.5)
omniauth-oauth2 (>= 1.4.0, < 2.0)
omniauth-oauth2 (1.4.0)
oauth2 (~> 1.0)
omniauth (~> 1.2)
orm_adapter (0.5.0)
paperclip (5.0.0)
activemodel (>= 4.2.0)
Expand Down Expand Up @@ -192,12 +212,12 @@ PLATFORMS
DEPENDENCIES
bootstrap-sass (~> 3.3.6)
byebug
cocoon
coffee-rails (~> 4.2)
devise
jbuilder (~> 2.5)
jquery-rails
listen (~> 3.0.5)
omniauth-github
paperclip
puma (~> 3.0)
rails (~> 5.0.4)
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# README

#Recipe App

This app will allow you to create or browse recipes with the features of adding ingredients, images and directions.
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/comments.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/javascripts/omniauth_callbacks_controller.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
2 changes: 1 addition & 1 deletion app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


@mixin box-shadow {
-webkit-box-shadow: rgba(0, 0, 0, 0.09) 0 2px 0;
-webkit-box-shadow: rgba(0, 0, 0, 0.09) 0 5px 0;
-moz-box-shadow: rgba(0, 0, 0, 0.09) 0 2px 0;
box-shadow: rgba(0, 0, 0, 0.09) 0 2px 0;
}
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/comments.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Comments controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

def after_sign_in_path_for(resource)
request.env['omniauth.origin'] || root_path
end

end
16 changes: 16 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CommentsController < ApplicationController
def create
@recipe = Recipe.find(params[:recipe_id])
@comment = @recipe.comments.create(comment_params)
redirect_to recipe_path(@recipe)
end


private

def comment_params
params.require(:comment).permit(:rating, :body, :recipe_id)
end


end
8 changes: 8 additions & 0 deletions app/controllers/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class OmniauthCallbacksController < Devise::OmniauthCallbacksController

def github
@user = User.from_omniauth(request.env["omniauth.auth"])
sign_in_and_redirect @user
end

end
19 changes: 6 additions & 13 deletions app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'byebug'
class RecipesController < ApplicationController
before_action :find_recipe, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
Expand All @@ -8,36 +9,33 @@ def index

def new
@recipe = Recipe.new
@ingredients = @recipe.ingredients.build
@ingredient_recipes = 3.times.collect { @recipe.recipe_ingredients.build }
@ingredients = 4.times.collect { @recipe.ingredients.build }
# @comments = @recipe.comments.build
end

def create
@recipe = Recipe.new(recipe_params)
@recipe = current_user.recipes.new(recipe_params)
@recipe = current_user.recipes.build(recipe_params)
if @recipe.save
@recipe.add_ingredients(recipe_ingredient_params)
redirect_to recipe_path(@recipe), notice: "Your recipe has successfully been added"
else
render 'new'
end
end

def edit
# 3.times.collect {@recipe.ingredients.build}
@ingredient_recipes = 3.times.collect { @recipe.recipe_ingredients.build }
end

def update
if @recipe.update(recipe_params)
@recipe.add_ingredients(recipe_ingredient_params)
redirect_to @recipe, notice: "Your recipe has successfully been updated"
else
render 'edit'
end
end

def show
@comments = @recipe.comments
end

def destroy
Expand All @@ -52,12 +50,7 @@ def find_recipe
end

def recipe_params
params.require(:recipe).permit(:name, :description, :image, :directions)
params.require(:recipe).permit(:name, :description, :image, :directions, :ingredient_ids => [], :ingredients_attributes => [:name])
end

def recipe_ingredient_params
params.require(:recipe).permit(recipe_ingredients_attributes: [:ingredient_id, ingredient: [:name]])
end


end
2 changes: 2 additions & 0 deletions app/helpers/comments_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CommentsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/omniauth_callbacks_controller_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module OmniauthCallbacksControllerHelper
end
9 changes: 9 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Comment < ApplicationRecord
belongs_to :recipe
belongs_to :user


validates_inclusion_of :rating, :in => 1..5, message: "is between 1-5"


end
3 changes: 3 additions & 0 deletions app/models/ingredient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ class Ingredient < ApplicationRecord
has_many :recipe_ingredients
has_many :recipes, through: :recipe_ingredients

validates :name, uniqueness: { case_sensitive: false },
allow_blank: true

end
32 changes: 9 additions & 23 deletions app/models/recipe.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,26 @@
# require 'byebug'
class Recipe < ApplicationRecord
belongs_to :user, optional: true
has_many :recipe_ingredients
has_many :ingredients, through: :recipe_ingredients
has_many :comments

# accepts_nested_attributes_for :recipe_ingredients


accepts_nested_attributes_for :recipe_ingredients, allow_destroy: true
accepts_nested_attributes_for :ingredients, allow_destroy: true


has_attached_file :image, styles: { medium: "300x300>"}
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/




def add_ingredients(params)
params[:recipe_ingredients_attributes].each do |k, recipe_ingredient|

if recipe_ingredient[:recipe_id].present?
@ingredient = Ingredient.find_or_create_by(params[:id])

elsif recipe_ingredient[:ingredient_id].present?
@ingredient = Ingredient.find_by(id: recipe_ingredient[:ingredient_id])
end

if recipe_ingredient[:recipe_id].present?
RecipeIngredient.create( ingredient_id: ingredient.id, recipe_id: self.id )
end
def ingredients_attributes=(ingredients_attributes)
ingredients_attributes.each do |k, attribute|
ingredient = Ingredient.find_or_create_by(attribute)
ingredients << ingredient if ingredient.persisted?
end
end

# def add_ingredients(params)
# params[:ingredients_attributes].each do |k, recipe_ingredient|
# if ingredient[:id].present?
# @recipe_ingredient = RecipeIngredient.find_or_create_by(params[:id])
# elsif ingredient[:name].present?
# end


end
1 change: 0 additions & 1 deletion app/models/recipe_ingredient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ class RecipeIngredient < ApplicationRecord
belongs_to :recipe
belongs_to :ingredient


end
14 changes: 12 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable
has_many :recipes


def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
end
end

end
12 changes: 12 additions & 0 deletions app/views/comments/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%= div_for @comments do |comment| %>
<p>
<strong>
Posted
<%= time_ago_in_words(comment.created_at) %>
ago
</strong>
<br/>
<%= comment.body %>
<%= comment.rating %>
</p>
<% end %>
11 changes: 11 additions & 0 deletions app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= simple_form_for([@recipe, @recipe.comments.build]) do |f| %>
<p><%= f.label :rating %>
<%= f.number_field :rating %>
</p>

<p><%= f.label :comment %>
<%= f.text_area :body %>
</p><br>
<p><%= f.submit %></p>

<% end %>
7 changes: 6 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<%= csrf_meta_tags %>
</head>
<body>

<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-brand">
Expand All @@ -17,8 +18,12 @@
<%= link_to "New Recipe", new_recipe_path %>
</li>
<li>
<%= link_to "Sign Out", method: :delete %>
<%= link_to "Sign Up", new_user_registration_path %></li>
<li><%= link_to "Sign In", new_user_session_path %></li>
<li>
<%= link_to "Sign Out", destroy_user_session_path, method: :delete %>
</li>
<li><%= link_to "Sign in with GitHub", user_github_omniauth_authorize_path %></li>
</ul>
</div>
</nav>
Expand Down
30 changes: 19 additions & 11 deletions app/views/recipes/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= simple_form_for recipe do |f| %>
<%= simple_form_for @recipe do |f| %>

<h4>Recipe Name:</h4>
<div><%= f.text_field :name %></div>
Expand All @@ -7,24 +7,32 @@
<div><%= f.input :directions, input_html: { class: 'form-control' }%></br>
</div>

<h4>
<h3>
Add Ingredients:
</h3>

<h4>New Ingredient:
</h4>

<div>
<%= f.simple_fields_for :recipe_ingredients do |recipe_ingredient| %>
<%= f.simple_fields_for :ingredients do |recipe_ingredient| %>
</br>

<div>
Select Ingredient:
<%= recipe_ingredient.collection_select :ingredient_id, Ingredient.all, :id, :name, {multiple: true} %>
<%= recipe_ingredient.fields_for :ingredients, recipe.ingredients.build do |ingredient| %>
Or Create New Ingredient:
<%= ingredient.text_field :name %>
<%= ingredient.label :_destroy, 'Remove' %>
<% end %>
</div>
<%= recipe_ingredient.input :name, input_html: { class: 'form-control'} %>
<% end %>
</div>
<h4>Select Existing Ingredient:
</h4>
<%= f.collection_check_boxes :ingredient_ids, Ingredient.all, :id, :name %>

</div>

<%= f.hidden_field :user_id, value: current_user.id %>
<% if params[:action] == "edit" %>
<%= f.submit " Edit Recipe", id:"submitButton" %>
<% else %>
<%= f.submit "Submit" %></br>
<% end %>

<% end %>
Loading