Skip to content
This repository has been archived by the owner on May 14, 2022. It is now read-only.

Commit

Permalink
Adds image uploader and tests to Floor model via carrierwave
Browse files Browse the repository at this point in the history
  • Loading branch information
tampakis committed Jun 21, 2016
1 parent 9cdb41c commit b8def87
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pkg/
Gemfile.lock
.internal_test_app/
db/data.yml
.DS_Store

# Ignore coverage
coverage/
2 changes: 1 addition & 1 deletion app/controllers/locations/floors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def set_floor

# Only allow a trusted parameter "white list" through.
def floor_params
params.require(:floor).permit(:label, :floor_plan_uri, :starting_point, :walkable_areas, :locations_library_id)
params.require(:floor).permit(:label, :floor_plan_image, :starting_point, :walkable_areas, :locations_library_id)
end
end
end
2 changes: 2 additions & 0 deletions app/models/locations/floor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ class Floor < ActiveRecord::Base
include Locations::Labeled
include Locations::WithLibrary

mount_uploader :floor_plan_image, FloorplanUploader

end
end
52 changes: 52 additions & 0 deletions app/uploaders/locations/floorplan_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# encoding: utf-8
module Locations
class FloorplanUploader < 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

# Create different versions of your uploaded files:
version :thumb do
process :resize_to_fit => [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
end
7 changes: 4 additions & 3 deletions app/views/locations/floors/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= form_for(@floor) do |f| %>
<%= form_for @floor, html: { multipart: true } do |f| %>

<div class="form-group form-text-box">
<%= f.label :label %><br>
Expand All @@ -7,9 +7,10 @@
</div>
</div>
<div class="form-group form-text-box">
<%= f.label :floor_plan_uri %><br>
<%= f.label :floor_plan_image %><br>
<%= image_tag(@floor.floor_plan_image_url) if @floor.floor_plan_image? %>
<div class="col-sm-10">
<%= f.text_field :floor_plan_uri %>
<%= f.file_field :floor_plan_image %>
</div>
</div>
<div class="form-group form-text-box">
Expand Down
2 changes: 1 addition & 1 deletion app/views/locations/floors/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<% @floors.each do |floor| %>
<tr>
<td><%= link_to floor.label, floor %></td>
<td><%= floor.floor_plan_uri %></td>
<td><%= image_tag floor.floor_plan_image_url(:thumb) if floor.floor_plan_image? %></td>
<td><%= link_to floor.library.code, floor.library %></td>
<td><%= link_to 'Edit', edit_floor_path(floor) %></td>
<td><%= link_to 'Destroy', floor, method: :delete, data: { confirm: 'Are you sure?' } %></td>
Expand Down
4 changes: 2 additions & 2 deletions app/views/locations/floors/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
</p>

<p>
<strong>Floor plan uri:</strong>
<%= @floor.floor_plan_uri %>
<strong>Floor plan:</strong>
<%= image_tag @floor.floor_plan_image_url if @floor.floor_plan_image? %>
</p>

<p>
Expand Down
20 changes: 20 additions & 0 deletions config/initializers/locations/floorplan_uploader_initializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
if Rails.env.test? or Rails.env.cucumber?
CarrierWave.configure do |config|
config.storage = :file
config.enable_processing = false
end

CarrierWave::Uploader::Base.descendants.each do |klass|
next if klass.anonymous?
klass.class_eval do
def cache_dir
"#{Rails.root}/public/uploads/tmp"
end

def store_dir
"#{Rails.root}/public/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
end

end
2 changes: 1 addition & 1 deletion db/migrate/20160610203622_create_locations_floors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class CreateLocationsFloors < ActiveRecord::Migration
def change
create_table :locations_floors do |t|
t.string :label
t.string :floor_plan_uri
t.string :floor_plan_image
t.string :starting_point
t.string :walkable_areas

Expand Down
2 changes: 1 addition & 1 deletion lib/generators/locations/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ def run_migrations
end

end
end
end
2 changes: 2 additions & 0 deletions lib/locations/engine.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Locations
class Engine < ::Rails::Engine
require 'jquery-tablesorter'
require 'carrierwave'

isolate_namespace Locations
config.generators do |g|
g.test_framework :rspec, view_specs: false, fixture: false
Expand Down
2 changes: 2 additions & 0 deletions locations.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Gem::Specification.new do |s|
s.add_dependency 'friendly_id', '~> 5.1.0'
s.add_dependency 'yaml_db', '~> 0.3.0'
s.add_dependency 'jquery-tablesorter', '~> 1.21'
s.add_dependency "rmagick"
s.add_dependency "carrierwave"

s.add_development_dependency 'sqlite3'
s.add_development_dependency 'rspec-rails', '~> 3.1'
Expand Down
7 changes: 5 additions & 2 deletions spec/factories/locations/floors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
FactoryGirl.define do
factory :floor, class: 'Locations::Floor' do
label { 'Floor ' + Faker::Number.number(10).to_s }
floor_plan_uri { Faker::Internet.url }
floor_plan_image Rack::Test::UploadedFile.new(File.open(File.join(Rails.root, '/public/uploads/floorplan.png')))
starting_point "10,10,10,10"
walkable_areas "<svg></svg>"
library
end

after :create do |b|
b.update_column(:floor_plan_image, "foo/bar/baz.png")
end
end
end
Binary file added spec/fixtures/floorplan.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions spec/fixtures/not-image.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This should not upload.
41 changes: 41 additions & 0 deletions spec/models/locations/floor_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'carrierwave/test/matchers'

module Locations
describe Floor, type: :model do
Expand All @@ -18,5 +19,45 @@ module Locations
end

end

describe FloorplanUploader do
include CarrierWave::Test::Matchers

let(:uploader) { FloorplanUploader.new(subject, :floor_plan_image) }

context 'the thumb version' do
before do
FloorplanUploader.enable_processing = true
File.open(File.join(Rails.root, '/public/uploads/floorplan.png')) { |f| uploader.store!(f) }
end

after do
FloorplanUploader.enable_processing = false
uploader.remove!
end

it "scales down a landscape image to be no greater than 50 x 50" do
expect(uploader.thumb).to be_no_wider_than(50)
expect(uploader.thumb).to be_no_taller_than(50)
end
end

context 'an unsupported format' do
before do
FloorplanUploader.enable_processing = true
end

after do
FloorplanUploader.enable_processing = false
uploader.remove!
end

it "only accepts jpg, png, and gif images" do
expect {
File.open(File.expand_path('../../../fixtures/not-image.txt', __FILE__)) { |f| uploader.store!(f) }
}.to raise_error(CarrierWave::IntegrityError)
end
end
end
end
end
Binary file added spec/test_app_templates/floorplan.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion spec/test_app_templates/lib/generators/test_app_generator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'rails/generators'

class TestAppGenerator < Rails::Generators::Base
source_root "../../spec/test_app_templates"
source_root File.expand_path("../../../../spec/fixtures", __FILE__)

# if you need to generate any additional configuration
# into the test app, this generator will be run immediately
Expand All @@ -20,4 +20,8 @@ def install_engine
generate 'locations:install', '-f'
end

def copy_fixture_data
copy_file "floorplan.png", "public/uploads/floorplan.png"
end

end

0 comments on commit b8def87

Please sign in to comment.