class ScopeSqlCounter::Syntax
Constants
- COUNTER_SQL_QUERY
Attributes
association_key[R]
conditions[R]
context[R]
count_alias[R]
Public Class Methods
new(context:, association_key:, conditions: nil, count_alias: nil)
click to toggle source
# File lib/scope_sql_counter/syntax.rb, line 16 def initialize(context:, association_key:, conditions: nil, count_alias: nil) @context = context @association_key = association_key @conditions = conditions @count_alias = count_alias end
Public Instance Methods
call()
click to toggle source
# File lib/scope_sql_counter/syntax.rb, line 23 def call if !context.respond_to?(:select_values) || context.select_values.blank? ["#{context.table_name}.*", query].join(', ') else query end end
Private Instance Methods
association()
click to toggle source
# File lib/scope_sql_counter/syntax.rb, line 33 def association reflection = context.reflect_on_association(association_key) reflection.through_reflection || reflection end
association_table_name()
click to toggle source
# File lib/scope_sql_counter/syntax.rb, line 39 def association_table_name if association.macro == :has_and_belongs_to_many association.join_table else association.table_name end end
conditions_sql()
click to toggle source
# File lib/scope_sql_counter/syntax.rb, line 60 def conditions_sql String.new.tap do |str| if conditions.present? str << "AND #{conditions}" end end end
count_alias_sql()
click to toggle source
# File lib/scope_sql_counter/syntax.rb, line 56 def count_alias_sql count_alias.to_s.presence || "#{association_key}_count" end
query()
click to toggle source
# File lib/scope_sql_counter/syntax.rb, line 47 def query COUNTER_SQL_QUERY.gsub(':target', association_table_name) .gsub(':foreign_key', association.foreign_key) .gsub(':context', context.table_name) .gsub(':conditions', conditions_sql) .gsub(':alias', count_alias_sql) .squish end