Commit 0fb2beda by Dao Minh Nhut

commit

parent cabef3e3
......@@ -13,7 +13,7 @@ gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
gem 'bootstrap-sass'
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
......@@ -44,5 +44,7 @@ group :development, :test do
gem 'devise'
gem 'vacuum'
end
......@@ -37,9 +37,15 @@ GEM
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.2)
autoprefixer-rails (5.2.1)
execjs
json
bcrypt (3.1.10)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
bootstrap-sass (3.3.5.1)
autoprefixer-rails (>= 5.0.0.1)
sass (>= 3.3.0)
builder (3.2.2)
byebug (5.0.0)
columnize (= 0.9.0)
......@@ -60,6 +66,7 @@ GEM
thread_safe (~> 0.1)
warden (~> 1.2.3)
erubis (2.7.0)
excon (0.45.4)
execjs (2.5.2)
globalid (0.3.5)
activesupport (>= 4.1.0)
......@@ -67,6 +74,8 @@ GEM
jbuilder (2.3.1)
activesupport (>= 3.0.0, < 5)
multi_json (~> 1.2)
jeff (1.5.0)
excon (>= 0.22.1)
jquery-rails (4.0.4)
rails-dom-testing (~> 1.0)
railties (>= 4.2.0)
......@@ -80,6 +89,7 @@ GEM
mini_portile (0.6.2)
minitest (5.7.0)
multi_json (1.11.2)
multi_xml (0.5.5)
mysql2 (0.3.18)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
......@@ -142,6 +152,9 @@ GEM
uglifier (2.7.1)
execjs (>= 0.3.0)
json (>= 1.8.0)
vacuum (1.3.0)
jeff (~> 1.0)
multi_xml (~> 0.5.0)
warden (1.2.3)
rack (>= 1.0)
web-console (2.2.1)
......@@ -154,6 +167,7 @@ PLATFORMS
ruby
DEPENDENCIES
bootstrap-sass
byebug
coffee-rails (~> 4.1.0)
devise
......@@ -166,6 +180,7 @@ DEPENDENCIES
spring
turbolinks
uglifier (>= 1.3.0)
vacuum
web-console (~> 2.0)
BUNDLED WITH
......
@import "bootstrap-sprockets";
@import "bootstrap";
/* mixins, variables, etc. */
$grayMediumLight: #eaeaea;
@mixin box_sizing {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
/* miscellaneous */
.debug_dump {
clear: both;
float: left;
width: 100%;
margin-top: 45px;
@include box_sizing;
}
/* universal */
html {
overflow-y: scroll;
}
body {
padding-top: 60px;
}
section {
overflow: auto;
}
textarea {
resize: vertical;
}
.center {
text-align: center;
h1 {
margin-bottom: 10px;
}
}
/* typography */
h1, h2, h3, h4, h5, h6 {
line-height: 1;
}
h1 {
font-size: 3em;
letter-spacing: -2px;
margin-bottom: 30px;
text-align: center;
}
h2 {
font-size: 1.7em;
letter-spacing: -1px;
margin-bottom: 30px;
text-align: center;
font-weight: normal;
color: $grayLight;
}
p {
font-size: 1.1em;
line-height: 1.7em;
}
/* header */
#logo {
float: left;
margin-right: 10px;
font-size: 1.7em;
color: white;
text-transform: uppercase;
letter-spacing: -1px;
padding-top: 9px;
font-weight: bold;
line-height: 1;
&:hover {
color: white;
text-decoration: none;
}
}
/* footer */
footer {
margin-top: 45px;
padding-top: 5px;
border-top: 1px solid $grayMediumLight;
color: $grayLight;
a {
color: $gray;
&:hover {
color: $grayDarker;
}
}
small {
float: left;
}
ul {
float: right;
list-style: none;
li {
float: left;
margin-left: 10px;
}
}
}
/* sidebar */
aside {
section {
padding: 10px 0;
border-top: 1px solid $grayLighter;
&:first-child {
border: 0;
padding-top: 0;
}
span {
display: block;
margin-bottom: 3px;
line-height: 1;
}
h1 {
font-size: 1.4em;
text-align: left;
letter-spacing: -1px;
margin-bottom: 3px;
margin-top: 0px;
}
}
}
.gravatar {
float: left;
margin-right: 10px;
}
/* forms */
input, textarea, select, .uneditable-input {
border: 1px solid #bbb;
width: 100%;
padding: 10px;
margin-bottom: 15px;
@include box_sizing;
}
input {
height: auto !important;
}
#error_explanation {
color: #f00;
ul {
list-style: none;
margin: 0 0 18px 0;
}
}
.field_with_errors {
@extend .control-group;
@extend .error;
}
/* users index */
.users {
list-style: none;
margin: 0;
li {
overflow: auto;
padding: 10px 0;
border-top: 1px solid $grayLighter;
&:last-child {
border-bottom: 1px solid $grayLighter;
}
}
}
/* microposts */
.microposts {
list-style: none;
margin: 10px 0 0 0;
li {
padding: 10px 0;
border-top: 1px solid #e8e8e8;
}
}
.content {
display: block;
}
.timestamp {
color: $grayLight;
}
.gravatar {
float: left;
margin-right: 10px;
}
aside {
textarea {
height: 100px;
margin-bottom: 5px;
}
}
/*sidebar*/
@media (min-width: 768px) {
.sb-fixed{
position: fixed;
}
}
\ No newline at end of file
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
protect_from_forgery
include SessionsHelper
# Force signout to prevent CSRF attacks
def handle_unverified_request
sign_out
super
end
end
require 'will_paginate/array'
class CategoriesController < ApplicationController
def new
end
end
def show
@items_of_category= Category.find(params[:id]).items
# @items_of_category = Item.all.select do |i|
# i.category_id == params[:id].to_i
# end
@items_of_category=@items_of_category.paginate(:page => params[:page], :per_page => 6)
end
end
\ No newline at end of file
class ProductController < ApplicationController
def new
end
before_filter :signed_in_user, only: [:create, :destroy]
before_filter :correct_user, only: :destroy
def show
@item = Product.find(params[:id])
end
def index
@items = Product.all.select do |i|
i.category==params[:id]
end
end
def new
@item= Product.new
render layout: "another_layout"
end
def create
@item = current_user.items.new(params[:item])
if @item.save
flash[:success] = "Your item has successfully posted!"
redirect_to current_user, layout: "another_layout"
else
render 'new', layout: "another_layout"
end
end
def destroy
@item.destroy
redirect_to current_user
end
private
def correct_user
@item = current_user.items.find_by_id(params[:id])
redirect_to root_url if @item.nil?
end
end
class Category < ActiveRecord::Base
attr_accessible :name
has_many :items
end
class Product < ActiveRecord::Base
attr_accessible :description, :image, :name, :price
belongs_to :user
has_and_belongs_to_many :carts
belongs_to :category
default_scope order: 'items.created_at DESC'
end
<div class="span9 categories">
<% @items_of_category.each do |i| %>
<div class="span5 center">
<h4><%= i.name%></h4>
<img src= <%= i.image%> atl= "Image not found">
<!-- <p><a class="btn btn-primary" href='/items/<%= i.id %>'>View details »</a></p> -->
<%= button_to "View details", "/items/#{i.id}",
:class => "btn btn-primary", :method => :get %>
</div><!--/span-->
<%end%>
<%= will_paginate @items_of_category, :inner_window => 2%>
</div><!--/span-->
\ No newline at end of file
<footer class="footer">
<div class="navbar-inner">
<div class="container">
<nav>
<ul>
<li><%= link_to "About", about_path %></li>
<li><%= link_to "Contact", contact_path %></li>
<li><a href="http://news.railstutorial.org/">News</a></li>
</ul>
</nav>
</div>
</div>
</footer>
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<%= link_to "VenShop", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", users_path %></li>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign out", signout_path, method: "delete" %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>
\ No newline at end of file
<!DOCTYPE HTML>
<div class= "span8">
<form class="form-search">
<input type="text" class="input-xxlarge search-query">
<button type="submit" class="btn btn-primary">Search</button>
</form>
</div>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<div class="span3">
<div class="sidebar-nav-fixed">
<div class="well">
<ul class="nav nav-list">
<li class="nav-header">Category</li>
<% category_list.each do |cat| %>
<li><a href='/categories/<%= cat.id%>'><%=cat.name%></a></li>
<%end%>
</ul>
</div><!--/.well -->
</div> <!--/sidebar-nav-fixed -->
</div><!--/span-->
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
<!-- <%= debug(params) if Rails.env.development? %> -->
</div>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>VenShop</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
</body>
</html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
<div class="span12 span-fixed-sidebar">
<div class="center hero-unit">
<h1 style="color:blue">Welcome to VenShop</h1>
<% if !signed_in? %>
<%= link_to "Sign up now!", signup_path, class: "btn btn-large btn-primary" %>
<% end %>
</div>
<%= render 'layouts/search' %>
<% if signed_in? %>
<div class= "span3">
<%= button_to "My cart", '/user/cart/',
:class => "btn btn-primary", :method => :get %>
</div>
<% end %>
<div style= "clear: both"></div>
<div class="row-fluid">
<%= render 'layouts/sidebar' %>
<%= yield %>
</div>
</div>
</div>
<%= render 'layouts/footer' %>
<!-- <%= debug(params) if Rails.env.development? %> -->
</body>
</html>
\ No newline at end of file
<li class = "center">
<span class="content"><%= item.name %></span>
<img src= <%= item.image%> atl= "Image not found" width="250" height="250"><br>
<p><a class="btn" href='/items/<%= item.id %>'>View details »</a></p>
<span class="timestamp">
Posted <%= time_ago_in_words(item.created_at) %> ago.
</span>
<% if current_user?(item.user) %>
<%= link_to "delete", item, method: :delete,
data: { confirm: "Are you sure?" },
title: item.name %>
<% end %>
</li>
\ No newline at end of file
<h1>Product#new</h1>
<p>Find me in app/views/product/new.html.erb</p>
<div class="container-home">
<div class="items">
<div class="clear" />
<div class="newest">
<div class="headline-newest">Add new product</div>
<div class="list-Browse-NewsRv">
<div class="span6 offset3" style="margin-left:0px">
<%= form_for(@product) do |f| %>
<%= f.label :Title %>
<%= f.text_field :title %>
<%= f.label :Price %>
<%= f.text_field :price %>
<%= f.label :Stock %>
<%= f.text_field :date %>
<%= f.label :Desc %>
<%= f.text_field :desc %>
<%= f.label :Image %>
<%= f.text_field :img %>
<%= f.label :Category %>
<%= f.collection_select(:categoryid, Category.all, :id, :name) %>
<%= f.submit "Add new", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
</div>
</div></div>
\ No newline at end of file
<div class="span8 center">
<h3>Name: </h3><%= @item.name%><br><br>
<img src= <%= @item.image%> atl= "Image not found" width="250" height="250">
<!-- <p><a class="btn" href='/items/<%= @item.id %>'>Add to cart »</a></p> -->
<br><br>
<!-- <button onclick="addToCart()" class="btn btn-primary">Add to cart!</button> -->
<%= button_to "Add to cart!", '/user/add_to_cart/'+ @item.id.to_s,
:class => "btn btn-primary", :method => :get %>
<h3>Price: </h3><%= @item.price%><br>
<h3>Rate: </h3><%= @item.rate%><br>
<h3>Description: </h3><br>
<p><%= @item.description%></p>
</div><!--/span-->
\ No newline at end of file
<div class="span3 sb-fixed">
<div class="well sidebar-nav">
<!-- Sidebar Contents -->
<% @category_list.each do |cat|%>
<%=link_to "#{cat.name}", signup_path,
class: "btn btn-large btn-primary" %>
<h1>hello</h1>
<% end %>
</div>
</div>
<% provide(:title, 'About Us') %>
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%=yield(:title)%></title>
</head>
<body>
<h1>About Us</h1>
<h1>Nothings to post here!</h1>
</body>
</html>
<% provide(:title, 'Contact') %>
<h1>Contact</h1>
<p>
<h1>Nothings to post here!</h1>
</p>
\ No newline at end of file
<% provide(:title, 'Help') %>
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%=yield(:title)%></title>
</head>
<body>
<h1>Help</h1>
<h1>So what do you want from me, huh?</h1>
<h1>Nothings to post here!</h1>
</body>
</html>
<div class="span9">
<div>
<h2>Recommended</h2>
<% recommended_items.each do |i|%>
<div class="span5 center">
<h4><%= i.name%></h4>
<div>
<img src= <%= i.image%> atl= "Image not found">
</div>
<%= button_to "View details", "/items/#{i.id}",
:class => "btn btn-primary", :method => :get %>
</div><!--/span-->
<%end%>
</div>
<div style= "clear: both">
<hr>
</div>
<div >
<h2>Newest</h2>
<%newest_items.each do |i|%>
<div class="span5 center">
<h4><%= i.name%></h4>
<div>
<img src= <%= i.image%>>
</div>
<%= button_to "View details", "/items/#{i.id}",
:class => "btn btn-primary", :method => :get %>
</div><!--/span-->
<%end%>
</div>
</div><!--/span-->
\ No newline at end of file
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
Rails.application.configure do
VenShop::Application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
......@@ -38,6 +38,6 @@ Rails.application.configure do
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
# config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
end
Rails.application.configure do
VenShop::Application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# Code is not reloaded between requests.
......
Rails.application.configure do
VenShop::Application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# The test environment is used exclusively to run your application's
......@@ -39,4 +39,8 @@ Rails.application.configure do
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
require 'bcrypt'
silence_warnings do
BCrypt::Engine::DEFAULT_COST = BCrypt::Engine::MIN_COST
end
end
# Be sure to restart your server when you modify this file.
# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'
VenShop::Application.config.assets.version = '1.0'
# Add additional assets to the asset load path
# Rails.application.config.assets.paths << Emoji.images_path
......
# Be sure to restart your server when you modify this file.
Rails.application.config.action_dispatch.cookies_serializer = :json
VenShop::Application.config.action_dispatch.cookies_serializer = :json
# Be sure to restart your server when you modify this file.
# Configure sensitive parameters which will be filtered from the log file.
Rails.application.config.filter_parameters += [:password]
VenShop::Application.config.filter_parameters += [:password]
# Be sure to restart your server when you modify this file.
Rails.application.config.session_store :cookie_store, key: '_VenShop_session'
VenShop::Application.config.session_store :cookie_store, key: '_VenShop_session'
......@@ -5,10 +5,10 @@
# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
ActiveSupport.on_load(:action_controller) do
wrap_parameters format: [:json] if respond_to?(:wrap_parameters)
wrap_parameters format: [:vacuum]
end
# To enable root element in JSON for ActiveRecord objects.
# ActiveSupport.on_load(:active_record) do
# self.include_root_in_json = true
# end
# Disable root element in JSON by default.
ActiveSupport.on_load(:active_record) do
self.include_root_in_json = false
end
Rails.application.routes.draw do
VenShop::Application.routes.draw do
get 'categories/new'
get 'product/new'
devise_for :users
root to: 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
get '/signin', to: 'sessions#new'
# match '/signout', to: 'sessions#destroy', via: :delete
# match '/categories/:id', to: 'categories#show'
# match '/items/:id', to: 'items#show'
# match '/user/add_to_cart/:id', to: 'users#add_to_cart'
# match 'user/check_out/', to: 'users#check_out'
# match '/user/cart', to: 'users#cart'
# match '/user/post_item', to: 'items#new'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
......
class Amazon
attr_reader :request, :item_pages
def initialize
@request = Vacuum.new('US')
@request.configure(
aws_access_key_id: "AKIAJ77C4CTZOP7TUVWQ",
aws_secret_access_key: "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",
associate_tag: "zigexn6400-22"
)
@item_pages = (1..10).to_a
end
private
def import_product(item)
begin
category = Category.find_or_create_by(name: item["ItemAttributes"]["ProductGroup"])
Product.find_or_create_by(name: item["ItemAttributes"]["Title"]) do |product|
product.image = item["LargeImage"]["URL"]
product.price = item["ItemAttributes"]["ListPrice"]["Amount"].to_i
product.description = item["EditorialReviews"]["EditorialReview"]["Content"]
product.category = category
end
rescue Exception => ex
puts ex
end
end
def import_products
item_pages.each do |page|
reponse = request.item_search(
query: {
"Keywords" => "*",
"SearchIndex" => "Blended",
"ResponseGroup" => "Medium",
"ItemPage" => page
}
)
items = reponse.to_h["ItemSearchResponse"]["Items"]["Item"]
items.each do |item|
import_product(item)
end
end
end
end
\ No newline at end of file
namespace :import_product do
desc 'Fill category and item table with data'
task get_data: :environment do
request = Vacuum.new('GB')
request.configure(
aws_access_key_id: 'key',
aws_secret_access_key: 'secret',
associate_tag: 'tag'
)
params = {
'SearchIndex' => 'Books',
'Keywords'=> 'Ruby on Rails',
'ResponseGroup' => "ItemAttributes,Images"
}
raw_products = request.item_search(query: params)
hashed_products = raw_products.to_h
namespace :products do
desc 'Get products'
task import: :environment do
Amazon.new.import
end
end
\ No newline at end of file
end
require 'delegate'
require 'multi_xml'
module Vacuum
# A wrapper around the Amazon Product Advertising API response.
class Response < SimpleDelegator
class << self
attr_accessor :parser
end
def parser
@parser || self.class.parser
end
attr_writer :parser
def parse
parser ? parser.parse(body) : to_h
end
def to_h
MultiXml.parse(body)
end
def body
__getobj__.body.force_encoding('UTF-8')
end
end
end
\ No newline at end of file
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