Add Annotate Gem and some more validations

This commit is contained in:
Benjamin Cousaert 2014-12-04 19:50:02 +01:00
parent 9e598c5751
commit 182932abf5
14 changed files with 162 additions and 0 deletions

View file

@ -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'

View file

@ -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)

View file

@ -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') }

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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:

View file

@ -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:

View file

@ -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:

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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