Class: ScopeSqlCounter::Syntax

Inherits:
Object
  • Object
show all
Defined in:
lib/scope_sql_counter/syntax.rb

Constant Summary collapse

COUNTER_SQL_QUERY =
"  (\n    SELECT COUNT(:target.id)\n    FROM :target\n    WHERE :target.:foreign_key = :context.id\n    :conditions\n  ) AS :alias\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:, association_key:, conditions: nil, count_alias: nil) ⇒ Syntax



16
17
18
19
20
21
# 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

Instance Attribute Details

#association_keyObject (readonly)

Returns the value of attribute association_key.



14
15
16
# File 'lib/scope_sql_counter/syntax.rb', line 14

def association_key
  @association_key
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



14
15
16
# File 'lib/scope_sql_counter/syntax.rb', line 14

def conditions
  @conditions
end

#contextObject (readonly)

Returns the value of attribute context.



14
15
16
# File 'lib/scope_sql_counter/syntax.rb', line 14

def context
  @context
end

#count_aliasObject (readonly)

Returns the value of attribute count_alias.



14
15
16
# File 'lib/scope_sql_counter/syntax.rb', line 14

def count_alias
  @count_alias
end

Instance Method Details

#callObject



23
24
25
26
27
28
29
# 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