Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invoice - check overlap dates #261

Merged
merged 3 commits into from
Sep 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ gem 'russian_central_bank'
gem 'gon'
gem 'whenever', require: false
gem 'cocoon'
gem 'validates_overlap'

group :development, :test do
gem 'rspec-rails'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ GEM
raindrops (~> 0.7)
uuid (2.3.7)
macaddr (~> 1.0)
validates_overlap (0.5.2)
rails (>= 3.0.0)
warden (1.2.3)
rack (>= 1.0)
wasabi (3.5.0)
Expand Down Expand Up @@ -425,6 +427,7 @@ DEPENDENCIES
timecop
uglifier (>= 1.3.0)
unicorn
validates_overlap
whenever

BUNDLED WITH
Expand Down
6 changes: 4 additions & 2 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# id :integer not null, primary key
# organization_id :integer not null
# customer_id :integer not null
# starts_at :datetime
# ends_at :datetime not null
# starts_at :date
# ends_at :date not null
# currency :string default("USD"), not null
# amount_cents :integer default(0), not null
# sent_at :datetime
Expand All @@ -32,11 +32,13 @@ class Invoice < ActiveRecord::Base
validates :ends_at, presence: true
validates :amount, presence: true
validates :currency, presence: true
validates :customer_name, presence: true

validates :amount, numericality: { greater_than: 0,
less_than_or_equal_to: Dictionaries.money_max }
validates :currency, inclusion: { in: Dictionaries.currencies,
message: "%{value} is not a valid currency" }
validates :starts_at, :ends_at, overlap: { scope: 'customer_id', message_content: 'overlaps with another Invoice' }

scope :ordered, -> { order('created_at DESC') }

Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20150910054909_change_invoice_date_columns.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ChangeInvoiceDateColumns < ActiveRecord::Migration
def change
change_column :invoices, :starts_at, :date
change_column :invoices, :ends_at, :date
end
end
6 changes: 3 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150909054901) do
ActiveRecord::Schema.define(version: 20150910054909) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -94,8 +94,8 @@
create_table "invoices", force: :cascade do |t|
t.integer "organization_id", null: false
t.integer "customer_id", null: false
t.datetime "starts_at"
t.datetime "ends_at", null: false
t.date "starts_at"
t.date "ends_at", null: false
t.string "currency", default: "USD", null: false
t.integer "amount_cents", default: 0, null: false
t.datetime "sent_at"
Expand Down
1 change: 1 addition & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
factory :invoice do
organization
customer
customer_name
ends_at { Time.now }
currency 'RUB'
amount 500
Expand Down
43 changes: 41 additions & 2 deletions spec/models/invoice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# id :integer not null, primary key
# organization_id :integer not null
# customer_id :integer not null
# starts_at :datetime
# ends_at :datetime not null
# starts_at :date
# ends_at :date not null
# currency :string default("USD"), not null
# amount_cents :integer default(0), not null
# sent_at :datetime
Expand All @@ -28,8 +28,47 @@
it { is_expected.to validate_presence_of(:organization) }
it { is_expected.to validate_presence_of(:ends_at) }
it { is_expected.to validate_presence_of(:currency) }
it { is_expected.to validate_presence_of(:customer_name) }
it { is_expected.to validate_numericality_of(:amount).
is_greater_than(0).is_less_than_or_equal_to(Dictionaries.money_max) }
it { is_expected.to validate_inclusion_of(:currency).in_array(Dictionaries.currencies) }

context 'check customer date range overlaping' do
let!(:org) { create :organization }
let!(:customer) { create :customer }
let!(:invoice) { create :invoice, customer_name: customer.name,
organization: org, starts_at: Date.today - 10.days, ends_at: Date.today }
let(:invoice1) { build :invoice, customer_name: customer.name,
organization: org, starts_at: Date.today - 9.days, ends_at: Date.today + 1.days }
let(:invoice2) { build :invoice, customer_name: customer.name,
organization: org, starts_at: Date.today - 11.days, ends_at: Date.today }
let(:invoice3) { build :invoice, customer_name: customer.name,
organization: org, starts_at: Date.today - 9.days, ends_at: Date.today }
let(:invoice4) { build :invoice, customer_name: customer.name,
organization: org, starts_at: Date.today - 11.days, ends_at: Date.today + 1.days }
let(:invoice5) { build :invoice, customer_name: customer.name,
organization: org, starts_at: Date.today + 2.days, ends_at: Date.today + 10.days }

it 'Show error on starts_at' do
invoice1.valid?
expect(invoice1.errors[:starts_at]).to include('overlaps with another Invoice')
end
it 'Show error on ends_at' do
invoice2.valid?
expect(invoice2.errors[:starts_at]).to include('overlaps with another Invoice')
end
it 'Show errors on starts_at and ends_at' do
invoice3.valid?
expect(invoice3.errors[:starts_at]).to include('overlaps with another Invoice')
end
it 'Show errors on starts_at and ends_at' do
invoice4.valid?
expect(invoice4.errors[:starts_at]).to include('overlaps with another Invoice')
end
it 'Dont show errors on starts_at and ends_at' do
invoice5.valid?
expect(invoice5.errors[:starts_at]).to_not include('overlaps with another Invoice')
end
end
end
end