Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion 9 Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ GEM
arel (8.0.0)
autoprefixer-rails (7.1.5)
execjs
bcrypt (3.1.11)
bcrypt (3.1.11-x64-mingw32)
bindex (0.5.0)
bootstrap-sass (3.3.7)
Expand Down Expand Up @@ -82,6 +83,7 @@ GEM
execjs (2.7.0)
faraday (0.13.1)
multipart-post (>= 1.2, < 3)
ffi (1.9.18)
ffi (1.9.18-x64-mingw32)
geocoder (1.4.4)
globalid (0.4.0)
Expand All @@ -106,9 +108,12 @@ GEM
multi_json (1.12.2)
multipart-post (2.0.0)
nio4r (2.1.0)
nokogiri (1.8.1)
mini_portile2 (~> 2.3.0)
nokogiri (1.8.1-x64-mingw32)
mini_portile2 (~> 2.3.0)
orm_adapter (0.5.0)
pg (0.21.0)
pg (0.21.0-x64-mingw32)
public_suffix (3.0.0)
puma (3.10.0)
Expand Down Expand Up @@ -170,6 +175,7 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sqlite3 (1.3.13)
sqlite3 (1.3.13-x64-mingw32)
stripe (3.6.0)
faraday (~> 0.10)
Expand Down Expand Up @@ -199,6 +205,7 @@ GEM
nokogiri (~> 1.3)

PLATFORMS
ruby
x64-mingw32

DEPENDENCIES
Expand All @@ -224,4 +231,4 @@ DEPENDENCIES
web-console (>= 3.3.0)

BUNDLED WITH
1.16.0.pre.2
1.16.0.pre.3
3 changes: 3 additions & 0 deletions 3 app/assets/javascripts/products.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions 3 app/assets/stylesheets/products.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Products controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
75 changes: 75 additions & 0 deletions 75 app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]

# GET /products
# GET /products.json
def index
@products = Product.all

end

# GET /products/1
# GET /products/1.json
def show
end

# GET /products/new
def new
@product = Product.new
end

# GET /products/1/edit
def edit
end

# POST /products
# POST /products.json
def create
@product = Product.new(product_params)
@product.user_id = current_user.id
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: @product }
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: @product }
else
format.html { render :edit }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end

# DELETE /products/1
# DELETE /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:description, :price, :image, :user_id)
end
end
2 changes: 2 additions & 0 deletions 2 app/helpers/products_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ProductsHelper
end
4 changes: 4 additions & 0 deletions 4 app/models/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Product < ApplicationRecord
belongs_to :user
mount_uploader :image
end
1 change: 1 addition & 0 deletions 1 app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :products
end
49 changes: 49 additions & 0 deletions 49 app/uploaders/image_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class ImageUploader < 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(*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
14 changes: 14 additions & 0 deletions 14 app/views/products/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<%= simple_form_for(@product) do |f| %>
<%= f.error_notification %>

<div class="form-inputs">
<%= f.input :description %>
<%= f.input :price %>
<%= f.input :image, as: :file %>

</div>

<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
2 changes: 2 additions & 0 deletions 2 app/views/products/_product.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! product, :id, :description, :price, :image, :user_id, :created_at, :updated_at
json.url product_url(product, format: :json)
6 changes: 6 additions & 0 deletions 6 app/views/products/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing Product</h1>

<%= render 'form', product: @product %>

<%= link_to 'Show', @product %> |
<%= link_to 'Back', products_path %>
33 changes: 33 additions & 0 deletions 33 app/views/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<p id="notice"><%= notice %></p>

<h1>Products</h1>

<table>
<thead>
<tr>
<th>Description</th>
<th>Price</th>
<th>Image</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @products.each do |product| %>
<tr>
<td><%= product.description %></td>
<td><%= product.price %></td>
<td><%= image_tag(product.image_url) %></td>

<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Product', new_product_path %>
1 change: 1 addition & 0 deletions 1 app/views/products/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @products, partial: 'products/product', as: :product
5 changes: 5 additions & 0 deletions 5 app/views/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New Product</h1>

<%= render 'form', product: @product %>

<%= link_to 'Back', products_path %>
21 changes: 21 additions & 0 deletions 21 app/views/products/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Description:</strong>
<%= @product.description %>
</p>

<p>
<strong>Price:</strong>
<%= @product.price %>
</p>

<!-- <p>
<strong>Image:</strong>
<%= @product.image %>
</p> -->

<td><%= image_tag(@product.image_url) %></td>

<%= link_to 'Edit', edit_product_path(@product) %> |
<%= link_to 'Back', products_path %>
1 change: 1 addition & 0 deletions 1 app/views/products/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "products/product", product: @product
1 change: 1 addition & 0 deletions 1 config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :products
devise_for :users

resources :users
Expand Down
12 changes: 12 additions & 0 deletions 12 db/migrate/20171019084148_create_products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateProducts < ActiveRecord::Migration[5.1]
def change
create_table :products do |t|
t.text :description
t.string :price
t.text :image
t.references :user, foreign_key: true

t.timestamps
end
end
end
12 changes: 11 additions & 1 deletion 12 db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20171019064222) do
ActiveRecord::Schema.define(version: 20171019084148) do

create_table "products", force: :cascade do |t|
t.text "description"
t.string "price"
t.text "image"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_products_on_user_id"
end

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
Expand Down
Binary file added BIN +8.6 KB public/uploads/download.jpeg
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions 48 test/controllers/products_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'test_helper'

class ProductsControllerTest < ActionDispatch::IntegrationTest
setup do
@product = products(:one)
end

test "should get index" do
get products_url
assert_response :success
end

test "should get new" do
get new_product_url
assert_response :success
end

test "should create product" do
assert_difference('Product.count') do
post products_url, params: { product: { description: @product.description, image: @product.image, price: @product.price, user_id: @product.user_id } }
end

assert_redirected_to product_url(Product.last)
end

test "should show product" do
get product_url(@product)
assert_response :success
end

test "should get edit" do
get edit_product_url(@product)
assert_response :success
end

test "should update product" do
patch product_url(@product), params: { product: { description: @product.description, image: @product.image, price: @product.price, user_id: @product.user_id } }
assert_redirected_to product_url(@product)
end

test "should destroy product" do
assert_difference('Product.count', -1) do
delete product_url(@product)
end

assert_redirected_to products_url
end
end
13 changes: 13 additions & 0 deletions 13 test/fixtures/products.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
description: MyText
price: MyString
image: MyText
user: one

two:
description: MyText
price: MyString
image: MyText
user: two
7 changes: 7 additions & 0 deletions 7 test/models/product_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class ProductTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.