MODEL for company
class Company < ActiveRecord::Base
has_many :facilities
has_many :green_products
has_many :discontinued_markets
has_many :wood_based_building_system_products
has_many :non_wood_based_building_system_products
has_many :wood_windows_doors_products
has_many :non_wood_windows_and_doors_products
has_many :millwork_products
has_many :wood_based_panel_products
has_many :flooring_products
has_many :cabinetry_and_furniture_products
has_many :outdoor_wood_based_products
has_many :outdoor_non_wood_based_products
has_many :other_wood_based_building_products
has_many :other_non_wood_based_building_products
has_many :stone_processed_products_products
has_many :insulation_products
after_update :save_facilities
after_update :save_green_products
after_update :save_discontinued_markets
after_update :save_wood_based_building_system_products
after_update :save_non_wood_based_building_system_products
after_update :save_wood_windows_doors_products
after_update :save_non_wood_windows_and_doors_products
after_update :save_millwork_products
after_update :save_wood_based_panel_products
after_update :save_flooring_products
after_update :save_cabinetry_and_furniture_products
after_update :save_outdoor_wood_based_products
after_update :save_outdoor_non_wood_based_products
after_update :save_other_wood_based_building_products
after_update :save_other_non_wood_based_building_products
after_update :save_stone_processed_products_products
after_update :save_insulation_products
def self.search(search)
if search[:test]
province = search.delete(:province)
test = search.delete(:test)
action = search.delete(:action)
controller = search.delete(:controller)
topdelcheckbox = search.delete(:topdelcheckbox)
sql = "0=0 and province LIKE ?"
varlength = search.length
counter = 1
if (varlength == 1)
sql += " and "
end
if (varlength >= 2)
sql += " and ("
end
search.each do |key, val|
if(key != "province")
if(key == "building_systems")
sql += "wood_based_building_systems = 1 or
non_wood_based_building_system = 1"
elsif(key == "windows_and_doors")
sql += "wood_windows_and_doors = 1 or
non_wood_windows_and_doors = 1"
elsif(key == "outdoor")
sql += "outdoor_wood_based_products = 1 or
outdoor_non_wood_based_products = 1"
elsif(key == "other_building_products")
sql += "other_wood_based_building_products = 1 or
other_non_wood_based_building_products = 1"
elsif(key == "wood_based_panels")
sql += "wood_based_panels = 1"
elsif(key == "millwork")
sql += "millwork = 1"
elsif(key == "cabinetry_and_furniture")
sql += "cabinetry_and_furniture = 1"
elsif(key == "flooring")
sql += "flooring = 1"
elsif(key == "stone_processed_products")
sql += "stone_processed_products = 1"
elsif(key == "insulation")
sql += "insulation = 1"
end
if (varlength != counter)
sql += " or "
end
counter = counter + 1
end
end
if (varlength >= 2)
sql += ") "
end
find(:all, :conditions => [sql, "%#{province}%"], :order =>
"name")
end
end
def facility_attributes=(facility_attributes)
facility_attributes.each do |attributes|
if attributes[:id].blank?
facilities.build(attributes)
else
facility = facilities.detect { |t| t.id ==
attributes[:id].to_i}
facility.attributes = attributes
end
end
end
def save_facilities
facilities.each do |t|
if t.should_destroy?
t.destroy
else
t.save(false)
end
end
end
def green_product_attributes=(green_product_attributes)
green_product_attributes.each do |attributes|
if attributes[:id].blank?
green_products.build(attributes)
else
green_product = green_products.detect { |t| t.id ==
attributes[:id].to_i}
green_product.attributes = attributes
end
end
end
def save_green_products
green_products.each do |t|
if t.should_destroy?
t.destroy
else
t.save(false)
end
end
end
def discontinued_market_attributes=(discontinued_market_attributes)
discontinued_market_attributes.each do |attributes|
if attributes[:id].blank?
discontinued_markets.build(attributes)
else
discontinued_market = discontinued_markets.detect { |t| t.id
== attributes[:id].to_i}
discontinued_market.attributes = attributes
end
end
end
def save_discontinued_markets
discontinued_markets.each do |t|
if t.should_destroy?
t.destroy
else
t.save(false)
end
end
end