carrierwave is working

This commit is contained in:
YOUR NAME 2017-11-06 14:48:47 +11:00
parent 3cca1142b9
commit 42d7e9cbda
26 changed files with 364 additions and 22 deletions

View file

@ -5,6 +5,11 @@ git_source(:github) do |repo_name|
"https://github.com/#{repo_name}.git"
end
#File uploads rails gem
gem 'carrierwave', '~> 1.0'
#Cloud storage for file uploads, created an account on cloudinary
gem 'cloudinary'
#For different Roles management
gem 'rolify'
#Authorization gem cancancan for controlling rolify

View file

@ -41,6 +41,7 @@ GEM
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
arel (8.0.0)
aws_cf_signer (0.1.3)
bcrypt (3.1.11)
bindex (0.5.0)
builder (3.2.3)
@ -53,8 +54,15 @@ GEM
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
carrierwave (1.2.1)
activemodel (>= 4.0.0)
activesupport (>= 4.0.0)
mime-types (>= 1.16)
childprocess (0.8.0)
ffi (~> 1.0, >= 1.0.11)
cloudinary (1.8.1)
aws_cf_signer
rest-client
coffee-rails (4.2.2)
coffee-script (>= 2.2.0)
railties (>= 4.0.0)
@ -71,11 +79,15 @@ GEM
responders
warden (~> 1.2.3)
diff-lcs (1.3)
domain_name (0.5.20170404)
unf (>= 0.0.5, < 1.0.0)
erubi (1.7.0)
execjs (2.7.0)
ffi (1.9.18)
globalid (0.4.1)
activesupport (>= 4.2.0)
http-cookie (1.0.3)
domain_name (~> 0.5)
i18n (0.9.1)
concurrent-ruby (~> 1.0)
jbuilder (2.7.0)
@ -91,10 +103,14 @@ GEM
mail (2.7.0)
mini_mime (>= 0.1.1)
method_source (0.9.0)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_mime (0.1.4)
mini_portile2 (2.3.0)
minitest (5.10.3)
multi_json (1.12.2)
netrc (0.11.0)
nio4r (2.1.0)
nokogiri (1.8.1)
mini_portile2 (~> 2.3.0)
@ -135,6 +151,10 @@ GEM
responders (2.4.0)
actionpack (>= 4.2.0, < 5.3)
railties (>= 4.2.0, < 5.3)
rest-client (2.0.2)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
rolify (5.1.0)
rspec-core (3.7.0)
rspec-support (~> 3.7.0)
@ -191,6 +211,9 @@ GEM
thread_safe (~> 0.1)
uglifier (3.2.0)
execjs (>= 0.3.0, < 3)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.4)
warden (1.2.7)
rack (>= 1.0)
web-console (3.5.1)
@ -211,6 +234,8 @@ DEPENDENCIES
byebug
cancancan (~> 2.0)
capybara (~> 2.13)
carrierwave (~> 1.0)
cloudinary
coffee-rails (~> 4.2)
devise
jbuilder (~> 2.5)

View file

@ -1,3 +1,8 @@
// Place all the styles related to the Listings controller here.
// Place all the styles related to the listings controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
listing.photo {
width: 10px;
length: 10px;
}

View file

@ -1,6 +1,6 @@
class HomeController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_user!
def index
end
end

View file

@ -69,6 +69,6 @@ class ListingsController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def listing_params
params.fetch(:listing, {})
params.require(:listing).permit(:photo, :address, :property_type, :number_of_bedrooms, :number_of_bathrooms, :max_price_per_week, :available_date, :roles)
end
end

View file

@ -1,2 +1,4 @@
class Listing < ApplicationRecord
mount_uploader :photo, PhotosForListingsUploader
# params.require(:Listing).permit({Photo: []})
end

View file

@ -0,0 +1,49 @@
class PhotosForListingsUploader < CarrierWave::Uploader::Base
# include Cloudinary::CarrierWave
# 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(*args)
# # 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_whitelist
# %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

View file

@ -1,6 +1,4 @@
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
<h2>Rently front page</h2>
<!-- <% if can? :update, @article %>
<%= link_to "Edit", edit_article_path(@article) %>
<% end %> -->
<%= link_to 'Listings', listings_path %>

View file

@ -12,6 +12,29 @@
<!--For devise to flash notices and alerts -->
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<!-- # views/devise/menu/_login_items.html.erb -->
<% if user_signed_in? %>
<li>
<%= link_to('Logout', destroy_user_session_path, method: :delete) %>
</li>
<% else %>
<li>
<%= link_to('Login', new_user_session_path) %>
</li>
<% end %>
<!-- # views/devise/menu/_registration_items.html.erb -->
<% if user_signed_in? %>
<li>
<%= link_to('Edit registration', edit_user_registration_path) %>
</li>
<% else %>
<li>
<%= link_to('Register', new_user_registration_path) %>
</li>
<% end %>
<%= yield %>
</body>
</html>

View file

@ -10,6 +10,50 @@
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :avatar %>
</div>
<div class="field">
<%= form.label :photo %>
<%= form.file_field :photo %>
</div>
<div class="field">
<%= form.label :address %>
<%= form.text_field :address, id: :listing_Address %>
</div>
<div class="field">
<%= form.label :property_type %>
<%= form.text_field :property_type, id: :listing_property_type %>
</div>
<div class="field">
<%= form.label :number_of_bedrooms %>
<%= form.number_field :number_of_bedrooms, id: :listing_number_of_bedrooms %>
</div>
<div class="field">
<%= form.label :number_of_bathrooms %>
<%= form.number_field :number_of_bathrooms, id: :listing_number_of_bathrooms %>
</div>
<div class="field">
<%= form.label :max_price_per_week %>
<%= form.number_field :max_price_per_week, id: :listing_max_price_per_week %>
</div>
<div class="field">
<%= form.label :available_date %>
<%= form.number_field :available_date, id: :listing_available_date %>
</div>
<div class="field">
<%= form.label :roles %>
<%= form.number_field :roles, id: :listing_Roles %>
</div>
<div class="actions">
<%= form.submit %>

View file

@ -1,2 +1,2 @@
json.extract! listing, :id, :created_at, :updated_at
json.extract! listing, :id, :Photo, :Address, :property_type, :number_of_bedrooms, :number_of_bathrooms, :max_price_per_week, :available_date, :Roles, :created_at, :updated_at
json.url listing_url(listing, format: :json)

View file

@ -5,6 +5,14 @@
<table>
<thead>
<tr>
<th>Photo</th>
<th>Address</th>
<th>Property type</th>
<th>Number of bedrooms</th>
<th>Number of bathrooms</th>
<th>Max price per week</th>
<th>Available date</th>
<th>Roles</th>
<th colspan="3"></th>
</tr>
</thead>
@ -12,6 +20,14 @@
<tbody>
<% @listings.each do |listing| %>
<tr>
<td><%= image_tag listing.photo_url %></td>
<td><%= listing.address %></td>
<td><%= listing.property_type %></td>
<td><%= listing.number_of_bedrooms %></td>
<td><%= listing.number_of_bathrooms %></td>
<td><%= listing.max_price_per_week %></td>
<td><%= listing.available_date %></td>
<td><%= listing.roles %></td>
<td><%= link_to 'Show', listing %></td>
<td><%= link_to 'Edit', edit_listing_path(listing) %></td>
<td><%= link_to 'Destroy', listing, method: :delete, data: { confirm: 'Are you sure?' } %></td>

View file

@ -1,4 +1,44 @@
<p id="notice"><%= notice %></p>
<p>
<strong>Photo:</strong>
<%= @listing.photo %>
</p>
<p>
<strong>Address:</strong>
<%= @listing.address %>
</p>
<p>
<strong>Property type:</strong>
<%= @listing.property_type %>
</p>
<p>
<strong>Number of bedrooms:</strong>
<%= @listing.number_of_bedrooms %>
</p>
<p>
<strong>Number of bathrooms:</strong>
<%= @listing.number_of_bathrooms %>
</p>
<p>
<strong>Max price per week:</strong>
<%= @listing.max_price_per_week %>
</p>
<p>
<strong>Available date:</strong>
<%= @listing.available_date %>
</p>
<p>
<strong>Roles:</strong>
<%= @listing.roles %>
</p>
<%= link_to 'Edit', edit_listing_path(@listing) %> |
<%= link_to 'Back', listings_path %>

View file

@ -15,4 +15,5 @@ module Rently
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
end
require 'carrierwave/orm/activerecord'
end

19
config/cloudinary.yml Normal file
View file

@ -0,0 +1,19 @@
---
development:
cloud_name: curlyhappiness
api_key: '363646175522825'
api_secret: fGga3bKgIksDm7_vn9uYVFhj2T0
enhance_image_tag: true
static_image_support: false
production:
cloud_name: curlyhappiness
api_key: '363646175522825'
api_secret: fGga3bKgIksDm7_vn9uYVFhj2T0
enhance_image_tag: true
static_image_support: true
test:
cloud_name: curlyhappiness
api_key: '363646175522825'
api_secret: fGga3bKgIksDm7_vn9uYVFhj2T0
enhance_image_tag: true
static_image_support: false

View file

@ -4,6 +4,7 @@ Rails.application.routes.draw do
devise_for :users
get 'home/index'
root to: "home#index"
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

View file

@ -1,8 +0,0 @@
class CreateListings < ActiveRecord::Migration[5.1]
def change
create_table :listings do |t|
t.timestamps
end
end
end

View file

@ -0,0 +1,16 @@
class CreateListings < ActiveRecord::Migration[5.1]
def change
create_table :listings do |t|
t.text :photo
t.string :address
t.string :property_type
t.integer :number_of_bedrooms
t.integer :number_of_bathrooms
t.integer :max_price_per_week
t.integer :available_date
t.integer :roles
t.timestamps
end
end
end

View file

@ -10,11 +10,24 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20171103035249) do
ActiveRecord::Schema.define(version: 20171103081805) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "listings", force: :cascade do |t|
t.text "photo"
t.string "address"
t.string "property_type"
t.integer "number_of_bedrooms"
t.integer "number_of_bathrooms"
t.integer "max_price_per_week"
t.integer "available_date"
t.integer "roles"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "roles", force: :cascade do |t|
t.string "name"
t.string "resource_type"

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

View file

@ -2,13 +2,38 @@ require 'rails_helper'
RSpec.describe "listings/edit", type: :view do
before(:each) do
@listing = assign(:listing, Listing.create!())
@listing = assign(:listing, Listing.create!(
:Photo => "MyString",
:Address => "MyString",
:property_type => "MyString",
:number_of_bedrooms => 1,
:number_of_bathrooms => 1,
:max_price_per_week => 1,
:available_date => 1,
:Roles => 1
))
end
it "renders the edit listing form" do
render
assert_select "form[action=?][method=?]", listing_path(@listing), "post" do
assert_select "input[name=?]", "listing[Photo]"
assert_select "input[name=?]", "listing[Address]"
assert_select "input[name=?]", "listing[property_type]"
assert_select "input[name=?]", "listing[number_of_bedrooms]"
assert_select "input[name=?]", "listing[number_of_bathrooms]"
assert_select "input[name=?]", "listing[max_price_per_week]"
assert_select "input[name=?]", "listing[available_date]"
assert_select "input[name=?]", "listing[Roles]"
end
end
end

View file

@ -3,12 +3,38 @@ require 'rails_helper'
RSpec.describe "listings/index", type: :view do
before(:each) do
assign(:listings, [
Listing.create!(),
Listing.create!()
Listing.create!(
:Photo => "Photo",
:Address => "Address",
:property_type => "Property Type",
:number_of_bedrooms => 2,
:number_of_bathrooms => 3,
:max_price_per_week => 4,
:available_date => 5,
:Roles => 6
),
Listing.create!(
:Photo => "Photo",
:Address => "Address",
:property_type => "Property Type",
:number_of_bedrooms => 2,
:number_of_bathrooms => 3,
:max_price_per_week => 4,
:available_date => 5,
:Roles => 6
)
])
end
it "renders a list of listings" do
render
assert_select "tr>td", :text => "Photo".to_s, :count => 2
assert_select "tr>td", :text => "Address".to_s, :count => 2
assert_select "tr>td", :text => "Property Type".to_s, :count => 2
assert_select "tr>td", :text => 2.to_s, :count => 2
assert_select "tr>td", :text => 3.to_s, :count => 2
assert_select "tr>td", :text => 4.to_s, :count => 2
assert_select "tr>td", :text => 5.to_s, :count => 2
assert_select "tr>td", :text => 6.to_s, :count => 2
end
end

View file

@ -2,13 +2,38 @@ require 'rails_helper'
RSpec.describe "listings/new", type: :view do
before(:each) do
assign(:listing, Listing.new())
assign(:listing, Listing.new(
:Photo => "MyString",
:Address => "MyString",
:property_type => "MyString",
:number_of_bedrooms => 1,
:number_of_bathrooms => 1,
:max_price_per_week => 1,
:available_date => 1,
:Roles => 1
))
end
it "renders new listing form" do
render
assert_select "form[action=?][method=?]", listings_path, "post" do
assert_select "input[name=?]", "listing[Photo]"
assert_select "input[name=?]", "listing[Address]"
assert_select "input[name=?]", "listing[property_type]"
assert_select "input[name=?]", "listing[number_of_bedrooms]"
assert_select "input[name=?]", "listing[number_of_bathrooms]"
assert_select "input[name=?]", "listing[max_price_per_week]"
assert_select "input[name=?]", "listing[available_date]"
assert_select "input[name=?]", "listing[Roles]"
end
end
end

View file

@ -2,10 +2,27 @@ require 'rails_helper'
RSpec.describe "listings/show", type: :view do
before(:each) do
@listing = assign(:listing, Listing.create!())
@listing = assign(:listing, Listing.create!(
:Photo => "Photo",
:Address => "Address",
:property_type => "Property Type",
:number_of_bedrooms => 2,
:number_of_bathrooms => 3,
:max_price_per_week => 4,
:available_date => 5,
:Roles => 6
))
end
it "renders attributes in <p>" do
render
expect(rendered).to match(/Photo/)
expect(rendered).to match(/Address/)
expect(rendered).to match(/Property Type/)
expect(rendered).to match(/2/)
expect(rendered).to match(/3/)
expect(rendered).to match(/4/)
expect(rendered).to match(/5/)
expect(rendered).to match(/6/)
end
end