Skip to content

Instantly share code, notes, and snippets.

@bogdan
Created February 5, 2016 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bogdan/f70d28711928de42a923 to your computer and use it in GitHub Desktop.
Save bogdan/f70d28711928de42a923 to your computer and use it in GitHub Desktop.
# == Schema Information
#
# Table name: origins
#
# id :integer not null, primary key
# order_number :string(255) not null
# site_id :integer not null
# subtotal :decimal(25, 2) not null
# order_date :datetime not null
# ip_address :string(255)
# internal_ip_address :string(45)
# created_at :datetime
# updated_at :datetime
# visitor_id :integer
# person_id :integer
# is_new :boolean default(FALSE), not null
# type :string(25)
# traffic_source_id :integer not null
# event_category_id :integer not null
#
class Purchase < Event
DEFAULT_TRAFFIC_SOURCE = 'Post-checkout'
#------------+
# Attributes |
#------------+
# Protect any sensitive attributes, since purchases controller comes in from public endpoint
# TODO uncomment this and check test suite
# attr_protected :site_id
has_custom_attributes :shipping_zip
has_custom_attributes :shipping_address
#-------------+
# Validations |
#-------------+
validates_presence_of :order_date
validates_presence_of :subtotal
validates_presence_of :person
#------------------+
# Instance methods |
#------------------+
def reward
rewards.first
end
def to_s
"##{order_number}"
end
def to_payload
as_json(only: [],
methods: [:id, :type, :order_number, :subtotal, :order_date, :customer_id]
).symbolize_keys.merge(
# Backward compatibility:
# We started to support multiple coupon codes only after some point
# And we need to maintain backward compatiblity and send them as String, not as Array
coupon_code: coupons.to_a.join(','),
)
end
private
def prevent_negative_subtotal
self.subtotal = 0 if subtotal.to_i < 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment