Skip to content
6 changes: 4 additions & 2 deletions app/controllers/receivables_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ def user_receivable_params
end

def pagy_calendar_period(collection)
to_time = collection.minmax.map(&:date)
to_time.map { |time| time.to_time }
return [Date.today.to_time, Date.today.to_time] if collection.empty?

to_time = collection.minmax.map!(&:date)
to_time.map { |time| time.to_time }.sort!
end

def pagy_calendar_filter(collection, from, to)
Expand Down
3 changes: 3 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'devise'
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
Expand Down Expand Up @@ -33,6 +34,8 @@
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::IntegrationHelpers, type: :request

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
Expand Down
35 changes: 30 additions & 5 deletions spec/requests/receivables_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,44 @@

# Receivable. As you add validations to Receivable, be sure to
# adjust the attributes here as well.
let(:user){ FactoryBot.create(:user, email: 'diego@example.com') }
let(:valid_attributes) {
skip("Add a hash of attributes valid for your model")
FactoryBot.attributes_for(:receivable).merge!(user_id: user.id)
}


let(:invalid_attributes) {
skip("Add a hash of attributes invalid for your model")
}

describe "GET /index" do
it "renders a successful response" do
Receivable.create! valid_attributes
get receivables_url
expect(response).to be_successful
before { sign_in user }

context "when Receivable is present" do
it "renders a successful response" do
Receivable.create! valid_attributes
get receivables_url
expect(response).to be_successful
end
end

context "when Receivable is empty" do
it "renders a successful response" do
get receivables_url
expect(response).to be_successful
end
end

context "when last Receivable has date lass than first Receivable" do
let(:date_one_day_before){ { date: "2021-08-07" }}

it "renders a successful response" do
Receivable.create! valid_attributes
Receivable.create! valid_attributes.merge!(date_one_day_before)

get receivables_url
expect(response).to be_successful
end
end
end

Expand Down