Skip to content
Permalink
Browse files

add cloudinary image uploader

  • Loading branch information...
polinadotio committed Nov 25, 2015
1 parent 3c4299c commit bc8a71ea5e189f7471bc505fdb7fb4b80889f7fb
BIN +6 KB .DS_Store
Binary file not shown.
@@ -10,7 +10,8 @@ An urban dictionary for Wellesley College. Users can explore, vote on, and contr
- [Faker](https://github.com/stympy/faker) for seeding data.
- [Redcarpet](https://github.com/vmg/redcarpet) for Markdown support.
- [RSpec](https://github.com/rspec/rspec-rails), [FactoryGirl](https://github.com/thoughtbot/factory_girl_rails), and [Capybara](https://github.com/jnicklas/capybara) for tests.
- [Heroku](https://devcenter.heroku.com/articles/getting-started-with-rails4) for app hosting
- [Heroku](https://devcenter.heroku.com/articles/getting-started-with-rails4) for app hosting.
- [Cloudinary](https://devcenter.heroku.com/articles/cloudinary#using-with-ruby-on-rails) for image uploading.


### Cloning the Project
@@ -41,7 +42,7 @@ rake routes
#models
rails g model Entry title:string body:text
rails g model Comment body:text entry:references
rails g model topic name:string public:boolean description:text
rails g model Topic name:string public:boolean description:text
rake db:migrate
#associations
@@ -65,10 +66,16 @@ rails g devise User
#figaro
figaro install
rake secret
//update env variables in production
figaro heroku:set -e production
#authorization
rails g pundit:install
#image uploader
rails generate uploader Avatar
rails g migration AddAvatarToUsers avatar:string
rake db:migrate
```

### Heroku command reference
@@ -84,9 +91,11 @@ $ heroku config
$ heroku logs --tail
//if something isn't working, you probably didn't do this:
$ heroku run rake db:migrate
$ heroku restart
#sendgrid
#heroku addons
heroku addons:create sendgrid:starter
heroku addons:create cloudinary:starter
```

Status: stopped at uploading images.
@@ -22,8 +22,6 @@ max-width: none;
padding: 0;
}*/



/*.container {
width: auto;
max-width: 700px;
@@ -43,13 +41,14 @@ padding: 0;
margin-bottom: 20px;
}

/*
.footer {
position: absolute;
bottom: 0;
width: 100%;
height: 60px;
background-color: rgba(46, 48, 58, 0.96);
}
}*/

/*#login-dp{
min-width: 250px;
@@ -14,6 +14,6 @@ def update
private

def user_params
params.require(:user).permit(:name)
params.require(:user).permit(:name, :avatar)
end
end
@@ -6,6 +6,7 @@ class User < ActiveRecord::Base
:validatable, :confirmable

has_many :entries
mount_uploader :avatar, AvatarUploader

def admin?
role == 'admin'
@@ -0,0 +1,56 @@
# encoding: utf-8

class AvatarUploader < CarrierWave::Uploader::Base

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

# 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 :resize_to_fill => [200, 300]

# Create different versions of uploaded files:
version :tiny do
process resize_to_fill: [20, 20]
end

version :small do
process resize_to_fill: [30, 30]
end

version :profile do
process resize_to_fill: [45, 45]
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
@@ -39,16 +39,16 @@
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control', placeholder: "Enter name", autofocus: true %>
</div>
<%# if current_user.avatar? %>
<% if current_user.avatar? %>
<div class="form-group">
<p>Current avatar</p>
<%#= image_tag( current_user.avatar.profile.url ) %>
<%= image_tag( current_user.avatar.profile.url ) %>
</div>
<%# end %>
<% end %>
<div class="form-group">
<%#= f.label :avatar %>
<%#= f.file_field :avatar %>
<%#= f.hidden_field :avatar_cache %>
<%= f.label :avatar %>
<%= f.file_field :avatar %>
<%= f.hidden_field :avatar_cache %>
</div>
<div class="form-group">
<%#= f.label :email_favorites %>
@@ -1,8 +1,14 @@
<h1><%= markdown_to_html @entry.title %></h1>

<p><%= markdown_to_html @entry.body %></p>

<small>
<%= image_tag(@entry.user.avatar.tiny.url) if @entry.user.avatar? %>
submitted <%= time_ago_in_words(@entry.created_at) %> ago by
<%= @entry.user.name %>
</small>

<% if policy(@entry).edit? %>
<%= link_to "Edit", edit_topic_entry_path(@topic,@entry), class: 'btn btn-success' %>
<%= link_to "Edit", edit_topic_entry_path(@topic,@entry), class: 'btn btn-success' %>
<% end %>

<p><%= markdown_to_html @entry.body %></p>

@@ -16,6 +16,7 @@

<div class="pull-right user-info">
<% if current_user %>
<%= image_tag(current_user.avatar.tiny.url) if current_user.avatar? %>
Hello
<%= link_to (current_user.name || current_user.email), edit_user_registration_path %>!
<%= link_to "Sign out", destroy_user_session_path, method: :delete %>
@@ -17,6 +17,13 @@
<%= entry.user.name %> <br>
<%= entry.comments.count %> Comments
</small>

<small>
<%= image_tag(entry.user.avatar.tiny.url) if entry.user.avatar? %>
submitted <%= time_ago_in_words(entry.created_at) %> ago by
<%= entry.user.name %><br>
<%= entry.comments.count %> Comments
</small>
</div>
</div>
<% end %>
@@ -0,0 +1,6 @@
Cloudinary.config do |config|
uri = URI.parse(ENV['CLOUDINARY_URL'])
config.api_key = uri.user
config.api_secret = uri.password
config.cloud_name = uri.host
end
@@ -0,0 +1,5 @@
class AddAvatarToUsers < ActiveRecord::Migration
def change
add_column :users, :avatar, :string
end
end
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20151023233050) do
ActiveRecord::Schema.define(version: 20151125181245) do

create_table "comments", force: true do |t|
t.text "body"
@@ -61,6 +61,7 @@
t.datetime "updated_at", null: false
t.string "name"
t.string "role"
t.string "avatar"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true
BIN +6 KB public/.DS_Store
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit bc8a71e

Please sign in to comment.
You can’t perform that action at this time.