Skip to content

Instantly share code, notes, and snippets.

@dangalipo
Created October 7, 2011 03:37
Show Gist options
  • Select an option

  • Save dangalipo/1269382 to your computer and use it in GitHub Desktop.

Select an option

Save dangalipo/1269382 to your computer and use it in GitHub Desktop.
models
#user Model
class User < ActiveRecord::Base
PASSWORD_SALT = 'BC25635383433FDF2F5CF11AE8AA7380'
extend LegacyTable
legacy_table_name "tblUsers"
set_primary_key :ID
alias_attribute :street, :Street
alias_attribute :email, :EmailAddress
alias_attribute :first_name, :FirstName
alias_attribute :last_name, :LastName
alias_attribute :encrypted_password, :Password
alias_attribute :franchise_bsb, :BSBNumber
alias_attribute :franchise_phone_number, :Phone1
alias_attribute :franchise_abn, :ABN
alias_attribute :franchise_account_number, :AccountNumber
alias_attribute :franchise_ref, :Reference
delegate :plumbers_license, :to => :potential, :allow_nil => true
delegate :gas_license, :to => :potential, :allow_nil => true
belongs_to :suburb, :foreign_key => :SuburbID
belongs_to :invoice_logo
has_one :state, :through => :suburb
has_one :potential, :foreign_key => :UserID
has_one :van_driver, :foreign_key => :UserID
has_one :van, :through => :van_driver
has_many :territory_users, :foreign_key => :UserID
has_many :territories, :through => :territory_users
has_many :personal_times, :foreign_key => :TapDoctorID
has_many :recurring_personal_times, :foreign_key => :TapDoctorID
has_many :timetables, :foreign_key => :TapDoctorID
has_many :jobs, :foreign_key => :TapDoctorID
has_many :franchises, :foreign_key => :UserID, :class_name => 'SupplierFranchisee'
has_many :suppliers, :through => :franchises
has_many :product_prices, :foreign_key => :UserID
has_many :products, :through => :product_prices
has_many :invoices
validates :api_token, :uniqueness => true, :allow_blank => true
validates :uuid, :uniqueness => true, :allow_blank => true
validates_presence_of :ShippingSuburbID
validates_presence_of :SMSToManager
validates_presence_of :AvailablePublicHolidays
validates_presence_of :LastLoggedIn
delegate :time_zone, :to => :state
def name
[first_name.presence, last_name.presence].compact.join ' '
end
def to_s
"Dr #{last_name}"
end
def as_json
{
'id' => id,
'token' => api_token,
'plumbers_name' => name,
'plumbers_license' => plumbers_license,
'gas_license' => gas_license,
'franchise_name' => franchise_name,
'franchise_address' => franchise_address,
'franchise_phone_number' => franchise_phone_number,
'franchise_abn' => franchise_abn,
'franchise_terms_of_payment' => franchise_terms_of_payment,
'invoice_logo_filename' => invoice_logo_filename,
'invoice_logo_extension' => invoice_logo_extension
}
end
def start_times_for(date)
# No Sundays
if date.wday == 0
[]
# If there are any timetables, use them
elsif timetables.present?
abbr_dayname = date.strftime('%a').downcase
timetables.detect { |timetable| timetable.day.try(:downcase) == abbr_dayname }.start_times
# Otherwise, provide some default times
else
['8:00', '10:00', '13:00', '15:00']
end.collect do |time|
datetime = time_zone.parse "#{date.to_s} #{time}"
end.sort
end
def start_times(days=7)
times = days.times.collect do |i|
# Don't book for the current day
date = Date.today + 1.day + i.days
start_times_for date
end.flatten.compact.reject { |time| time.past? }
end
def available_times(days=7)
start_times(days).reject do |time|
recurring_personal_times.where(:Day => time.wday).present? || personal_times.where(:Date => time.to_date).present?
end.reject do |time|
jobs.where(:JobTime => time).present?
end
end
def generate_token
self.api_token = ActiveSupport::SecureRandom.hex(40)
end
def password=(password)
self.encrypted_password = encrypt_password(password)
end
def authenticate(password)
encrypted_password == encrypt_password(password)
end
## Builds the full address of the franchise using attributes on this model
# Street, Suburb, State Abbreviation Postcode
# EG: 72 Kings Park Road, West Perth, WA 6005
def franchise_address
"#{street}, #{suburb.try(:name)}, #{suburb.try(:state).try(:abbreviation)} #{suburb.try(:postcode)}"
end
# Parse the attached InvoiceLogo to find the filename
def invoice_logo_filename
return "" if invoice_logo.blank?
/(.*)\..*/.match(invoice_logo.ios_path).try(:captures).try(:first)
end
# Parse the attached InvoiceLogo to find the file extension
def invoice_logo_extension
return "" if invoice_logo.blank?
/.*\.(.*)/.match(invoice_logo.ios_path).try(:captures).try(:first)
end
# Demeter
def invoice_logo_web_path
return InvoiceLogo.no_logo_path if invoice_logo.blank?
invoice_logo.web_path || ""
end
def available_job_times
response = RestClient.get(URI.join(MEMBERS_TAPDOCTOR_URL, "index.php?module=operators&action=tapdoctoravailableblocks&tapdoctor=#{self.id}").to_s)
end
# Creates a Job (Continuation) with the given data
def book_job_continuation(job_id, time, duration)
old_job = Job.find_by_ID(job_id)
# Copy the attributes over from the previous job
new_job = Job.new
new_job.job_status = JobStatus.booked_job_status.id
new_job.time = Time.at(time.to_i)
new_job.time_end = Time.at(time.to_i + duration.to_i)
new_job.job = old_job
new_job.user = old_job.user
new_job.client = old_job.client
new_job.street = old_job.street
new_job.suburb = old_job.suburb
new_job.job_type = old_job.job_type
new_job.notes = old_job.notes
new_job.job_reference_source = old_job.job_reference_source
# Copy the line items over from the previous job
new_invoice = old_job.invoice.clone!
new_job.invoice = new_invoice
# Add a JobUpdate for the new job
JobUpdate.add_job_update(new_job)
new_job.save!
new_job
end
private
def encrypt_password(password)
Digest::MD5.hexdigest("#{password}#{PASSWORD_SALT}").upcase
end
public
end
#van model
class Van < ActiveRecord::Base
extend LegacyTable
legacy_table_name "tblVans"
set_primary_key :ID
alias_attribute :van_owner_id, :OwnerID
validates_presence_of :LicencePlate
validates_presence_of :OwnerID
validates_presence_of :VIN
end
#van_driver model
class VanDriver < ActiveRecord::Base
extend LegacyTable
legacy_table_name "tblVanDrivers"
set_primary_key :ID
alias_attribute :van_id, :VanID
alias_attribute :user_id, :UserID
belongs_to :van, :foreign_key => :VanID
belongs_to :user, :foreign_key => :UserID
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment