removing initialize from Company

master
Matt Huntington 8 years ago
parent 6a192c4555
commit 477f329600

@ -1,16 +1,6 @@
class Company
attr_reader :id, :name, :industry, :employees
# connect to postgres
DB = PG.connect(host: "localhost", port: 5432, dbname: 'contacts')
def initialize(opts = {})
@id = opts["id"].to_i
@name = opts["name"]
@industry = opts["industry"]
if opts["employees"]
@employees = opts["employees"]
end
end
DB = PG.connect({:host => "localhost", :port => 5432, :dbname => 'contacts'})
def self.all
results = DB.exec(
@ -31,17 +21,17 @@ class Company
last_company_id = nil;
results.each do |result|
if result["id"] != last_company_id
company = Company.new({
"id" => result["id"],
company = {
"id" => result["id"].to_i,
"name" => result["name"],
"industry" => result["industry"],
"employees" => []
});
};
companies.push(company)
last_company_id = result["id"]
end
if result["person_id"]
companies.last.employees.push({
companies.last["employees"].push({
"id" => result["person_id"].to_i,
"name" => result["person_name"],
"age" => result["age"].to_i,
@ -76,12 +66,12 @@ class Company
})
end
end
return Company.new({
"id" => results.first["id"],
return {
"id" => results.first["id"].to_i,
"name" => results.first["name"],
"industry" => results.first["industry"],
"employees" => employees
})
}
end
def self.create(opts={})
@ -92,12 +82,16 @@ class Company
RETURNING id, name, industry;
SQL
)
return Company.new(results.first)
return {
"id" => results.first["id"].to_i,
"name" => results.first["name"],
"industry" => results.first["industry"]
}
end
def self.delete(id)
results = DB.exec("DELETE FROM companies WHERE id=#{id};")
return { deleted: true }
return { "deleted" => true }
end
def self.update(id, opts={})
@ -109,6 +103,10 @@ class Company
RETURNING id, name, industry;
SQL
)
return Company.new(results.first)
return {
"id" => results.first["id"].to_i,
"name" => results.first["name"],
"industry" => results.first["industry"]
}
end
end

@ -47,11 +47,11 @@ class Person
people.push(new_person)
end
if result["company_id"]
employer = Company.new({
"id" => result["company_id"],
employer = {
"id" => result["company_id"].to_i,
"name" => result["company"],
"industry" => result["industry"]
})
}
people.last["employers"].push(employer)
end
end
@ -94,11 +94,11 @@ class Person
employers = []
results.each do |result|
if result["company_id"]
employers.push(Company.new({
"id" => result["company_id"],
employers.push( {
"id" => result["company_id"].to_i,
"name" => result["company"],
"industry" => result["industry"],
}))
"industry" => result["industry"]
})
end
end
person = {

Loading…
Cancel
Save