Skip to content

Commit c17edf8

Browse files
committed
Integrate CarrierWave, add image upload for campaign
1 parent 278f9fe commit c17edf8

11 files changed

+69
-4
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ config/database.yml
2525
/app/assets/javascripts/.tern-port
2626
.sourceme
2727
.tern-port
28+
29+
public/uploads

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ gem 'friendly_id'
3535
gem 'exception_notification'
3636
gem 'httparty'
3737
gem 'timecop'
38+
gem 'carrierwave'
3839

3940
#gem 'cancancan', '~> 2.0'
4041

Gemfile.lock

+5
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ GEM
8888
rack (>= 1.0.0)
8989
rack-test (>= 0.5.4)
9090
xpath (~> 2.0)
91+
carrierwave (1.2.1)
92+
activemodel (>= 4.0.0)
93+
activesupport (>= 4.0.0)
94+
mime-types (>= 1.16)
9195
childprocess (0.8.0)
9296
ffi (~> 1.0, >= 1.0.11)
9397
coderay (1.1.2)
@@ -384,6 +388,7 @@ DEPENDENCIES
384388
capistrano-rails (~> 1.3)
385389
capistrano-rbenv (~> 2.1)
386390
capybara
391+
carrierwave
387392
coffee-rails (~> 4.2)
388393
country_select
389394
database_cleaner

app/admin/campaign.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
permit_params :goal, :start_date, :end_date, :title, :youtube_url,
44
:description, :claim, :twitter_url, :facebook_url,
5-
:order_description, :order_success, :email
5+
:order_description, :order_success, :email, :image
66

77
# friendly_id resource lookup
88
controller do
@@ -29,6 +29,7 @@ def find_resource
2929
input :claim
3030
input :goal
3131
input :email
32+
input :image
3233
input :start_date
3334
input :end_date
3435
input :youtube_url

app/models/campaign.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class Campaign < ApplicationRecord
22
extend FriendlyId
33
friendly_id :title, use: :slugged
4+
mount_uploader :image, CampaignImageUploader
45

56
has_many :goodies, dependent: :destroy
67
has_many :supporters, through: :goodies
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class CampaignImageUploader < CarrierWave::Uploader::Base
2+
3+
# Include RMagick or MiniMagick support:
4+
# include CarrierWave::RMagick
5+
# include CarrierWave::MiniMagick
6+
7+
# Choose what kind of storage to use for this uploader:
8+
storage :file
9+
# storage :fog
10+
11+
# Override the directory where uploaded files will be stored.
12+
# This is a sensible default for uploaders that are meant to be mounted:
13+
def store_dir
14+
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
15+
end
16+
17+
# Provide a default URL as a default if there hasn't been a file uploaded:
18+
# def default_url(*args)
19+
# # For Rails 3.1+ asset pipeline compatibility:
20+
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
21+
#
22+
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
23+
# end
24+
25+
# Process files as they are uploaded:
26+
# process scale: [200, 300]
27+
#
28+
# def scale(width, height)
29+
# # do something
30+
# end
31+
32+
# Create different versions of your uploaded files:
33+
# version :thumb do
34+
# process resize_to_fit: [50, 50]
35+
# end
36+
37+
# Add a white list of extensions which are allowed to be uploaded.
38+
# For images you might use something like this:
39+
# def extension_whitelist
40+
# %w(jpg jpeg gif png)
41+
# end
42+
43+
# Override the filename of the uploaded files:
44+
# Avoid using model.id or version_name here, see uploader/store.rb for details.
45+
# def filename
46+
# "something.jpg" if original_filename
47+
# end
48+
49+
end

app/views/root/index.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<div class="carousel-inner">
99
<% @campaigns.each_with_index do |campaign, index| %>
1010
<div class="carousel-item <% if index == 0 %>active<% end %>">
11-
<img class="d-block w-100" src="/Landsgemeindering.jpg" alt="First slide">
11+
<img class="d-block w-100" src="<%= campaign.image.url %>" alt="First slide">
1212
<div class="carousel_text">
1313
<h2><%= campaign.title %></h2>
1414
<p class="mt-3 mb-5"><%= campaign.claim %></p>

app/views/shared/_campaign_card.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div class="col-md-4">
22
<div class="campaign_card">
3-
<img class="d-block w-100" src="/Landsgemeindering.jpg" alt="First slide">
3+
<img class="d-block w-100" src="<%= campaign.image.url %>" alt="First slide">
44
<div class="campaign_card_content">
55
<%
66
=begin
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddImageToCampaigns < ActiveRecord::Migration[5.1]
2+
def change
3+
add_column :campaigns, :image, :string
4+
end
5+
end

db/schema.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 20171118123625) do
13+
ActiveRecord::Schema.define(version: 20171119180257) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -65,6 +65,7 @@
6565
t.string "email"
6666
t.text "order_success"
6767
t.text "order_success_html"
68+
t.string "image"
6869
t.index ["slug"], name: "index_campaigns_on_slug", unique: true
6970
end
7071

public/Landsgemeindering.jpg

-197 KB
Binary file not shown.

0 commit comments

Comments
 (0)