Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7618ce4
build the back end
miss-irene-lin Jul 13, 2014
ad4090f
build product model
miss-irene-lin Jul 13, 2014
e066bf4
add product function
miss-irene-lin Jul 13, 2014
51478f0
finish /admin/products page
miss-irene-lin Jul 13, 2014
32f7af0
install gem devise
miss-irene-lin Jul 13, 2014
c85a12b
authenticate users
miss-irene-lin Jul 13, 2014
802ed4e
admin_required
miss-irene-lin Jul 13, 2014
23aeb0e
admin_required error fixed
miss-irene-lin Jul 13, 2014
ddd61c6
logout function
miss-irene-lin Jul 13, 2014
aadb647
build the front end
miss-irene-lin Jul 13, 2014
05fff05
finish products/index
miss-irene-lin Jul 13, 2014
16f2a1d
finish products/show
miss-irene-lin Jul 13, 2014
5e23ea9
install gem Bootstrap
miss-irene-lin Jul 13, 2014
83c9265
install gem simple_form, and replace form
miss-irene-lin Jul 13, 2014
8e0feb6
add /admin/products/edit & /admin/products/show page
miss-irene-lin Jul 14, 2014
48eb786
add price column, and change quantity default
miss-irene-lin Jul 14, 2014
83bf711
navbar
miss-irene-lin Jul 14, 2014
3aa6803
show price in /products/show
miss-irene-lin Jul 14, 2014
616a8f1
navbar revised, add dropdown function
miss-irene-lin Jul 14, 2014
e8d49a8
install gem carrierwave & mini_magick
miss-irene-lin Jul 14, 2014
1b3530d
add photo model
miss-irene-lin Jul 14, 2014
39ccdfd
$ rails generate uploader image
miss-irene-lin Jul 14, 2014
ceaa991
set product and photo model relationship
miss-irene-lin Jul 14, 2014
d2554b0
photo upload at create action
miss-irene-lin Jul 14, 2014
f85df33
photo upload at create action, error fixed
miss-irene-lin Jul 14, 2014
03750ba
image_uploader.rb setting revised
miss-irene-lin Jul 14, 2014
ac94524
show photo at admin/products/index.html.erb
miss-irene-lin Jul 14, 2014
e521115
show photo at app/views/products/index.html.erb
miss-irene-lin Jul 14, 2014
261ed2d
show photo at app/views/products/show.html.erb
miss-irene-lin Jul 14, 2014
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
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,15 @@ gem 'spring', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]

gem 'devise' # login/logout
gem 'bootstrap-sass' # make beautiful front end
gem 'simple_form'

# upload image
gem 'carrierwave'

# belongs to carrierwave, it has to be installed with carrierwave.
# used to resize the image when uploading.
gem 'mini_magick'

gem 'better_errors', group: :development
33 changes: 33 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,32 @@ GEM
thread_safe (~> 0.1)
tzinfo (~> 1.1)
arel (5.0.1.20140414130214)
bcrypt (3.1.7)
better_errors (1.1.0)
coderay (>= 1.0.0)
erubis (>= 2.6.6)
bootstrap-sass (3.2.0.0)
sass (~> 3.2)
builder (3.2.2)
carrierwave (0.10.0)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
json (>= 1.7)
mime-types (>= 1.16)
coderay (1.1.0)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
coffee-script (2.2.0)
coffee-script-source
execjs
coffee-script-source (1.7.0)
devise (3.2.4)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 3.2.6, < 5)
thread_safe (~> 0.1)
warden (~> 1.2.3)
erubis (2.7.0)
execjs (2.2.0)
hike (1.2.3)
Expand All @@ -51,8 +69,11 @@ GEM
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.25.1)
mini_magick (3.7.0)
subexec (~> 0.2.1)
minitest (5.3.4)
multi_json (1.10.1)
orm_adapter (0.5.0)
polyglot (0.3.5)
rack (1.5.2)
rack-test (0.6.2)
Expand Down Expand Up @@ -84,6 +105,9 @@ GEM
sdoc (0.4.0)
json (~> 1.8)
rdoc (~> 4.0, < 5.0)
simple_form (3.0.2)
actionpack (~> 4.0)
activemodel (~> 4.0)
spring (1.1.3)
sprockets (2.11.0)
hike (~> 1.2)
Expand All @@ -95,6 +119,7 @@ GEM
activesupport (>= 3.0)
sprockets (~> 2.8)
sqlite3 (1.3.9)
subexec (0.2.3)
thor (0.19.1)
thread_safe (0.3.4)
tilt (1.4.1)
Expand All @@ -108,17 +133,25 @@ GEM
uglifier (2.5.0)
execjs (>= 0.3.0)
json (>= 1.8.0)
warden (1.2.3)
rack (>= 1.0)

PLATFORMS
ruby

DEPENDENCIES
better_errors
bootstrap-sass
carrierwave
coffee-rails (~> 4.0.0)
devise
jbuilder (~> 2.0)
jquery-rails
mini_magick
rails (= 4.1.0)
sass-rails (~> 4.0.3)
sdoc (~> 0.4.0)
simple_form
spring
sqlite3
turbolinks
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/admin/products.js.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/
4 changes: 3 additions & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
//= require bootstrap/dropdown
//= require bootstrap/alert
//= require_tree .
3 changes: 3 additions & 0 deletions app/assets/javascripts/products.js.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 app/assets/stylesheets/admin/products.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the admin::products controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
2 changes: 2 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
*
*= require_tree .
*= require_self
*= require bootstrap
*= require products
*/
12 changes: 12 additions & 0 deletions app/assets/stylesheets/products.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 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/

.product-price{
padding-top: 20px;
padding-bottom: 20px;

font-size: 30px;
font-weight: bold;
color: #ff007c;
}
56 changes: 56 additions & 0 deletions app/controllers/admin/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Admin::ProductsController < ApplicationController

before_action :authenticate_user!
before_action :admin_required

def index
@products = Product.all
end


def new
@product = Product.new
@photo = @product.photos.new
end


def show
@product = Product.find(params[:id])
end


def create
@product = Product.new(product_params)

if @product.save
redirect_to admin_products_path
else
render :new
end
end


def edit
@product = Product.find(params[:id])
end


def update
@product = Product.find(params[:id])

if @product.update(product_params)
redirect_to admin_products_path
else
render :edit
end
end


private


def product_params
params.require(:product).permit(:title, :description, :quantity, :price, :photos_attributes => [:image] )
end

end
7 changes: 7 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception

def admin_required
if !current_user.admin?
redirect_to root_path, alert: 'you are not admin!!'
end
end

end
11 changes: 11 additions & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ProductsController < ApplicationController

def index
@products = Product.order("id DESC")
end

def show
@product = Product.find(params[:id])
end

end
2 changes: 2 additions & 0 deletions app/helpers/admin/products_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Admin::ProductsHelper
end
37 changes: 37 additions & 0 deletions app/helpers/products_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module ProductsHelper

def render_product_photo(photo, size = "thumb")
if photo.present?
image_url = photo.image.send(size).url
else

case size
when :medium
volume = "300x300"
else
volume = "200x200"
end

image_url = "http://placehold.it/#{volume}&text=No Pic"
end

image_tag(image_url, :class => "thumbnail")
end

def render_product_name(product)
product.title
end

def render_product_desc(product)
simple_format(product.description)
end

def render_product_quantity(product)
product.quantity
end

def render_product_price(product)
product.price
end

end
7 changes: 7 additions & 0 deletions app/models/photo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Photo < ActiveRecord::Base

belongs_to :product

mount_uploader :image, ImageUploader

end
13 changes: 13 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Product < ActiveRecord::Base

has_many :photos
accepts_nested_attributes_for :photos

validates :title, :presence => true
validates :quantity, :presence => true

def default_photo
photos.first
end

end
11 changes: 11 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

def admin?
is_admin
end

end
61 changes: 61 additions & 0 deletions app/uploaders/image_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# encoding: utf-8

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

process :resize_to_fit => [800, 800]

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

version :medium do
process :resize_to_fill => [400,400]
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
12 changes: 12 additions & 0 deletions app/views/admin/products/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%= simple_form_for [:admin, @product] do |f| %>

<%= f.input :title %>
<%= f.input :description %>
<%= f.input :quantity %>
<%= f.input :price %>

<%= f.submit "Submit", :disable_with => 'Submiting...' %>

<% end %>
<br>
<%= link_to 'Cancel', admin_products_path %>
Loading