Skip to content

Commit

Permalink
Add Recipe Image
Browse files Browse the repository at this point in the history
  • Loading branch information
bessieyy committed May 25, 2014
1 parent 04682bb commit 843429a
Show file tree
Hide file tree
Showing 22 changed files with 105 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ gem "nested_form"

ruby "2.1.2"

gem 'carrierwave'
gem 'mini_magick'
11 changes: 11 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ GEM
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
carrierwave (0.10.0)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
json (>= 1.7)
mime-types (>= 1.16)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
Expand Down Expand Up @@ -70,6 +75,8 @@ GEM
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.25.1)
mini_magick (3.7.0)
subexec (~> 0.2.1)
mini_portile (0.5.3)
minitest (5.3.3)
multi_json (1.9.3)
Expand Down Expand Up @@ -141,6 +148,8 @@ GEM
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (~> 2.8)
sqlite3 (1.3.9)
subexec (0.2.3)
thor (0.19.1)
thread_safe (0.3.3)
tilt (1.4.1)
Expand All @@ -163,11 +172,13 @@ PLATFORMS
DEPENDENCIES
bcrypt (~> 3.1.7)
capybara
carrierwave
coffee-rails (~> 4.0.0)
dotenv-rails
factory_girl_rails
jbuilder (~> 2.0)
jquery-rails
mini_magick
nested_form
pg
rails (= 4.1.0)
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def add_to_meal
end
@tmp_recipe.step_ids = @steps_arry
@tmp_recipe.save
@tmp_recipe.avatar = @recipe.avatar
@tmp_recipe.save
for @step in @tmp_recipe.steps
for @sm in @step.step_mappers
@sm.prereq_id = @steps_hash[@sm.prereq_step_number]
Expand Down Expand Up @@ -106,7 +108,7 @@ def index

def recipe_params
params.require(:recipe).permit(:title, :secret, :tags, :meal_id, :serving,
:ingredients,
:ingredients, :avatar,
steps_attributes: [:id, :step_number,
:description, :time,
:attentiveness,
Expand Down
2 changes: 2 additions & 0 deletions app/models/recipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class Recipe < ActiveRecord::Base
accepts_nested_attributes_for :steps,
:allow_destroy => true

mount_uploader :avatar, AvatarUploader

validates :title, presence: true
validates :tags, presence: true
validates :ingredients, presence: true
Expand Down
53 changes: 53 additions & 0 deletions app/uploaders/avatar_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# encoding: utf-8

class AvatarUploader < CarrierWave::Uploader::Base

# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick

# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog

# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end

# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end

process :resize_to_fill => [300, 300]

# Create different versions of your uploaded files:
version :thumb do
process :resize_to_fill => [50, 50]
end

# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end

# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end

end
2 changes: 2 additions & 0 deletions app/views/recipes/_recipe_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
</a>
</span>

<%= f.file_field :avatar %>

<p>
<strong><%= f.label :serving %></strong><br />
<%= f.number_field :serving, :class => "number-input-box", :min => 1 %>
Expand Down
1 change: 1 addition & 0 deletions app/views/recipes/_temp_recipe_edit_field.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
meal_path(current_user.meal), method: :get %>
</div>
<% end -%>
<p><%= image_tag(@recipe.avatar.url, alt: @recipe.title) %> </p>
<h1><%= @recipe.title %></h1>
</div>
<% if current_user == @recipe.user %>
Expand Down
1 change: 1 addition & 0 deletions app/views/recipes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<% @recipes.each do |recipe| %>
<div class="recipe">
<p><%= image_tag(recipe.avatar.thumb.url, alt: recipe.title) %></p>
<p class="recipe-title"><%= link_to recipe.title, recipe %>
<% if recipe.secret %>
[SECRET]
Expand Down
1 change: 1 addition & 0 deletions app/views/recipes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
add_to_meal_path(meal_id: current_user.meal.id, id: @recipe.id) %>
</div>
<% end %>
<p><%= image_tag(@recipe.avatar.url, alt: @recipe.title) %> </p>
<h1><%= @recipe.title %></h1>
</div>

Expand Down
2 changes: 2 additions & 0 deletions app/views/shared/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<span class="meal-recipe-link-side-bar">
<%= link_to "Customize", recipe %>
</span>
<p><%= image_tag(recipe.avatar.thumb.url, alt: recipe.title) %></p>
<strong><%= recipe.title %></strong><br />
<%= recipe.tags %>
</li>
Expand All @@ -79,6 +80,7 @@
<ul>
<% recent_recipes.each do |recipe| %>
<li class="side-bar-recipe">
<p><%= image_tag(recipe.avatar.thumb.url, alt: recipe.title) %></p>
<p class="recipe-title"><%= link_to recipe.title, recipe %>
<% if recipe.secret %>
[SECRET]
Expand Down
1 change: 1 addition & 0 deletions app/views/users/myrecipes.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<% end %>
<% @my_recipes.each do |recipe| %>
<div class="recipe">
<p><%= image_tag(recipe.avatar.thumb.url, alt: recipe.title) %></p>
<p class="recipe-title"><%= link_to recipe.title, recipe %>
<% if recipe.secret %>
[SECRET]
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20140524211755_add_avatar_columns_to_recipes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddAvatarColumnsToRecipes < ActiveRecord::Migration
def self.up
add_attachment :recipes, :avatar
end

def self.down
remove_attachment :recipes, :avatar
end
end
5 changes: 5 additions & 0 deletions db/migrate/20140524215145_add_avatar_to_recipes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAvatarToRecipes < ActiveRecord::Migration
def change
add_column :recipes, :avatar, :string
end
end
16 changes: 12 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140513235634) do
ActiveRecord::Schema.define(version: 20140524215145) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -32,16 +32,24 @@
end

create_table "recipes", force: true do |t|
t.boolean "temp", default: false
t.boolean "temp", default: false
t.string "title"
t.integer "serving", default: 1
t.boolean "secret", default: false
t.integer "serving", default: 1
t.boolean "secret", default: false
t.string "tags"
t.text "ingredients"
t.string "image_name"
t.string "imaage_content_type"
t.string "image_size"
t.integer "user_id"
t.integer "meal_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.string "avatar"
end

add_index "recipes", ["meal_id"], name: "index_recipes_on_meal_id", using: :btree
Expand Down
Binary file added public/uploads/recipe/avatar/5/111.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/recipe/avatar/5/thumb_111.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/recipe/avatar/9/111.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/recipe/avatar/9/thumb_111.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/tmp/1400969295-2862-3619/loli.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/tmp/1400974003-26827-3457/miss.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/tmp/1400974304-78767-6678/miss.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

1 comment on commit 843429a

@src712
Copy link
Collaborator

@src712 src712 commented on 843429a May 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix #89

Please sign in to comment.