Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CarrierWave, bootstrap navbar #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
source 'https://rubygems.org'

gem 'devise'
gem 'bootstrap-sass'
gem 'font-awesome-rails'
gem 'carrierwave'
gem 'mini_magick'
gem 'simple_form'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.0'
Expand Down
31 changes: 31 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,32 @@ GEM
thread_safe (~> 0.1)
tzinfo (~> 1.1)
arel (5.0.1.20140414130214)
bcrypt (3.1.7)
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)
font-awesome-rails (4.1.0.0)
railties (>= 3.2, < 5.0)
hike (1.2.3)
i18n (0.6.9)
jbuilder (2.0.8)
Expand All @@ -51,8 +67,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 +103,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 +117,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 +131,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
font-awesome-rails
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/
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
//= require jquery_ujs
//= require turbolinks
//= require_tree .
//= require bootstrap
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 font-awesome
*/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/products.css.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/
51 changes: 51 additions & 0 deletions app/controllers/admin/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Admin::ProductsController < ApplicationController

before_action :authenticate_user!
before_action :admin_required

def index
@products = Product.all
end

def new
@product = Product.new
end

def create
@product = Product.new(product_params)

if @product.save
redirect_to admin_products_path
else
render :new
end
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.destroy
end

private

def product_params
params.require(:product).permit(:title, :description, :quantity, :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
9 changes: 9 additions & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ProductsController < ApplicationController
def index
@products = Product.all
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
2 changes: 2 additions & 0 deletions app/helpers/products_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ProductsHelper
end
3 changes: 3 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Product < ActiveRecord::Base
mount_uploader :image, ImageUploader
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
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_fit => [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
29 changes: 29 additions & 0 deletions app/views/admin/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<div class="group">
<%= link_to("New Product", new_admin_product_path, :class => "btn btn-primary btn-sm") %>
</div>

<table class="table">
<thead>
<tr class="active">
<td>標題</td>
<td>敘述</td>
<td>數量</td>
<td>圖片</td>
<td>修改</td>
</tr>
</thead>
<tbody>
<% @products.each do |p| %>
<tr>
<td><%= link_to(p.title, admin_product_path(p)) %></td>
<td><%= p.description %></td>
<td><%= p.quantity %></td>
<td><%= image_tag p.image_url(:thumb).to_s %></td>
<td>
<%= link_to("修改", edit_admin_product_path(p), :class => "btn btn-default") %>
<%= link_to("刪除", admin_product_path(p), method: :delete, data: { confirm: '你確定要刪除嗎?' } ) %>
</td>
</tr>
<% end %>
</tbody>
</table>
25 changes: 25 additions & 0 deletions app/views/admin/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%= form_for [:admin, @product] do |f| %>

<div class="group">
<%= f.label("標題") %>
<%= f.text_field :title %>
</div>

<div class="group">
<%= f.label("敘述") %>
<%= f.text_area :description %>
</div>

<div class="group">
<%= f.label("數量") %>
<%= f.text_field :quantity %>
</div>

<div class="group">
<%= f.label("圖片") %>
<%= f.file_field :image %>
</div>

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

<% end %>
13 changes: 13 additions & 0 deletions app/views/admin/products/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="col-md-4">
<%= image_tag @product.image_url.to_s %>
</div>
<div class="col-md-4">
<h1><%= @product.title %></h1>
<p><%= @product.description %></p>
<p>數量: <%= @product.quantity %></p>
<p>價格:</p>

</div>
<div class="col-md-4">
<button class="btn btn-danger btn-lg">加入購物車</button>
</div>
12 changes: 12 additions & 0 deletions app/views/devise/confirmations/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h2>Resend confirmation instructions</h2>

<%= form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %>
<%= devise_error_messages! %>

<div><%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %></div>

<div><%= f.submit "Resend confirmation instructions" %></div>
<% end %>

<%= render "devise/shared/links" %>
5 changes: 5 additions & 0 deletions app/views/devise/mailer/confirmation_instructions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>Welcome <%= @email %>!</p>

<p>You can confirm your account email through the link below:</p>

<p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>
8 changes: 8 additions & 0 deletions app/views/devise/mailer/reset_password_instructions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p>Hello <%= @resource.email %>!</p>

<p>Someone has requested a link to change your password. You can do this through the link below.</p>

<p><%= link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %></p>

<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
7 changes: 7 additions & 0 deletions app/views/devise/mailer/unlock_instructions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<p>Hello <%= @resource.email %>!</p>

<p>Your account has been locked due to an excessive number of unsuccessful sign in attempts.</p>

<p>Click the link below to unlock your account:</p>

<p><%= link_to 'Unlock my account', unlock_url(@resource, unlock_token: @token) %></p>
16 changes: 16 additions & 0 deletions app/views/devise/passwords/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h2>Change your password</h2>

<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :reset_password_token %>

<div><%= f.label :password, "New password" %><br />
<%= f.password_field :password, autofocus: true, autocomplete: "off" %></div>

<div><%= f.label :password_confirmation, "Confirm new password" %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %></div>

<div><%= f.submit "Change my password" %></div>
<% end %>

<%= render "devise/shared/links" %>
Loading