diff --git a/Gemfile b/Gemfile index d561ce6..6499338 100644 --- a/Gemfile +++ b/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 gem 'spring', group: :development +# add annotations of schema inside models +gem 'annotate' + # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' diff --git a/Gemfile.lock b/Gemfile.lock index df01c83..5d62c06 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -27,6 +27,9 @@ GEM minitest (~> 5.1) thread_safe (~> 0.1) tzinfo (~> 1.1) + annotate (2.6.5) + activerecord (>= 2.3.0) + rake (>= 0.8.7) arel (5.0.1.20140414130214) bcrypt (3.1.7) bootstrap-sass (3.2.0.0) @@ -143,6 +146,7 @@ PLATFORMS ruby DEPENDENCIES + annotate bcrypt (= 3.1.7) bootstrap-sass (= 3.2.0.0) bootstrap-will_paginate (= 0.0.10) diff --git a/app/models/order.rb b/app/models/order.rb index dfa5245..f75bc73 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -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 belongs_to :user + has_many :order_products has_many :products, -> { includes :order_product }, { through: :order_products} do def << (product) @@ -11,6 +23,8 @@ class Order < ActiveRecord::Base end end + validates :user, presence: true + accepts_nested_attributes_for :order_products default_scope -> { order('created_at DESC') } diff --git a/app/models/order_product.rb b/app/models/order_product.rb index 30d554f..9196d2b 100644 --- a/app/models/order_product.rb +++ b/app/models/order_product.rb @@ -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 belongs_to :order 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 end diff --git a/app/models/product.rb b/app/models/product.rb index 6bdbbbd..b23028b 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -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 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 diff --git a/app/models/user.rb b/app/models/user.rb index 34ad658..8224a79 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 has_many :orders diff --git a/test/fixtures/order_products.yml b/test/fixtures/order_products.yml index 937a0c0..30844f3 100644 --- a/test/fixtures/order_products.yml +++ b/test/fixtures/order_products.yml @@ -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 # This model initially had no columns defined. If you add columns to the diff --git a/test/fixtures/orders.yml b/test/fixtures/orders.yml index 8516b25..4753176 100644 --- a/test/fixtures/orders.yml +++ b/test/fixtures/orders.yml @@ -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 one: diff --git a/test/fixtures/products.yml b/test/fixtures/products.yml index 28ba2a5..8a2bdb4 100644 --- a/test/fixtures/products.yml +++ b/test/fixtures/products.yml @@ -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 one: diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 6ef07e8..9d9c22b 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -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 one: diff --git a/test/models/order_product_test.rb b/test/models/order_product_test.rb index 289c5a8..a6600ed 100644 --- a/test/models/order_product_test.rb +++ b/test/models/order_product_test.rb @@ -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' class OrderProductTest < ActiveSupport::TestCase diff --git a/test/models/order_test.rb b/test/models/order_test.rb index 15b8ed1..9910d04 100644 --- a/test/models/order_test.rb +++ b/test/models/order_test.rb @@ -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' class OrderTest < ActiveSupport::TestCase diff --git a/test/models/product_test.rb b/test/models/product_test.rb index 211cdd0..15ec4a3 100644 --- a/test/models/product_test.rb +++ b/test/models/product_test.rb @@ -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' class ProductTest < ActiveSupport::TestCase diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 82f61e0..9c11dac 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -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' class UserTest < ActiveSupport::TestCase