Commit 8f5bb1b4 by Tran Hoang Viet

VietTH: Implement product management

parent f9bdb5f9
...@@ -11,3 +11,4 @@ ...@@ -11,3 +11,4 @@
/log/* /log/*
!/log/.keep !/log/.keep
/tmp /tmp
/public/uploads/
\ No newline at end of file
...@@ -59,4 +59,7 @@ gem 'settingslogic', '~> 2.0.9' ...@@ -59,4 +59,7 @@ gem 'settingslogic', '~> 2.0.9'
gem 'draper', '~> 2.1.0' gem 'draper', '~> 2.1.0'
gem 'devise', '~> 3.5.1' gem 'devise', '~> 3.5.1'
\ No newline at end of file
gem 'carrierwave', '~> 0.10.0'
gem 'mini_magick', '~> 3.8.1'
\ No newline at end of file
...@@ -43,6 +43,11 @@ GEM ...@@ -43,6 +43,11 @@ GEM
builder (3.2.2) builder (3.2.2)
byebug (5.0.0) byebug (5.0.0)
columnize (= 0.9.0) columnize (= 0.9.0)
carrierwave (0.10.0)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
json (>= 1.7)
mime-types (>= 1.16)
coderay (1.1.0) coderay (1.1.0)
coffee-rails (4.1.0) coffee-rails (4.1.0)
coffee-script (>= 2.2.0) coffee-script (>= 2.2.0)
...@@ -105,6 +110,8 @@ GEM ...@@ -105,6 +110,8 @@ GEM
mime-types (>= 1.16, < 3) mime-types (>= 1.16, < 3)
method_source (0.8.2) method_source (0.8.2)
mime-types (2.6.1) mime-types (2.6.1)
mini_magick (3.8.1)
subexec (~> 0.2.1)
mini_portile (0.6.2) mini_portile (0.6.2)
minitest (5.7.0) minitest (5.7.0)
multi_json (1.11.1) multi_json (1.11.1)
...@@ -177,6 +184,7 @@ GEM ...@@ -177,6 +184,7 @@ GEM
actionpack (>= 3.0) actionpack (>= 3.0)
activesupport (>= 3.0) activesupport (>= 3.0)
sprockets (>= 2.8, < 4.0) sprockets (>= 2.8, < 4.0)
subexec (0.2.3)
thor (0.19.1) thor (0.19.1)
thread_safe (0.3.5) thread_safe (0.3.5)
tilt (1.4.1) tilt (1.4.1)
...@@ -212,6 +220,7 @@ PLATFORMS ...@@ -212,6 +220,7 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
byebug byebug
carrierwave (~> 0.10.0)
coffee-rails (~> 4.1.0) coffee-rails (~> 4.1.0)
devise (~> 3.5.1) devise (~> 3.5.1)
draper (~> 2.1.0) draper (~> 2.1.0)
...@@ -220,6 +229,7 @@ DEPENDENCIES ...@@ -220,6 +229,7 @@ DEPENDENCIES
haml-rails (~> 0.9) haml-rails (~> 0.9)
jbuilder (~> 2.0) jbuilder (~> 2.0)
jquery-rails jquery-rails
mini_magick (~> 3.8.1)
mysql2 mysql2
pry-rails (~> 0.3.4) pry-rails (~> 0.3.4)
quiet_assets quiet_assets
......
...@@ -12,4 +12,8 @@ ...@@ -12,4 +12,8 @@
// //
//= require jquery //= require jquery
//= require jquery_ujs //= require jquery_ujs
//= require jquery.validate.min
//= require twitter/bootstrap //= require twitter/bootstrap
//= require product
//= require ready
//= require additional-methods
@productLib = (->
@validateForm = ->
$('#form-product form').validate
rules:
'product[title]':
required: true
maxlength: 255
'product[price]':
number: true
'product[image]':
required: true
accept: "image/*"
return @
)()
\ No newline at end of file
$(document).ready ->
window.productLib.validateForm()
\ No newline at end of file
...@@ -2,6 +2,16 @@ ...@@ -2,6 +2,16 @@
height: 22px; height: 22px;
} }
input, textarea, select{
&.error{
border-color: #FF4F4F;
}
}
label.error{
color: #FF4F4F;
}
#wrapper{ #wrapper{
margin-top: 50px; margin-top: 50px;
......
...@@ -4,4 +4,18 @@ class ApplicationController < ActionController::Base ...@@ -4,4 +4,18 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception. # Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead. # For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception protect_from_forgery with: :exception
end
before_action :get_sidebar_categories
protected
def params_permitted data = nil
data ||= self.class::PERMIT
params.require(data.keys.first).permit(data.values.first)
end
def get_sidebar_categories
unless devise_controller?
@sidebar_categories = Category.limit(Settings.limit_category).select(Category::JSON_DEFAULT)
end
end
end
\ No newline at end of file
class HomeController < ApplicationController class HomeController < ApplicationController
def index def index
@categories = Category.limit(Settings.limit_category).select(Category::JSON_DEFAULT)
@recommended_items = Product.recommended @recommended_items = Product.recommended
@newest_items = Product.newest @newest_items = Product.newest
end end
......
class ProductsController < ApplicationController
before_action :set_product, only: :new
before_action :set_categories, only: [:new, :create]
PERMIT = { product: %i(title price category_id image) }
def new
end
def create
@product = Product.create(params_permitted)
if @product.valid?
redirect_to root_path
else
render :new
end
end
private
def set_product
@product = Product.find_by(id: params[:id]) || Product.new
end
def set_categories
@categories = Category.all.limit(Settings.limit_category)
end
end
\ No newline at end of file
...@@ -12,4 +12,6 @@ class Product < ActiveRecord::Base ...@@ -12,4 +12,6 @@ class Product < ActiveRecord::Base
validates :category, presence: true validates :category, presence: true
enum product_type: %i(system amazon) enum product_type: %i(system amazon)
mount_uploader :image, ImageUploader
end end
# encoding: utf-8
require 'carrierwave/processing/mini_magick'
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
# # 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 :large do
process resize_to_fill: [110, 86]
end
version :medium do
process resize_to_fill: [71, 38]
end
version :small do
process resize_to_fill: [51, 32]
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
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
= link_to 'Logout', destroy_user_session_path, method: :delete = link_to 'Logout', destroy_user_session_path, method: :delete
%li %li
= link_to 'Change profile', edit_user_registration_path = link_to 'Change profile', edit_user_registration_path
%li
= link_to 'New product', new_product_path
- else - else
%li %li
......
%h3.title Category %h3.title Category
%ul.nav.nav-sidebar %ul.nav.nav-sidebar
= render @categories = render @sidebar_categories
\ No newline at end of file \ No newline at end of file
#form-product
= form_for @product, html: {class: 'form-horizontal'} do |f|
.form-group
= f.label :title, 'Title*', class: 'col-sm-2 control-label'
.col-sm-4
= f.text_field :title, autofocus: true, class: 'form-control'
.form-group
= f.label :price, class: 'col-sm-2 control-label'
.col-sm-4
= f.text_field :price, class: 'form-control'
.form-group
= f.label :category, class: 'col-sm-2 control-label'
.col-sm-4
= f.collection_select :category_id, @categories.decorate, :id, :title, {}, class: 'form-control'
.form-group
= f.label :image, 'Image*', class: 'col-sm-2 control-label'
.col-sm-4
= f.file_field :image, class: 'form-control'
.form-group
.col-sm-offset-2.col-sm-10
= f.submit "Save", class: 'btn btn-default'
\ No newline at end of file
class AddImageToProducts < ActiveRecord::Migration
def change
add_column :products, :image, :string
end
end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150703024141) do ActiveRecord::Schema.define(version: 20150703074751) do
create_table "categories", force: :cascade do |t| create_table "categories", force: :cascade do |t|
t.datetime "created_at", null: false t.datetime "created_at", null: false
...@@ -32,6 +32,7 @@ ActiveRecord::Schema.define(version: 20150703024141) do ...@@ -32,6 +32,7 @@ ActiveRecord::Schema.define(version: 20150703024141) do
t.decimal "price", precision: 10 t.decimal "price", precision: 10
t.integer "category_id", limit: 4, null: false t.integer "category_id", limit: 4, null: false
t.integer "product_type", limit: 4, default: 0 t.integer "product_type", limit: 4, default: 0
t.string "image", limit: 255
end end
add_index "products", ["category_id"], name: "index_products_on_category_id", using: :btree add_index "products", ["category_id"], name: "index_products_on_category_id", using: :btree
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment