Sha256: e9764d6021cc4902ba3ebd5be7797b011fbeb4dc0c46248c7836c339409ffb7b
Contents?: true
Size: 1.08 KB
Versions: 1
Compression:
Stored size: 1.08 KB
Contents
class CounterCacheUpdate::TableUpdatorService < ServicePattern::Service attr_reader :model_class, :reflection def initialize(reflection:) @reflection = reflection @model_class = @reflection.class_name.constantize end def execute! model_class.connection.execute(sql) ServicePattern::Response.new(success: true) end private def column_name @column_name ||= proc do if reflection.options[:counter_cache] == true "#{reflection.active_record.name.pluralize.underscore}_count" else reflection.options[:counter_cache] end end.call end def count_sql "SELECT COUNT(*) FROM #{reflection_table_name} WHERE #{reflection_table_name}.#{reflection_primary_key} = #{table_name}.#{primary_key}" end def primary_key model_class.primary_key end def reflection_primary_key reflection.active_record.primary_key end def reflection_table_name reflection.active_record.table_name end def sql "UPDATE #{table_name} SET #{column_name} = (#{count_sql})" end def table_name model_class.table_name end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
counter_cache_update-0.0.0 | app/services/counter_cache_update/table_updator_service.rb |