Skip to content

Instantly share code, notes, and snippets.

@dudo
Created February 14, 2016 00:14
Show Gist options
  • Save dudo/f25767f00c874842a005 to your computer and use it in GitHub Desktop.
Save dudo/f25767f00c874842a005 to your computer and use it in GitHub Desktop.
# classes
class User < ApplicationRecord
has_many :buckets, -> { order(row_order: :asc) }
def current_transactions
@current_transactions ||= plaid_user.current_transactions
end
def get_token
@token ||= Plaid.exchange_token(public_token).access_token
end
def plaid_user(levels: %w(connect))
@plaid_user ||= Api::Plaid.new(get_token, api_levels: levels)
# call '#upgrade' on this object to promote api_level
end
end
class Bucket < ApplicationRecord
belongs_to :user
def included_transactions
user.current_transactions.select do |t|
t.name == original_name &&
t.category_id.to_s == original_category.to_s &&
(t.amount.abs-fluctuation .. t.amount.abs+fluctuation).include?(amount.to_f.abs)
end
end
def percent_complete
[included_transactions.size/frequency.to_f*100, 100].min
end
end
# Serializers
class UserSerializer < ActiveModel::Serializer
has_many :buckets
end
class BucketSerializer < ActiveModel::Serializer
attributes :percent_complete
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment