Skip to content

Instantly share code, notes, and snippets.

@ColPanic
Created January 20, 2011 22:22
Show Gist options
  • Save ColPanic/788808 to your computer and use it in GitHub Desktop.
Save ColPanic/788808 to your computer and use it in GitHub Desktop.
I un-KLCannonized it... this looks ok, yeah?
# This will associate ANY device type with an ESI and create the
# associated EsiDeviceType record properly populated.
def add_device
#Look up device to add
@device = Kernel.const_get(self.model_name).find(params[:id])
#Ok, this line is some serious black magic and needs explained...
# 1. Extract the name of the model (device type)
# 2. Create the matching EsiDevice object by
# 3. mapping any attributes present in the device model with a matching
# attribute in the EsiDevice.
@esi_dev = Kernel.const_get("Esi#{self.model_name}").new(
@device.attributes.delete_if{|k,v|
!Kernel.const_get("Esi#{self.model_name}").respond_to?(k)
})
#Set the ESI ID in both?
@device.esi_id = params[:esi_id]
@esi_dev.esi_id = params[:esi_id]
#Save both
@device.save!
@esi_dev.save!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment