Last active
September 3, 2015 15:26
-
-
Save khadzhinov/3d954505a078c691adf3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# | |
# Table name: devices | |
# | |
# id :integer not null, primary key | |
# user_id :integer | |
# token :string(255) | |
# device_type :string(255) | |
# name :string(255) | |
# os :string(255) | |
# created_at :datetime | |
# updated_at :datetime | |
# | |
class Device < ActiveRecord::Base | |
belongs_to :user, touch: true | |
before_create :generate_access_token | |
validates :device_type, :name, :os, :user_id, presence: true | |
private | |
def generate_access_token | |
self.token = SecureRandom.hex | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# | |
# Table name: users | |
# | |
# id :integer not null, primary key | |
# email :string(255) not null | |
# crypted_password :string(255) not null | |
# salt :string(255) not null | |
# created_at :datetime | |
# updated_at :datetime | |
# admin :boolean default(FALSE) | |
# stripe_id :string(255) | |
# last4 :string(255) | |
# name :string(255) | |
# | |
class User < ActiveRecord::Base | |
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i | |
validates :email, :name, :password, :device_limit, :event_limit, :discount, presence: true | |
validates :password, length: { minimum: 8 } | |
validates :device_limit, numericality: { only_integer: true, :greater_than_or_equal_to => 0, :message => ' count is invalid' } | |
validates :event_limit, numericality: { only_integer: true, :greater_than_or_equal_to => 0, :message => ' count is invalid' } | |
validates :discount, numericality: { only_integer: true, :greater_than_or_equal_to => 0, :less_than_or_equal_to => 100, :message => ' is invalid' } | |
has_many :devices, dependent: :destroy | |
def update_card(token) | |
if stripe_id.parent? | |
customer = Stripe::Customer.retrieve(stripe_id) | |
else | |
customer = Stripe::Customer.create(email: email, description: "ID " + id.to_s) | |
self.stripe_id = customer.id | |
self.save | |
end | |
card = customer.cards.create({ card: token }) # customer.source.create for new Stripe API | |
customer.default_card = card.id # default_source for new Stripe API | |
customer.save | |
self.last4 = card[:last4] | |
self.save | |
end | |
def add_new_device(device_attrs) | |
raise Exceptions::DeviceLimitReached.new("device_limit_reached") if self.devices.count >= self.device_limit | |
self.devices.create(device_attrs) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment