Skip to content

Commit

Permalink
Add picture uploading in room form
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonlg committed Mar 18, 2017
1 parent 6f9857b commit 379c290
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 46 deletions.
9 changes: 8 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
source 'https://rubygems.org'

gem 'rails', '4.2.7.1'
gem 'sqlite3'
# gem 'sqlite3'
gem 'pg'
gem 'rails_12factor'

gem 'coffee-rails'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
Expand All @@ -13,6 +16,10 @@ gem 'font-awesome-rails', '4.0.3.2'
gem 'friendly_id', '5.0.3'
gem 'kaminari'

gem 'carrierwave', '0.10.0'
gem 'rmagick', :require => 'RMagick'


group :development, :test do
gem 'byebug'
end
Expand Down
18 changes: 16 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ GEM
debug_inspector (>= 0.0.1)
builder (3.2.2)
byebug (9.0.6)
carrierwave (0.10.0)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
json (>= 1.7)
mime-types (>= 1.16)
coffee-rails (4.1.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.1.x)
Expand Down Expand Up @@ -88,6 +93,7 @@ GEM
minitest (5.9.1)
nokogiri (1.6.8.1)
mini_portile2 (~> 2.1.0)
pg (0.20.0)
rack (1.6.4)
rack-test (0.6.3)
rack (>= 1.0)
Expand All @@ -110,6 +116,11 @@ GEM
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
rails_12factor (0.0.3)
rails_serve_static_assets
rails_stdout_logging
rails_serve_static_assets (0.0.5)
rails_stdout_logging (0.0.5)
railties (4.2.7.1)
actionpack (= 4.2.7.1)
activesupport (= 4.2.7.1)
Expand All @@ -118,6 +129,7 @@ GEM
rake (11.3.0)
rdoc (4.2.2)
json (~> 1.4)
rmagick (2.16.0)
sass (3.4.22)
sass-rails (5.0.6)
railties (>= 4.0.0, < 6)
Expand All @@ -137,7 +149,6 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sqlite3 (1.3.12)
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.5)
Expand All @@ -160,16 +171,19 @@ PLATFORMS
DEPENDENCIES
bcrypt (~> 3.1.7)
byebug
carrierwave (= 0.10.0)
coffee-rails
font-awesome-rails (= 4.0.3.2)
friendly_id (= 5.0.3)
jquery-rails
kaminari
pg
rails (= 4.2.7.1)
rails_12factor
rmagick
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
spring
sqlite3
turbolinks
uglifier (>= 1.3.0)
web-console (~> 2.0)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/rooms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ def set_users_room
end

def room_params
params.require(:room).permit(:title, :location, :description)
params.require(:room).permit(:title, :location, :description, :picture)
end
end
7 changes: 4 additions & 3 deletions app/models/room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Room < ActiveRecord::Base
validates_presence_of :title
validates_presence_of :slug

mount_uploader :picture, PictureUploader
friendly_id :title, use: [:slugged, :history]

scope :most_recent, -> { order(created_at: :desc) }
Expand All @@ -17,9 +18,9 @@ def complete_name

def self.search(query)
if query.present?
where(['location LIKE :query OR
title LIKE :query OR
description LIKE :query', query: "%#{query}%"])
where(['location ILIKE :query OR
title ILIKE :query OR
description ILIKE :query', query: "%#{query}%"])
else
all
end
Expand Down
12 changes: 12 additions & 0 deletions app/presenters/room_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,16 @@ def total_reviews
def to_partial_path
'room'
end

def picture_url
@room.picture_url
end

def thumb_url
@room.picture.thumb.url
end

def has_picture?
@room.picture?
end
end
21 changes: 21 additions & 0 deletions app/uploaders/picture_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# encoding: utf-8

class PictureUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick

storage :file

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

process :resize_to_fill => [500, 500]

version :thumb do
process :resize_to_fill => [100, 100]
end

def extension_white_list
%w(jpg jpeg gif png)
end
end
5 changes: 5 additions & 0 deletions app/views/rooms/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<%= f.text_area :description %>
<%= error_tag @room, :description %>
</div>
<div>
<%= f.label :picture %><br>
<%= f.file_field :picture %>
<%= error_tag @room, :picture %>
</div>
<div class="actions">
<%= f.submit %>
</div>
Expand Down
28 changes: 14 additions & 14 deletions app/views/rooms/_room.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<article class='room'>
<h2><%= link_to highlight(room.title, @search_query), room.route %></h2>
<h2>
<%= link_to highlight(room.title, @search_query), room.route %>
</h2>

<%= render partial: 'review', locals: {room: room} %>

<span class='created'>
<%= t '.owner_html',
owner: room.user.full_name,
when: l(room.created_at, format: :short) %>
</span>
<p>
<span class="location">
<%= link_to highlight(room.location, @search_query),
"https://maps.google.com/?q=#{room.location}",
target: :blank %>
</span>
</p>
<p><%= highlight(room.description || '', @search_query) %></p>
<div class="room_description">
<%= link_to(
image_tag(room.thumb_url),
room.picture_url
) if room.has_picture? %>

<p>
<%= highlight(room.description || '', @search_query) %>
</p>
</div>

<% if belongs_to_user(room) %>
<ul>
<li><%= link_to t('.edit'), edit_room_path(room.route) %></li>
Expand Down
21 changes: 7 additions & 14 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
adapter: postgresql
encoding: unicode
username: ramon
password:
pool: 5
timeout: 5000

development:
<<: *default
database: db/development.sqlite3
database: colchonet_development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
database: colchonet_test

production:
<<: *default
database: db/production.sqlite3
database: colchonet_production
1 change: 1 addition & 0 deletions config/locales/pt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pt:
description: 'Descrição'
location: 'Localização'
title: 'Titulo'
picture: 'Foto'

activemodel:
attributes:
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20170318230629_add_picture_to_rooms.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPictureToRooms < ActiveRecord::Migration
def change
add_column :rooms, :picture, :string
end
end
29 changes: 18 additions & 11 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170318220921) do
ActiveRecord::Schema.define(version: 20170318230629) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "friendly_id_slugs", force: :cascade do |t|
t.string "slug", null: false
Expand All @@ -21,10 +24,10 @@
t.datetime "created_at"
end

add_index "friendly_id_slugs", ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true
add_index "friendly_id_slugs", ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type"
add_index "friendly_id_slugs", ["sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_id"
add_index "friendly_id_slugs", ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type"
add_index "friendly_id_slugs", ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true, using: :btree
add_index "friendly_id_slugs", ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type", using: :btree
add_index "friendly_id_slugs", ["sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_id", using: :btree
add_index "friendly_id_slugs", ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type", using: :btree

create_table "reviews", force: :cascade do |t|
t.integer "user_id"
Expand All @@ -34,9 +37,9 @@
t.datetime "updated_at", null: false
end

add_index "reviews", ["room_id"], name: "index_reviews_on_room_id"
add_index "reviews", ["user_id", "room_id"], name: "index_reviews_on_user_id_and_room_id", unique: true
add_index "reviews", ["user_id"], name: "index_reviews_on_user_id"
add_index "reviews", ["room_id"], name: "index_reviews_on_room_id", using: :btree
add_index "reviews", ["user_id", "room_id"], name: "index_reviews_on_user_id_and_room_id", unique: true, using: :btree
add_index "reviews", ["user_id"], name: "index_reviews_on_user_id", using: :btree

create_table "rooms", force: :cascade do |t|
t.string "title"
Expand All @@ -47,10 +50,11 @@
t.integer "user_id"
t.integer "reviews_count"
t.string "slug"
t.string "picture"
end

add_index "rooms", ["slug"], name: "index_rooms_on_slug", unique: true
add_index "rooms", ["user_id"], name: "index_rooms_on_user_id"
add_index "rooms", ["slug"], name: "index_rooms_on_slug", unique: true, using: :btree
add_index "rooms", ["user_id"], name: "index_rooms_on_user_id", using: :btree

create_table "users", force: :cascade do |t|
t.string "full_name"
Expand All @@ -64,6 +68,9 @@
t.string "confirmation_token"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree

add_foreign_key "reviews", "rooms"
add_foreign_key "reviews", "users"
add_foreign_key "rooms", "users"
end
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.

0 comments on commit 379c290

Please sign in to comment.