Author Topic: Rails Revisted  (Read 3046 times)

webwhy

  • Jackass IV
  • Posts: 608
  • Karma: +15/-10
Rails Revisted
« on: October 27, 2005, 10:50:29 AM »
At work we are now using rails exclusively for the web front end for a major project, and so far it's been everything as advertised.

I've asked permission to post some code here, and since this is an early revision (no logging or exception handling...plus a logic error or two) and it contains no ip my bosses didn't have a problem with it.

This is the Developer class in our application, and it is a "model" in rails.  Basically, that mean objects of this class map to a row in the developers table.  The model not only encapsulates the row data, but also business behavior related to the entity.  The best part?  Where's the SQL? not necessary...Where's the mammoth mapping config ala hibernate or ibatis?  not necessary either...beautiful...

Code: [Select]
class Developer < ActiveRecord::Base
# set up entity relationships
has_many :locations
has_many :addresses
belongs_to :user

# validation code
validates_presence_of :name, :addresses, :dma
validates_presence_of :user, :if => :active?
validates_uniqueness_of :number, :dma
validates_associated :user, :addresses
validates_length_of :name, :maximum => 255
validates_length_of :number, :maximum => 25, :allow_nil => true
validates_length_of :dma, :maximum => 50

def active?
self.active == 1
end

def open
self.activate
end

# effectively deletes the developer
# sets active flag false, user to null, and closes all locations
def close
transaction do
deactivate
self.close_locations
self.user = nil
self.save
end
end

# add location to the developer
# grants access to the location
def open_location( location )
transaction do
unless location.nil?
location.open if !location.opened?
location.add_user( self.user )
locations << location
end
self.save
end
end

# location transfer class method
# this method transfers locations between two developers while under one database transaction
# all location rights are removed for the "selling" developer's primary user and his children users
def self.transfer( seller, buyer, locations )
transaction do
selling_user = seller.user
# find all the child users of the selling user, then add the selling_user
users_to_remove = Array.new
users_to_remove << selling_user.users << selling_user
locations.each { |location|
# Take the difference of the the two user collections to remove the old users
new_users = location.users - users_to_remove
location.users = new_users
# Open the new location under the new developer
buyer.open_location( location)
}
end
end

# change the primary contact for the developer
# this method is passed two parameters which indicated whether the user should retain access to the developer's locations
# and just have primary contact privleges removed
# if this parameter is false, the user's privileges to all the developers locations is removed.
# The first parameter is the new primary_user
def change_contact( new_user, retain_access = false)
transaction do
old_user = self.user
old_user.locations |= self.locations unless old_user.nil?
self.user = new_user
new_user.locations += self.locations
self.save
end
end

# returns a list of all the users who have access to at least one of the
# developers locations
def users
d_users = Array.new
self.locations.collect { |location|
d_users |= location.users
}
d_users
end

        def to_s
                "#{self.dma}[#{self.name}]"
        end


private
def close_locations
self.locations.each { |location| location.close }
end

def activate
self.active = 1
end

def deactivate
self.active = 0
end
end

« Last Edit: October 27, 2005, 10:55:18 AM by webwhy »