app/models/cadenero/v1/account.rb in cadenero-0.0.2.a2 vs app/models/cadenero/v1/account.rb in cadenero-0.0.2.a3
- old
+ new
@@ -1,14 +1,15 @@
module Cadenero::V1
class Account < ActiveRecord::Base
belongs_to :owner, :class_name => "Cadenero::User"
has_many :members, :class_name => "Cadenero::Member"
- has_many :users, :through => :members
+ has_many :users, :through => :members, :class_name => "Cadenero::User"
accepts_nested_attributes_for :owner
attr_accessible :name, :subdomain, :owner_attributes, :owner
validates :subdomain, :presence => true, :uniqueness => true
+ validates :owner, :presence => true
# Creates an accout and assign the provided [Cadenero::User] as owner to the account
# @param [Hash] params list
# @example
# Example for the params JSON: {name: "Testy", subdomain: "test",
@@ -27,34 +28,46 @@
# Create a database schema using the subdomain
def create_schema
Apartment::Database.create(subdomain)
end
+ # Generate authentication token unless already exists.
+ def ensure_authentication_token
+ reset_authentication_token if authentication_token.blank?
+ end
+
# Generate authentication token unless already exists and save the record.
def ensure_authentication_token!
reset_authentication_token! if authentication_token.blank?
end
# Generate new authentication token (a.k.a. "single access token").
- def reset_authentication_token!
+ def reset_authentication_token
self.authentication_token = self.class.authentication_token
end
- # Generate a token checking if one does not already exist in the database.
- # @return the authentication_token
- def authentication_token
- generate_token(:authentication_token)
+ # Generate new authentication token and save the record.
+ def reset_authentication_token!
+ reset_authentication_token
+ save(:validate => false)
end
- # Generate a token by looping and ensuring does not already exist.
- # @params [String] column is the name of the column that has the authentication token
- # @return a unique generated authentication_token
- def generate_token(column)
- loop do
- token = SecureRandom.base64(15).tr('+/=lIO0', 'pqrsxyz')
- break token unless Account.where({ column => token }).first
+ class << self
+ # Generate a token checking if one does not already exist in the database.
+ def authentication_token
+ generate_token(:authentication_token)
end
- end
+ protected
+ # Generate a token by looping and ensuring does not already exist.
+ # @params [String] column is the name of the column that has the authentication token
+ # @return a unique generated authentication_token
+ def generate_token(column)
+ loop do
+ token = SecureRandom.base64(15).tr('+/=lIO0', 'pqrsxyz')
+ break token unless Account.where({ column => token }).first
+ end
+ end
+ end
end
end