Skip to content

Instantly share code, notes, and snippets.

@songjiayang
Last active December 20, 2015 15:48
Show Gist options
  • Save songjiayang/6156446 to your computer and use it in GitHub Desktop.
Save songjiayang/6156446 to your computer and use it in GitHub Desktop.
notify ..
before_save :set_material_should_change_field
after_save :change_material_attributes
def set_material_should_change_field
option_changed_attributes = self.changed_attributes
@change_field = {}
if self.new_record?
@change_field.merge!( { :price => true, :lead_time => true } )
elsif option_changed_attributes.include?('price_min') || option_changed_attributes.include?('price_max')
@change_field.merge!( { :price => true })
elsif option_changed_attributes.include?('min_lead_time') || option_changed_attributes.include?('max_lead_time')
@change_field.merge!({ :lead_time => true })
end
@change_field
end
def change_material_attributes
return if @change_field.empty?
material = self.material
material_options = material.material_options
if @change_field[:price]
price_min = material_options.minimum(:price_min)
price_max = material_options.maximum(:price_max)
material.price_range = self.price_range
elsif @change_field[:lead_time]
min_lead_time = material_options.minimum(:min_lead_time)
max_lead_time = material_options.maximum(:max_lead_time)
material.lead_time_range = self.lead_time_range
end
material.save!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment