Skip to content

Instantly share code, notes, and snippets.

@marcamillion
Created April 6, 2011 07:20
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 marcamillion/905267 to your computer and use it in GitHub Desktop.
Save marcamillion/905267 to your computer and use it in GitHub Desktop.
# == Schema Information
# Schema version: 20110330215959
#
# Table name: clients
#
# id :integer not null, primary key
# email :string(255)
# user_id :integer
# created_at :datetime
# updated_at :datetime
# token :string(255)
#
class Client < ActiveRecord::Base
acts_as_voter
devise :token_authenticatable
belongs_to :user
has_many :projects, :order => 'created_at DESC', :dependent => :destroy
has_many :stages, :through => :projects
has_many :uploads, :through => :stages
has_one :ownership, :dependent => :destroy
has_one :designer, :through => :ownership
before_create :reset_authentication_token
validates :email,
:presence => true,
#:uniqueness => true,
:format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }
validate :number_of_clients
def number_of_clients
errors.add(:base, "You cannot add another client") if
Authorization.current_user.client_threshold_reached?
end
def role_symbols
[:client]
end
end
def create
client = Client.new(params[:client])
if client.valid?
Client.transaction do
Ownership.transaction do
client.save!
Ownership.create!(:client => client, :designer => current_user)
end
end
end
unless client.new_record?
respond_with(client, :status => :created, :location => client) do |format|
flash.now[:notice] = 'Client was successfully created.'
format.html { redirect_to(client) }
format.js { render :partial => "clients/show", :locals => {:client => client}, :layout => false, :status => :created }
end
else
respond_with(client.errors, :status => :unprocessable_entity) do |format|
format.js { render :json => client.errors, :layout => false, :status => :unprocessable_entity }
format.html { render :action => "new" }
end
end
end
Started GET "/settings" for 127.0.0.1 at 2011-04-06 02:18:10 -0500
Processing by Devise::RegistrationsController#edit as HTML
User Load (1.7ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
#<User id: 1, email: "test@abc.com", encrypted_password: "$2a$10$qUbNGm6lZ366jRiE0vK0gOpxbGXD5JmfqWmH1lfLlCEC...", password_salt: "$2a$10$qUbNGm6lZ366jRiE0vK0gO", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 280, current_sign_in_at: "2011-04-06 07:15:31", last_sign_in_at: "2011-04-06 02:34:45", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", username: "test", f_name: "Test", l_name: "User", created_at: "2011-01-22 07:17:45", updated_at: "2011-04-06 07:15:31", invitation_token: nil, invitation_sent_at: nil, plan_id: 3, current_state: nil, confirmation_token: nil, confirmed_at: "2011-02-11 23:19:15", confirmation_sent_at: "2011-02-11 23:18:20">
Plan Load (0.3ms) SELECT "plans".* FROM "plans" WHERE ("plans"."id" = 3) LIMIT 1
Client Load (2.8ms) SELECT "clients".* FROM "clients" INNER JOIN "ownerships" ON "clients".id = "ownerships".client_id WHERE (("ownerships".designer_id = 1)) ORDER BY created_at DESC
SQL (0.3ms) SELECT COUNT(*) FROM "projects" WHERE ("projects".client_id = 57)
SQL (0.2ms) SELECT COUNT(*) FROM "projects" WHERE ("projects".client_id = 56)
SQL (0.2ms) SELECT COUNT(*) FROM "projects" WHERE ("projects".client_id = 55)
SQL (0.2ms) SELECT COUNT(*) FROM "projects" WHERE ("projects".client_id = 54)
SQL (0.2ms) SELECT COUNT(*) FROM "projects" WHERE ("projects".client_id = 52)
SQL (0.1ms) SELECT COUNT(*) FROM "projects" WHERE ("projects".client_id = 49)
SQL (0.2ms) SELECT COUNT(*) FROM "projects" WHERE ("projects".client_id = 40)
SQL (0.2ms) SELECT COUNT(*) FROM "projects" WHERE ("projects".client_id = 35)
SQL (0.1ms) SELECT COUNT(*) FROM "projects" WHERE ("projects".client_id = 34)
SQL (0.1ms) SELECT COUNT(*) FROM "projects" WHERE ("projects".client_id = 5)
SQL (0.2ms) SELECT COUNT(*) FROM "projects" WHERE ("projects".client_id = 4)
SQL (0.1ms) SELECT COUNT(*) FROM "projects" WHERE ("projects".client_id = 1)
SQL (0.2ms) SELECT COUNT(*) FROM "clients" INNER JOIN "ownerships" ON "clients".id = "ownerships".client_id WHERE (("ownerships".designer_id = 1))
SQL (0.2ms) SELECT COUNT(*) FROM "uploads" WHERE ("uploads".user_id = 1)
Upload Load (3.6ms) SELECT "uploads".* FROM "uploads" WHERE ("uploads".user_id = 1)
Rendered devise/registrations/edit.html.erb within layouts/application (346.6ms)
Completed 200 OK in 543ms (Views: 376.0ms | ActiveRecord: 11.1ms)
# == Schema Information
# Schema version: 20110131093541
#
# Table name: ownerships
#
# id :integer not null, primary key
# designer_id :integer
# client_id :integer
# created_at :datetime
# updated_at :datetime
#
class Ownership < ActiveRecord::Base
belongs_to :designer, :class_name => "User"
belongs_to :client
validates :designer_id, :presence => true
validates :client_id, :presence => true
# Only allow a 1:1 mapping
validates_uniqueness_of :designer_id, :scope => :client_id
end
# == Schema Information
# Schema version: 20110131093541
#
# Table name: projects
#
# id :integer not null, primary key
# name :string(255)
# description :string(255)
# notified :boolean
# created_at :datetime
# updated_at :datetime
# client_id :integer
#
class Project < ActiveRecord::Base
has_and_belongs_to_many :users
belongs_to :client
has_many :stages, :dependent => :destroy, :order => 'created_at DESC'
has_many :comments
validate :number_of_projects
validates :name, :presence => true
def number_of_projects
errors[:base] << "You cannot add another project" if
Authorization.current_user.projects_threshold_reached?
end
end
Started GET "/settings" for 127.0.0.1 at 2011-04-06 04:46:50 -0500
Processing by Devise::RegistrationsController#edit as HTML
User Load (1.1ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
#<User id: 1, email: "test@abc.com", encrypted_password: "$2a$10$qUbNGm6lZ366jRiE0vK0gOpxbGXD5JmfqWmH1lfLlCEC...", password_salt: "$2a$10$qUbNGm6lZ366jRiE0vK0gO", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 281, current_sign_in_at: "2011-04-06 08:57:40", last_sign_in_at: "2011-04-06 07:15:31", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", username: "test", f_name: "Test", l_name: "User", created_at: "2011-01-22 07:17:45", updated_at: "2011-04-06 08:57:40", invitation_token: nil, invitation_sent_at: nil, plan_id: 3, current_state: nil, confirmation_token: nil, confirmed_at: "2011-02-11 23:19:15", confirmation_sent_at: "2011-02-11 23:18:20">
Plan Load (0.3ms) SELECT "plans".* FROM "plans" WHERE ("plans"."id" = 3) LIMIT 1
SQL (0.3ms) SELECT COUNT(*) FROM "projects" INNER JOIN "clients" ON "projects".client_id = "clients".id WHERE (("clients".user_id = 1))
SQL (0.3ms) SELECT COUNT(*) FROM "clients" INNER JOIN "ownerships" ON "clients".id = "ownerships".client_id WHERE (("ownerships".designer_id = 1))
SQL (0.2ms) SELECT COUNT(*) FROM "uploads" WHERE ("uploads".user_id = 1)
Upload Load (3.2ms) SELECT "uploads".* FROM "uploads" WHERE ("uploads".user_id = 1)
Rendered devise/registrations/edit.html.erb within layouts/application (336.4ms)
Completed 200 OK in 519ms (Views: 344.5ms | ActiveRecord: 5.4ms)
# == Schema Information
# Schema version: 20110214082231
#
# Table name: users
#
# id :integer not null, primary key
# email :string(255)
# encrypted_password :string(128)
# password_salt :string(255)
# reset_password_token :string(255)
# remember_token :string(255)
# remember_created_at :datetime
# sign_in_count :integer
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string(255)
# last_sign_in_ip :string(255)
# username :string(255)
# f_name :string(255)
# l_name :string(255)
# created_at :datetime
# updated_at :datetime
# invitation_token :string(60)
# invitation_sent_at :datetime
# plan_id :integer
# current_state :string(255)
# confirmation_token :string(255)
# confirmed_at :datetime
# confirmation_sent_at :datetime
#
class User < ActiveRecord::Base
has_many :projects, :through => :clients
has_many :stages
has_many :uploads
has_many :comments, :dependent => :destroy
has_many :assignments
has_many :roles, :through => :assignments
belongs_to :plan
has_many :client_ownerships,
:class_name => 'Ownership',
:foreign_key => 'designer_id'
has_many :clients,
:through => :client_ownerships,
:order => 'created_at DESC'
def role_symbols
roles.map do |role|
role.name.underscore.to_sym
end
end
def space
total_size = 0
if self.uploads.count > 0
self.uploads.each do |upload|
total_size += upload[:image_file_size]
end
end
total_size
end
def client_threshold_reached?
self.plan.num_of_clients == self.clients.count
end
def remaining_client_count
self.plan.num_of_clients - self.clients.count
end
def number_of_projects
clients.collect{ |c| c.projects.count }.sum
end
def projects_threshold_reached?
self.plan.num_of_projects == self.number_of_projects
end
def remaining_projects_count
self.plan.num_of_projects - self.number_of_projects
end
def space_threshold_reached?
self.plan.storage == self.space
end
def space_left
(self.plan.storage * 1024 * 1024 * 1024) - self.space
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment