Created
June 6, 2013 23:07
-
-
Save kshsieh/5725730 to your computer and use it in GitHub Desktop.
i'm oddly proud of this?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
desc "seperate medium and surface product properties" | |
task :seperate_medium_and_surface_properties => :environment do | |
# create a new surface property | |
Property.create(name: "Surface", presentation: "Surface") | |
# assign property ids to an actionable variables to work with later | |
# don't assume that medium id is what it is in the database, so don't hardcode it | |
medium_property_id = Property.find_by_name("Medium").id | |
surface_property_id = Property.find_by_name("Surface").id | |
# create an array of all "Medium" ProductProperties | |
actionable_properties = ProductProperty.all.select { |pp| pp.property_id == medium_property_id } | |
# here comes the tricky part | |
actionable_properties.each do |prop| | |
product_id = prop.product_id | |
# search for " on " with spaces to make it easier, returns an array ["Oil", " on ", "Canvas"] for example | |
value_array = prop.value.rpartition(" on ") | |
if value_array[1] = " on " # then it is a correctly worded and has a surface. should be split into two properties | |
# current medium property will remain the medium property associated with the current product | |
prop.update_attribute(:value, value_array[0]) | |
# create a new ProductProperty for surface associated to correct product | |
ProductProperty.create(product_id: product_id, property_id: surface_property_id, value: value_array[2]) | |
else # assume that artist only created a medium. raise a dataerror for human to check | |
prod.data_errors.new(message: "ProductProperty for Medium needs a review") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment