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
############################################
def
wood_based_building_system_product_attributes=(wood_based_building_system_product_attributes)
wood_based_building_system_product_attributes.each do |attributes|
if attributes[:id].blank?
wood_based_building_system_products.build(attributes)
else
wood_based_building_system_product =
wood_based_building_system_products.detect { |t| t.id ==
attributes[:id].to_i}
wood_based_building_system_product.attributes = attributes
end
end
end
def save_wood_based_building_system_products
wood_based_building_system_products.each do |t|
if t.should_destroy?
t.destroy
else
t.save(false)
end
end
end
def
non_wood_based_building_system_product_attributes=(non_wood_based_building_system_product_attributes)
non_wood_based_building_system_product_attributes.each do |
attributes|
if attributes[:id].blank?
non_wood_based_building_system_products.build(attributes)
else
non_wood_based_building_system_product =
non_wood_based_building_system_products.detect { |t| t.id ==
attributes[:id].to_i}
non_wood_based_building_system_product.attributes = attributes
end
end
end
def save_non_wood_based_building_system_products
non_wood_based_building_system_products.each do |t|
if t.should_destroy?
t.destroy
else
t.save(false)
end
end
end
def
wood_windows_doors_product_attributes=(wood_windows_doors_product_attributes)
wood_windows_doors_product_attributes.each do |attributes|
if attributes[:id].blank?
wood_windows_doors_products.build(attributes)
else
wood_windows_doors_product =
wood_windows_doors_products.detect { |t| t.id == attributes[:id].to_i}
wood_windows_doors_product.attributes = attributes
end
end
end
def save_wood_windows_doors_products
wood_windows_doors_products.each do |t|
if t.should_destroy?
t.destroy
else
t.save(false)
end
end
end
def
non_wood_windows_and_doors_product_attributes=(non_wood_windows_and_doors_product_attributes)
non_wood_windows_and_doors_product_attributes.each do |attributes|
if attributes[:id].blank?
non_wood_windows_and_doors_products.build(attributes)
else
non_wood_windows_and_doors_product =
non_wood_windows_and_doors_products.detect { |t| t.id ==
attributes[:id].to_i}
non_wood_windows_and_doors_product.attributes = attributes
end
end
end
def save_non_wood_windows_and_doors_products
non_wood_windows_and_doors_products.each do |t|
if t.should_destroy?
t.destroy
else
t.save(false)
end
end
end
def millwork_product_attributes=(millwork_product_attributes)
millwork_product_attributes.each do |attributes|
if attributes[:id].blank?
millwork_products.build(attributes)
else
millwork_product = millwork_products.detect { |t| t.id ==
attributes[:id].to_i}
millwork_product.attributes = attributes
end
end
end
def save_millwork_products
millwork_products.each do |t|
if t.should_destroy?
t.destroy
else
t.save(false)
end
end
end
def
wood_based_panel_product_attributes=(wood_based_panel_product_attributes)
wood_based_panel_product_attributes.each do |attributes|
if attributes[:id].blank?
wood_based_panel_products.build(attributes)
else
wood_based_panel_product = wood_based_panel_products.detect { |
t| t.id == attributes[:id].to_i}
wood_based_panel_product.attributes = attributes
end
end
end
def save_wood_based_panel_products
wood_based_panel_products.each do |t|
if t.should_destroy?
t.destroy
else
t.save(false)
end
end
end
def flooring_product_attributes=(flooring_product_attributes)
flooring_product_attributes.each do |attributes|
if attributes[:id].blank?
flooring_products.build(attributes)
else
flooring_product = flooring_products.detect { |t| t.id ==
attributes[:id].to_i}
flooring_product.attributes = attributes
end
end
end
def save_flooring_products
flooring_products.each do |t|
if t.should_destroy?
t.destroy
else
t.save(false)
end
end
end
def
cabinetry_and_furniture_product_attributes=(cabinetry_and_furniture_product_attributes)
cabinetry_and_furniture_product_attributes.each do |attributes|
if attributes[:id].blank?
cabinetry_and_furniture_products.build(attributes)
else
cabinetry_and_furniture_product =
cabinetry_and_furniture_products.detect { |t| t.id ==
attributes[:id].to_i}
cabinetry_and_furniture_product.attributes = attributes
end
end
end
def save_cabinetry_and_furniture_products
cabinetry_and_furniture_products.each do |t|
if t.should_destroy?
t.destroy
else
t.save(false)
end
end
end
def
outdoor_wood_based_product_attributes=(outdoor_wood_based_product_attributes)
outdoor_wood_based_product_attributes.each do |attributes|
if attributes[:id].blank?
outdoor_wood_based_products.build(attributes)
else
outdoor_wood_based_product =
outdoor_wood_based_products.detect { |t| t.id == attributes[:id].to_i}
outdoor_wood_based_product.attributes = attributes
end
end
end
def save_outdoor_wood_based_products
outdoor_wood_based_products.each do |t|
if t.should_destroy?
t.destroy
else
t.save(false)
end
end
end
def
outdoor_non_wood_based_product_attributes=(outdoor_non_wood_based_product_attributes)
outdoor_non_wood_based_product_attributes.each do |attributes|
if attributes[:id].blank?
outdoor_non_wood_based_products.build(attributes)
else
outdoor_non_wood_based_product =
outdoor_non_wood_based_products.detect { |t| t.id ==
attributes[:id].to_i}
outdoor_non_wood_based_product.attributes = attributes
end
end
end
def save_outdoor_non_wood_based_products
outdoor_non_wood_based_products.each do |t|
if t.should_destroy?
t.destroy
else
t.save(false)
end
end
end
def
other_wood_based_building_product_attributes=(other_wood_based_building_product_attributes)
other_wood_based_building_product_attributes.each do |attributes|
if attributes[:id].blank?
other_wood_based_building_products.build(attributes)
else
other_wood_based_building_product =
other_wood_based_building_products.detect { |t| t.id ==
attributes[:id].to_i}
other_wood_based_building_product.attributes = attributes
end
end
end
def save_other_wood_based_building_products
other_wood_based_building_products.each do |t|
if t.should_destroy?
t.destroy
else
t.save(false)
end
end
end
def
other_non_wood_based_building_product_attributes=(other_non_wood_based_building_product_attributes)
other_non_wood_based_building_product_attributes.each do |
attributes|
if attributes[:id].blank?
other_non_wood_based_building_products.build(attributes)
else
other_non_wood_based_building_product =
other_non_wood_based_building_products.detect { |t| t.id ==
attributes[:id].to_i}
other_non_wood_based_building_product.attributes = attributes
end
end
end
def save_other_non_wood_based_building_products
other_non_wood_based_building_products.each do |t|
if t.should_destroy?
t.destroy
else
t.save(false)
end
end
end
def
stone_processed_products_product_attributes=(stone_processed_products_product_attributes)
stone_processed_products_product_attributes.each do |attributes|
if attributes[:id].blank?
stone_processed_products_products.build(attributes)
else
stone_processed_products_product =
stone_processed_products_products.detect { |t| t.id ==
attributes[:id].to_i}
stone_processed_products_product.attributes = attributes
end
end
end
def save_stone_processed_products_products
stone_processed_products_products.each do |t|
if t.should_destroy?
t.destroy
else
t.save(false)
end
end
end
def insulation_product_attributes=(insulation_product_attributes)
insulation_product_attributes.each do |attributes|
if attributes[:id].blank?
insulation_products.build(attributes)
else
insulation_product = insulation_products.detect { |t| t.id ==
attributes[:id].to_i}
insulation_product.attributes = attributes
end
end
end
def save_insulation_products
insulation_products.each do |t|
if t.should_destroy?
t.destroy
else
t.save(false)
end
end
end
end