Add Annotate Gem and some more validations
This commit is contained in:
parent
9e598c5751
commit
182932abf5
14 changed files with 162 additions and 0 deletions
3
Gemfile
3
Gemfile
|
@ -26,6 +26,9 @@ gem 'sdoc', '~> 0.4.0', group: :doc
|
||||||
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
|
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
|
||||||
gem 'spring', group: :development
|
gem 'spring', group: :development
|
||||||
|
|
||||||
|
# add annotations of schema inside models
|
||||||
|
gem 'annotate'
|
||||||
|
|
||||||
# Use ActiveModel has_secure_password
|
# Use ActiveModel has_secure_password
|
||||||
# gem 'bcrypt', '~> 3.1.7'
|
# gem 'bcrypt', '~> 3.1.7'
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,9 @@ GEM
|
||||||
minitest (~> 5.1)
|
minitest (~> 5.1)
|
||||||
thread_safe (~> 0.1)
|
thread_safe (~> 0.1)
|
||||||
tzinfo (~> 1.1)
|
tzinfo (~> 1.1)
|
||||||
|
annotate (2.6.5)
|
||||||
|
activerecord (>= 2.3.0)
|
||||||
|
rake (>= 0.8.7)
|
||||||
arel (5.0.1.20140414130214)
|
arel (5.0.1.20140414130214)
|
||||||
bcrypt (3.1.7)
|
bcrypt (3.1.7)
|
||||||
bootstrap-sass (3.2.0.0)
|
bootstrap-sass (3.2.0.0)
|
||||||
|
@ -143,6 +146,7 @@ PLATFORMS
|
||||||
ruby
|
ruby
|
||||||
|
|
||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
|
annotate
|
||||||
bcrypt (= 3.1.7)
|
bcrypt (= 3.1.7)
|
||||||
bootstrap-sass (= 3.2.0.0)
|
bootstrap-sass (= 3.2.0.0)
|
||||||
bootstrap-will_paginate (= 0.0.10)
|
bootstrap-will_paginate (= 0.0.10)
|
||||||
|
|
|
@ -1,5 +1,17 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: orders
|
||||||
|
#
|
||||||
|
# id :integer not null, primary key
|
||||||
|
# user_id :integer
|
||||||
|
# cost :integer
|
||||||
|
# created_at :datetime not null
|
||||||
|
# updated_at :datetime not null
|
||||||
|
#
|
||||||
|
|
||||||
class Order < ActiveRecord::Base
|
class Order < ActiveRecord::Base
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
|
||||||
has_many :order_products
|
has_many :order_products
|
||||||
has_many :products, -> { includes :order_product }, { through: :order_products} do
|
has_many :products, -> { includes :order_product }, { through: :order_products} do
|
||||||
def << (product)
|
def << (product)
|
||||||
|
@ -11,6 +23,8 @@ class Order < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
validates :user, presence: true
|
||||||
|
|
||||||
accepts_nested_attributes_for :order_products
|
accepts_nested_attributes_for :order_products
|
||||||
|
|
||||||
default_scope -> { order('created_at DESC') }
|
default_scope -> { order('created_at DESC') }
|
||||||
|
|
|
@ -1,6 +1,20 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: order_products
|
||||||
|
#
|
||||||
|
# id :integer not null, primary key
|
||||||
|
# order_id :integer
|
||||||
|
# product_id :integer
|
||||||
|
# count :integer default(1)
|
||||||
|
#
|
||||||
|
|
||||||
class OrderProduct < ActiveRecord::Base
|
class OrderProduct < ActiveRecord::Base
|
||||||
belongs_to :order
|
belongs_to :order
|
||||||
belongs_to :product
|
belongs_to :product
|
||||||
|
|
||||||
|
validates :order, presence: true
|
||||||
|
validates :product, presence: true
|
||||||
|
validates :count, numericality: { greater_than_or_equal_to: 0 }
|
||||||
|
|
||||||
accepts_nested_attributes_for :product
|
accepts_nested_attributes_for :product
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,20 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: products
|
||||||
|
#
|
||||||
|
# id :integer not null, primary key
|
||||||
|
# name :string(255)
|
||||||
|
# purchase_price :integer
|
||||||
|
# sale_price :integer
|
||||||
|
# img_path :string(255)
|
||||||
|
# created_at :datetime
|
||||||
|
# updated_at :datetime
|
||||||
|
#
|
||||||
|
|
||||||
class Product < ActiveRecord::Base
|
class Product < ActiveRecord::Base
|
||||||
has_one :order_product
|
has_one :order_product
|
||||||
|
|
||||||
|
validates :name, presence: true
|
||||||
|
validates :purchase_price, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||||
|
validates :sale_price, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,17 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: users
|
||||||
|
#
|
||||||
|
# id :integer not null, primary key
|
||||||
|
# name :string(255)
|
||||||
|
# last_name :string(255)
|
||||||
|
# balance :integer default(0)
|
||||||
|
# nickname :string(255)
|
||||||
|
# password_digest :string(255)
|
||||||
|
# created_at :datetime
|
||||||
|
# updated_at :datetime
|
||||||
|
#
|
||||||
|
|
||||||
class User < ActiveRecord::Base
|
class User < ActiveRecord::Base
|
||||||
has_many :orders
|
has_many :orders
|
||||||
|
|
||||||
|
|
10
test/fixtures/order_products.yml
vendored
10
test/fixtures/order_products.yml
vendored
|
@ -1,3 +1,13 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: order_products
|
||||||
|
#
|
||||||
|
# id :integer not null, primary key
|
||||||
|
# order_id :integer
|
||||||
|
# product_id :integer
|
||||||
|
# count :integer default(1)
|
||||||
|
#
|
||||||
|
|
||||||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||||
|
|
||||||
# This model initially had no columns defined. If you add columns to the
|
# This model initially had no columns defined. If you add columns to the
|
||||||
|
|
11
test/fixtures/orders.yml
vendored
11
test/fixtures/orders.yml
vendored
|
@ -1,3 +1,14 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: orders
|
||||||
|
#
|
||||||
|
# id :integer not null, primary key
|
||||||
|
# user_id :integer
|
||||||
|
# cost :integer
|
||||||
|
# created_at :datetime not null
|
||||||
|
# updated_at :datetime not null
|
||||||
|
#
|
||||||
|
|
||||||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||||
|
|
||||||
one:
|
one:
|
||||||
|
|
13
test/fixtures/products.yml
vendored
13
test/fixtures/products.yml
vendored
|
@ -1,3 +1,16 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: products
|
||||||
|
#
|
||||||
|
# id :integer not null, primary key
|
||||||
|
# name :string(255)
|
||||||
|
# purchase_price :integer
|
||||||
|
# sale_price :integer
|
||||||
|
# img_path :string(255)
|
||||||
|
# created_at :datetime
|
||||||
|
# updated_at :datetime
|
||||||
|
#
|
||||||
|
|
||||||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||||
|
|
||||||
one:
|
one:
|
||||||
|
|
14
test/fixtures/users.yml
vendored
14
test/fixtures/users.yml
vendored
|
@ -1,3 +1,17 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: users
|
||||||
|
#
|
||||||
|
# id :integer not null, primary key
|
||||||
|
# name :string(255)
|
||||||
|
# last_name :string(255)
|
||||||
|
# balance :integer default(0)
|
||||||
|
# nickname :string(255)
|
||||||
|
# password_digest :string(255)
|
||||||
|
# created_at :datetime
|
||||||
|
# updated_at :datetime
|
||||||
|
#
|
||||||
|
|
||||||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||||
|
|
||||||
one:
|
one:
|
||||||
|
|
|
@ -1,3 +1,13 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: order_products
|
||||||
|
#
|
||||||
|
# id :integer not null, primary key
|
||||||
|
# order_id :integer
|
||||||
|
# product_id :integer
|
||||||
|
# count :integer default(1)
|
||||||
|
#
|
||||||
|
|
||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class OrderProductTest < ActiveSupport::TestCase
|
class OrderProductTest < ActiveSupport::TestCase
|
||||||
|
|
|
@ -1,3 +1,14 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: orders
|
||||||
|
#
|
||||||
|
# id :integer not null, primary key
|
||||||
|
# user_id :integer
|
||||||
|
# cost :integer
|
||||||
|
# created_at :datetime not null
|
||||||
|
# updated_at :datetime not null
|
||||||
|
#
|
||||||
|
|
||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class OrderTest < ActiveSupport::TestCase
|
class OrderTest < ActiveSupport::TestCase
|
||||||
|
|
|
@ -1,3 +1,16 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: products
|
||||||
|
#
|
||||||
|
# id :integer not null, primary key
|
||||||
|
# name :string(255)
|
||||||
|
# purchase_price :integer
|
||||||
|
# sale_price :integer
|
||||||
|
# img_path :string(255)
|
||||||
|
# created_at :datetime
|
||||||
|
# updated_at :datetime
|
||||||
|
#
|
||||||
|
|
||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class ProductTest < ActiveSupport::TestCase
|
class ProductTest < ActiveSupport::TestCase
|
||||||
|
|
|
@ -1,3 +1,17 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: users
|
||||||
|
#
|
||||||
|
# id :integer not null, primary key
|
||||||
|
# name :string(255)
|
||||||
|
# last_name :string(255)
|
||||||
|
# balance :integer default(0)
|
||||||
|
# nickname :string(255)
|
||||||
|
# password_digest :string(255)
|
||||||
|
# created_at :datetime
|
||||||
|
# updated_at :datetime
|
||||||
|
#
|
||||||
|
|
||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class UserTest < ActiveSupport::TestCase
|
class UserTest < ActiveSupport::TestCase
|
||||||
|
|
Loading…
Reference in a new issue