Skip to content

Instantly share code, notes, and snippets.

@isqad
Forked from sclinede/recalc_company_rating.rb
Last active August 29, 2015 14:21
Show Gist options
  • Save isqad/102eab119bb65b143eef to your computer and use it in GitHub Desktop.
Save isqad/102eab119bb65b143eef to your computer and use it in GitHub Desktop.
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))
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