diff --git a/Gemfile b/Gemfile index 2a644ca..7a22a4d 100644 --- a/Gemfile +++ b/Gemfile @@ -63,3 +63,5 @@ gem "nested_form" ruby "2.1.2" +gem 'carrierwave' +gem 'mini_magick' diff --git a/Gemfile.lock b/Gemfile.lock index 9c758ce..6250c31 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb index bdfb562..c3bb80c 100644 --- a/app/controllers/recipes_controller.rb +++ b/app/controllers/recipes_controller.rb @@ -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] @@ -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, diff --git a/app/models/recipe.rb b/app/models/recipe.rb index 759ac12..1ae24cc 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -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 diff --git a/app/uploaders/avatar_uploader.rb b/app/uploaders/avatar_uploader.rb new file mode 100644 index 0000000..99a9e3e --- /dev/null +++ b/app/uploaders/avatar_uploader.rb @@ -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 diff --git a/app/views/recipes/_recipe_fields.html.erb b/app/views/recipes/_recipe_fields.html.erb index 2466055..c8b3dc6 100644 --- a/app/views/recipes/_recipe_fields.html.erb +++ b/app/views/recipes/_recipe_fields.html.erb @@ -18,6 +18,8 @@ + <%= f.file_field :avatar %> +
<%= f.label :serving %>
<%= f.number_field :serving, :class => "number-input-box", :min => 1 %>
diff --git a/app/views/recipes/_temp_recipe_edit_field.html.erb b/app/views/recipes/_temp_recipe_edit_field.html.erb
index ddb3ec6..280dfd8 100644
--- a/app/views/recipes/_temp_recipe_edit_field.html.erb
+++ b/app/views/recipes/_temp_recipe_edit_field.html.erb
@@ -13,6 +13,7 @@
meal_path(current_user.meal), method: :get %>
<% end -%>
+
<%= image_tag(@recipe.avatar.url, alt: @recipe.title) %>
<%= image_tag(recipe.avatar.thumb.url, alt: recipe.title) %>
<%= link_to recipe.title, recipe %> <% if recipe.secret %> [SECRET] diff --git a/app/views/recipes/show.html.erb b/app/views/recipes/show.html.erb index bfe2ec8..311b6ad 100644 --- a/app/views/recipes/show.html.erb +++ b/app/views/recipes/show.html.erb @@ -26,6 +26,7 @@ add_to_meal_path(meal_id: current_user.meal.id, id: @recipe.id) %>
<%= image_tag(@recipe.avatar.url, alt: @recipe.title) %>
<%= image_tag(recipe.avatar.thumb.url, alt: recipe.title) %>
<%= recipe.title %>