Skip to content

Instantly share code, notes, and snippets.

@jamesgecko
Created April 9, 2012 19:56
Show Gist options
  • Save jamesgecko/2346188 to your computer and use it in GitHub Desktop.
Save jamesgecko/2346188 to your computer and use it in GitHub Desktop.
Date compare, line 17
class Bill < ActiveRecord::Base
belongs_to :customer
attr_accessible :amount, :date_due, :paid
validates :amount, presence: true
validates :date_due, presence: true
before_create :default_values
before_save :update_date_paid
after_save :update_history
def default_values
self.paid ||= false
true # Don't prevent save by returning false.
end
scope :pending, where(:paid => false)
scope :overdue, pending.where('date_due < ?', Date.today)
def self.find_paid
where(:paid => true)
end
def update_date_paid
if self.paid && self.date_paid.nil?
write_attribute(:date_paid, Time.now)
elsif !self.paid && !self.date_paid.nil?
write_attribute(:date_paid, nil)
end
end
def update_history
History.add_entry('bills', self.id, self.previous_changes)
end
end
# == Schema Information
#
# Table name: bills
#
# id :integer not null, primary key
# customer_id :integer
# amount :integer
# date_paid :datetime
# paid :boolean
# created_at :datetime not null
# updated_at :datetime not null
# date_due :date
#
# app/views/customers/index.erb.html
<h1>Listing customers</h1>
<table class="table table-striped sortable">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>County</th>
<th>State</th>
<th>Zip</th>
<th></th>
<th></th>
</tr>
</thead>
<% @customers.each do |customer| %>
<tr>
<td><%= link_to customer.name, customer %>
<% if !customer.active %><span class="label">Inactive</span><% end %>
<% if customer.bills.overdue.any? %><span class="label label-important">Overdue</span><% end %>
</td>
<td><%= customer.address %></td>
<td><%= customer.county %></td>
<td><%= customer.state %></td>
<td><%= customer.zip %></td>
<td><%= link_to 'Edit', edit_customer_path(customer) %></td>
<td><%= link_to 'Destroy', customer, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<%= link_to 'New Customer', new_customer_path, class: 'btn' %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment