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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@

# Ignore Byebug command history file.
.byebug_history

# Ignore key
/config/application.yml
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
source 'https://rubygems.org'

gem "devise"
ruby '2.3.1'


gem 'settingslogic' #管理我們的秘密key

gem 'omniauth-google-oauth2'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.0'
# Use sqlite3 as the database for Active Record
Expand Down
42 changes: 42 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ GEM
tzinfo (~> 1.1)
arel (7.1.0)
ast (2.3.0)
bcrypt (3.1.11)
builder (3.2.2)
byebug (9.0.5)
coffee-rails (4.2.1)
Expand All @@ -51,11 +52,20 @@ GEM
coffee-script-source (1.10.0)
concurrent-ruby (1.0.2)
debug_inspector (0.0.2)
devise (4.2.0)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0, < 5.1)
responders
warden (~> 1.2.3)
erubis (2.7.0)
execjs (2.7.0)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
ffi (1.9.14)
globalid (0.3.6)
activesupport (>= 4.1.0)
hashie (3.4.4)
i18n (0.7.0)
jbuilder (2.6.0)
activesupport (>= 3.0.0, < 5.1)
Expand All @@ -64,6 +74,7 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
jwt (1.5.4)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -78,10 +89,30 @@ GEM
mini_portile2 (2.1.0)
minitest (5.9.0)
multi_json (1.12.1)
multi_xml (0.5.5)
multipart-post (2.0.0)
nio4r (1.2.1)
nokogiri (1.6.8)
mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7)
oauth2 (1.2.0)
faraday (>= 0.8, < 0.10)
jwt (~> 1.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
omniauth (1.3.1)
hashie (>= 1.2, < 4)
rack (>= 1.0, < 3)
omniauth-google-oauth2 (0.4.1)
jwt (~> 1.5.2)
multi_json (~> 1.3)
omniauth (>= 1.1.1)
omniauth-oauth2 (>= 1.3.1)
omniauth-oauth2 (1.4.0)
oauth2 (~> 1.0)
omniauth (~> 1.2)
orm_adapter (0.5.0)
parser (2.3.1.2)
ast (~> 2.2)
pkg-config (1.1.7)
Expand Down Expand Up @@ -118,6 +149,8 @@ GEM
rb-fsevent (0.9.7)
rb-inotify (0.9.7)
ffi (>= 0.5.0)
responders (2.2.0)
railties (>= 4.2.0, < 5.1)
rubocop (0.42.0)
parser (>= 2.3.1.1, < 3.0)
powerpack (~> 0.1)
Expand All @@ -132,6 +165,7 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
settingslogic (2.0.9)
spring (1.7.2)
spring-watcher-listen (2.0.0)
listen (>= 2.7, < 4.0)
Expand All @@ -155,6 +189,8 @@ GEM
uglifier (3.0.0)
execjs (>= 0.3.0, < 3)
unicode-display_width (1.1.0)
warden (1.2.6)
rack (>= 1.0)
web-console (3.3.1)
actionview (>= 5.0)
activemodel (>= 5.0)
Expand All @@ -170,13 +206,16 @@ PLATFORMS
DEPENDENCIES
byebug
coffee-rails (~> 4.2)
devise
jbuilder (~> 2.5)
jquery-rails
listen (~> 3.0.5)
omniauth-google-oauth2
puma (~> 3.0)
rails (~> 5.0.0)
rubocop
sass-rails (~> 5.0)
settingslogic
spring
spring-watcher-listen (~> 2.0.0)
sqlite3
Expand All @@ -185,5 +224,8 @@ DEPENDENCIES
uglifier (>= 1.3.0)
web-console

RUBY VERSION
ruby 2.3.1p112

BUNDLED WITH
1.12.5
3 changes: 3 additions & 0 deletions app/assets/javascripts/account/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 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 app/assets/javascripts/testpage.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/javascripts/users/omniauth_callbacks.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/account/products.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the account::products controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions 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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/testpage.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the testpage controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/users/omniauth_callbacks.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Users::OmniauthCallbacks controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
40 changes: 40 additions & 0 deletions app/controllers/account/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Account::ProductsController < ApplicationController
def index
@products = Product.all
end

def new
@product = Product.new
end

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

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

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

def create
@product = Product.new(product_params)

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

private

def product_params
params.require(:product).permit(:title, :description, :quantity, :price)
end

end
11 changes: 11 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

before_action :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) do |u|
u.permit(:name, :email, :password, :password_confirmation)
end
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
14 changes: 14 additions & 0 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Users::OmniauthCallbacksController < ApplicationController
def google_oauth2

@user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user)

if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.google_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
5 changes: 5 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class UsersController < ApplicationController
def index
@users = User.all
end
end
2 changes: 2 additions & 0 deletions app/helpers/account/products_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Account::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
2 changes: 2 additions & 0 deletions app/helpers/testpage_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module TestpageHelper
end
2 changes: 2 additions & 0 deletions app/helpers/users/omniauth_callbacks_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Users::OmniauthCallbacksHelper
end
5 changes: 5 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Product < ApplicationRecord


belongs_to :owner, class_name: "User", foreign_key: :user_id
end
4 changes: 4 additions & 0 deletions app/models/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Settings < Settingslogic
source "#{Rails.root}/config/application.yml"
namespace Rails.env
end
32 changes: 32 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable


has_many :products

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:google_oauth2]

def self.find_for_google_oauth2(access_token, signed_in_resource=nil)
data = access_token.info
user = User.where(:provider => access_token.provider, :uid => access_token.uid ).first
if user
return user
else
registered_user = User.where(:email => access_token.info.email).first
if registered_user
return registered_user
else
user = User.create(name: data["name"],
provider:access_token.provider,
email: data["email"],
uid: access_token.uid ,
password: Devise.friendly_token[0,20]
)
end
end
end

end
25 changes: 25 additions & 0 deletions app/views/account/products/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%= form_for [:account, @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.text_field :price %>
</div>

<%= f.submit "Submit", data: { disable_with: "Submitting..." } %>

<% end %>
7 changes: 7 additions & 0 deletions app/views/account/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ul>
<%= link_to("Add", new_account_product_path, class: "btn btn-primary btn-sm") %>
<% @products.each do |product| %>
<li> <%= link_to(product.title, account_product_path(product)) %> </li>
<% end %>

</ul>
26 changes: 26 additions & 0 deletions app/views/account/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<h2> 新增產品 </h2>
<%= form_for [:account, @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.text_field :price %>
</div>

<%= f.submit "Submit", data: { disable_with: "Submitting..." } %>

<% end %>
16 changes: 16 additions & 0 deletions app/views/devise/confirmations/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<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 class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email) %>
</div>

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

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