Skip to content
Open
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
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'

gem 'bootstrap-sass'

gem 'simple_form'

#carrierwave use imagemagic
gem 'rmagick'
gem 'carrierwave'

#navi bar
gem 'rails_bootstrap_navbar'
33 changes: 33 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,33 @@ GEM
thread_safe (~> 0.1)
tzinfo (~> 1.1)
arel (5.0.1.20140414130214)
bcrypt (3.1.7)
bootstrap-navbar (2.1.4)
gem_config (~> 0.3)
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)
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)
gem_config (0.3.1)
hike (1.2.3)
i18n (0.6.9)
jbuilder (2.0.8)
Expand All @@ -53,6 +70,7 @@ GEM
mime-types (1.25.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 All @@ -67,6 +85,9 @@ GEM
bundler (>= 1.3.0, < 2.0)
railties (= 4.1.0)
sprockets-rails (~> 2.0)
rails_bootstrap_navbar (2.0.1)
bootstrap-navbar (~> 2.0)
rails (>= 3.0.0)
railties (4.1.0)
actionpack (= 4.1.0)
activesupport (= 4.1.0)
Expand All @@ -75,6 +96,7 @@ GEM
rake (10.3.2)
rdoc (4.1.1)
json (~> 1.4)
rmagick (2.13.2)
sass (3.2.19)
sass-rails (4.0.3)
railties (>= 4.0.0, < 5.0)
Expand All @@ -84,6 +106,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 @@ -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
bootstrap-sass
carrierwave
coffee-rails (~> 4.0.0)
devise
jbuilder (~> 2.0)
jquery-rails
rails (= 4.1.0)
rails_bootstrap_navbar
rmagick
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/
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/
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
*
*= require_tree .
*= require_self
*= require bootstrap
*/
60 changes: 60 additions & 0 deletions app/controllers/admin/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class Admin::ProductsController < ApplicationController

before_action :authenticate_user!
before_action :admin_required

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

def create
@product = Product.new(product_params)

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

def index
@products = Product.all
@photos = Photo.all
end

def show
@product = Product.find(params[:id])
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

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

@product.destroy

flash[:success] = "Delete Successfully!."

redirect_to admin_products_path
end

private

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

end
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ 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
current_user.admin?
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
4 changes: 4 additions & 0 deletions app/models/photo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Photo < ActiveRecord::Base
belongs_to :product
mount_uploader :image, ImageUploader
end
4 changes: 4 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Product < ActiveRecord::Base
has_many :photos
accepts_nested_attributes_for :photos
end
10 changes: 10 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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
51 changes: 51 additions & 0 deletions app/uploaders/image_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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

# 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_limit => [200, 200]
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
18 changes: 18 additions & 0 deletions app/views/admin/products/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h2 class="text-center"> Edit My Own Product! </h2>

<div class="col-md-6 col-md-offset-3 well">
<%= simple_form_for [:admin, @product] do |f| %>

<%= f.input :title, label: 'Product Name' %>

<%= f.input :description, label: 'Product Description' %>

<%= f.input :price, label: 'Price (NTD)' %>

<%= f.input :quantity, label: 'Quantity' %>

<%= f.submit "Update", :disable_with => 'Submiting...', :class => "btn btn-primary" %>
<% end %>

</div>

25 changes: 25 additions & 0 deletions app/views/admin/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h2 class="text-center"> All Product List! </h2>

<div class="row">

<% @products.each do |product|%>

<div class="col-md-3">
<%= link_to image_tag(product.photos.first.image.url), product, :class => "thumbnail" %>

</div>

<div class="ccol-md-9">
<h2> Product Name : <%= link_to product.title, admin_product_path(product.id) %></h2>

<h4> Product Description : <%= product.description %></h4>

<h4> Quantity : <%= product.quantity %></h4>

<h4> Price : <%= product.price %></h4>
</div>
<% end %>

</div>

<%= link_to("Create Product", new_admin_product_path, :class => "btn btn-primary btn-sm") %>
22 changes: 22 additions & 0 deletions app/views/admin/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<h2 class="text-center"> Add My New Products to Market! </h2>
<div class="col-md-6 col-md-offset-3 well">

<%= simple_form_for [:admin, @product] do |f| %>

<%= f.input :title, label: 'Product Name' %>

<%= f.input :description, label: 'Product Description' %>

<%= f.input :price, label: 'Price (NTD)' %>

<%= f.input :quantity, label: 'Quantity' %>

<%= f.simple_fields_for :photos do |p| %>
<%= p.file_field :image %>
<% end %>

<%= f.submit "Submit", :disable_with => 'Submiting...', :class => "btn btn-primary" %>

<% end %>

</div>
26 changes: 26 additions & 0 deletions app/views/admin/products/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<h2 class="text-center"> My Product Management! </h2>

<div class="row">
<div class="col-md-3">
<% for photo in @product.photos %>
<%= image_tag photo.image, :style => "width:100%;" %>
<%end%>
</div>

<div class="col-md-6 well">
<h2> Product Name : <%= @product.title %> </h2>

<h4> Product Description : <%= @product.description %> </h4>

<h4> Quantity : <%= @product.quantity %> </h4>

<h4> Price (NTD) : <%= @product.price %> </h4>
</div>

</div>


<%= link_to("Edit Product", edit_admin_product_path(@product), :class=> 'btn btn-info btn-sm') %>
<%= link_to("Delete Product", admin_product_path(@product), :method=>:delete, :class=> 'btn btn-danger btn-sm')%>


7 changes: 7 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
</head>
<body>

<% if !current_user %>
<%= link_to("登入", new_user_session_path) %>
<% else %>
<%= link_to("登出", destroy_user_session_path, :method => :delete ) %>
<% end %>

<%= yield %>

</body>
</html>

Loading