Last active
August 29, 2015 14:01
-
-
Save sclinede/af06f2ae29cf7655bc61 to your computer and use it in GitHub Desktop.
Recalculate Company Rating for Company
This file contains hidden or 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
company_id = 11973147 | |
wrong_field = :active_reviews_count | |
# before | |
CoreDenormalization::Fields::Company::Reviews.value_as_string(object: company_id, field: wrong_field) | |
sql_for_data = initialize_statement # with company_id filter | |
data = ActiveRecord::Base.connection.execute sql_for_data | |
hash_with_object_key = {data.delete(:object_id) => data} | |
CoreDenormalization::Fields::Company::Reviews.instance.write_data hash_with_object_key | |
# after | |
CoreDenormalization::Fields::Company::Reviews.value_as_string(object: company_id, field: wrong_field) | |
CompanyRating::Calculator.calculate(Company.find(company_id)) |
This file contains hidden or 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
def recalc_denorm_field_for_company(field_class_name, company_id) | |
# field_class_name = 'CoreDenormalization::Fields::Company::Reviews' | |
# company_id = 11973147 | |
# wrong_field = :active_reviews_count | |
change_pattern = %r((\A(.*\n)*)(\s+OFFSET %{offset}\s+LIMIT %{limit})) | |
change_to_sql = "WITH object_select AS (%s) SELECT * FROM object_select WHERE object_id = #{company_id}" | |
field_class = field_class_name.constantize | |
# before | |
# field_class.value_as_string(object: company_id, field: wrong_field) | |
sql_for_data = change_to_sql % change_pattern.match(field_class.instance.initialize_statement)[1] | |
data = field_class.instance.send(:work_connection).execute(sql_for_data)[0] | |
hash_with_object_key = {data.delete('object_id') => data} | |
field_class.instance.write_data hash_with_object_key | |
# after | |
# field_class.value_as_string(object: company_id, field: wrong_field) | |
CompanyRating::Calculator.calculate(Company.find(company_id)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment