Commit 1cd32a56 by vulehuan

delete and update some files when using mongodb

parent b669e37f
......@@ -16,7 +16,10 @@ module SessionsHelper
def current_user
remember_token = User.encrypt(cookies[:remember_token])
@current_user ||= User.find_by(remember_token: remember_token)
begin
@current_user ||= User.find_by(remember_token: remember_token)
rescue
end
end
def current_user?(user)
......
class MyCard < ActiveRecord::Base
class MyCard
include Mongoid::Document
field :full_name, type: String
field :email, type: String
field :phone, type: String
field :address, type: String
field :note, type: String
field :items, type: String
field :user_id, type: Integer
end
class Product < ActiveRecord::Base
class Product
include Mongoid::Document
field :name, type: String
field :description, type: String
field :headline, type: String
field :availability, type: String
field :code, type: String
field :condition, type: String
field :image_small, type: String
field :image_medium, type: String
field :review_rate, type: Float
field :review_count, type: Integer
field :price_currency, type: String
field :price, type: Float
field :product_category_id, type: Integer
field :user_id, type: Integer
field :status, type: Mongoid::Boolean
belongs_to :product_category
belongs_to :user
validates :product_category, presence: true
......@@ -8,31 +25,33 @@ class Product < ActiveRecord::Base
validates :price, :product_category_id, :user_id, numericality: true
validates :price_currency, presence: true, if: "price > 0"
searchable :auto_index => false do
text :name, :description
string :headline, :code, :condition, :price_currency
double :price
integer :product_category_id, :user_id
end
# searchable :auto_index => false do
# text :name, :description
# string :headline, :code, :condition, :price_currency
# double :price
# integer :product_category_id, :user_id
# end
# Returns recommended products (order by rate, and available)
def self.get_recommended_products(options = { limit: 8 })
limit = options[:limit]
return Product.select('id, name, image_medium, price, price_currency')
.where(availability: 'instock')
.where(status: true)
.order('review_rate DESC').order('review_count DESC')
.limit(limit)
# return Product.select('id, name, image_medium, price, price_currency')
# .where(availability: 'instock')
# .where(status: true)
# .order('review_rate DESC').order('review_count DESC')
# .limit(limit)
return Array.new
end
# Returns newest products
def self.get_newest_products(options = { limit: 8 })
limit = options[:limit]
return Product.select('id, name, image_medium, price, price_currency')
.where(availability: 'instock')
.where(status: true)
.order('created_at DESC').order('updated_at DESC').order('name')
.limit(limit)
# return Product.select('id, name, image_medium, price, price_currency')
# .where(availability: 'instock')
# .where(status: true)
# .order('created_at DESC').order('updated_at DESC').order('name')
# .limit(limit)
return Array.new
end
# Returns products in a category
......
class ProductCategory < ActiveRecord::Base
class ProductCategory
include Mongoid::Document
field :name, type: String
field :weight, type: Integer
field :status, type: Mongoid::Boolean
has_many :products
end
class User < ActiveRecord::Base
class User
include Mongoid::Document
field :name, type: String
field :email, type: String
field :password_digest, type: String
field :remember_token, type: String
field :admin, type: Mongoid::Boolean
has_many :products
before_save { self.email = email.downcase }
before_create :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
has_secure_password
# has_secure_password
validates :password, length: { minimum: 6 }
def User.new_remember_token
......
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
require "active_record/railtie"
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
......
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MYSQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql2
encoding: utf8
database: venshop_app_development
pool: 5
username: venshop
password: "venshop"
socket: /var/run/mysqld/mysqld.sock
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql2
encoding: utf8
database: venshop_app_test
pool: 5
username: venshop
password: "venshop"
socket: /var/run/mysqld/mysqld.sock
production:
adapter: mysql2
encoding: utf8
database: venshop_app_production
pool: 5
username: venshop
password: "venshop"
socket: /var/run/mysqld/mysqld.sock
......@@ -19,8 +19,6 @@ VenshopApp::Application.configure do
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
......
development:
# Configure available database sessions. (required)
sessions:
# Defines the default session. (required)
default:
# Defines the name of the default database that Mongoid can connect to.
# (required).
database: venshop_app_development
# Provides the hosts the default session can connect to. Must be an array
# of host:port pairs. (required)
hosts:
- localhost:27017
options:
# Change the default write concern. (default = { w: 1 })
# write:
# w: 1
# Change the default consistency model to primary, secondary.
# 'secondary' will send reads to secondaries, 'primary' sends everything
# to master. (default: primary)
# read: secondary_preferred
# How many times Moped should attempt to retry an operation after
# failure. (default: 30)
# max_retries: 30
# The time in seconds that Moped should wait before retrying an
# operation on failure. (default: 1)
# retry_interval: 1
# Configure Mongoid specific options. (optional)
options:
# Enable the identity map, needed for eager loading. (default: false)
# identity_map_enabled: false
# Includes the root model name in json serialization. (default: false)
# include_root_in_json: false
# Include the _type field in serializaion. (default: false)
# include_type_for_serialization: false
# Preload all models in development, needed when models use
# inheritance. (default: false)
# preload_models: false
# Protect id and type from mass assignment. (default: true)
# protect_sensitive_fields: true
# Raise an error when performing a #find and the document is not found.
# (default: true)
# raise_not_found_error: true
# Raise an error when defining a scope with the same name as an
# existing method. (default: false)
# scope_overwrite_exception: false
# Skip the database version check, used when connecting to a db without
# admin access. (default: false)
# skip_version_check: false
# Use Active Support's time zone in conversions. (default: true)
# use_activesupport_time_zone: true
# Ensure all times are UTC in the app side. (default: false)
# use_utc: false
test:
sessions:
default:
database: venshop_app_test
hosts:
- localhost:27017
options:
read: primary
# In the test environment we lower the retries and retry interval to
# low amounts for fast failures.
max_retries: 1
retry_interval: 0
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :password_digest
t.string :remember_token
t.boolean :admin
t.timestamps
end
end
end
class CreateProductCategories < ActiveRecord::Migration
def change
create_table :product_categories do |t|
t.string :name
t.integer :weight
t.boolean :status
t.timestamps
end
end
end
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :name
t.text :description
t.string :headline
t.string :availability
t.string :code
t.string :condition
t.string :image_small
t.string :image_medium
t.decimal :review_rate
t.integer :review_count
t.string :price_currency
t.decimal :price
t.integer :product_category_id
t.integer :user_id
t.boolean :status
t.timestamps
end
end
end
class CreateMyCards < ActiveRecord::Migration
def change
create_table :my_cards do |t|
t.string :full_name
t.string :email
t.string :phone
t.string :address
t.text :note
t.text :items
t.integer :user_id
t.timestamps
end
end
end
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20131024162951) do
create_table "my_cards", force: true do |t|
t.string "full_name"
t.string "email"
t.string "phone"
t.string "address"
t.text "note"
t.text "items"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "product_categories", force: true do |t|
t.string "name"
t.integer "weight"
t.boolean "status"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "products", force: true do |t|
t.string "name"
t.text "description"
t.string "headline"
t.string "availability"
t.string "code"
t.string "condition"
t.string "image_small"
t.string "image_medium"
t.decimal "review_rate", precision: 10, scale: 0
t.integer "review_count"
t.string "price_currency"
t.decimal "price", precision: 10, scale: 0
t.integer "product_category_id"
t.integer "user_id"
t.boolean "status"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.string "password_digest"
t.string "remember_token"
t.boolean "admin"
t.datetime "created_at"
t.datetime "updated_at"
end
end
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