Module: ReputationSystem::QueryBuilder::ClassMethods

Defined in:
lib/reputation_system/query_builder.rb

Constant Summary collapse

DELTA =
0.000001
REPUTATION_JOIN_STATEMENT =
"LEFT JOIN rs_reputations ON %s.id = rs_reputations.target_id AND rs_reputations.target_type = ? AND rs_reputations.reputation_name = ? AND rs_reputations.active = ?"
REPUTATION_FIELD_STRING =
"COALESCE(rs_reputations.value, 0)"

Instance Method Summary collapse

Instance Method Details

#build_condition_statement(reputation_name, conditions = nil, srn = nil, normalize = false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/reputation_system/query_builder.rb', line 45

def build_condition_statement(reputation_name, conditions=nil, srn=nil, normalize=false)
  conditions ||= [""]
  conditions = [conditions] unless conditions.is_a? Array
  if normalize
    normalized_reputation_name = sanitize_sql_array(["normalized_%s", reputation_name.to_s])
    conditions[0] = conditions[0].gsub(normalized_reputation_name, normalized_field_string(srn))
  end
  conditions[0] = conditions[0].gsub(reputation_name.to_s, REPUTATION_FIELD_STRING)
  conditions
end

#build_join_statement(table_name, class_name, srn, joins = nil) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/reputation_system/query_builder.rb', line 56

def build_join_statement(table_name, class_name, srn, joins=nil)
    joins ||= []
    joins = [joins] unless joins.is_a? Array
    rep_join = sanitize_sql_array([REPUTATION_JOIN_STATEMENT, class_name.to_s, srn.to_s, true])
    rep_join = sanitize_sql_array([rep_join, table_name])
    joins << rep_join
end

#build_select_statement(table_name, reputation_name, select = nil, srn = nil, normalize = false) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/reputation_system/query_builder.rb', line 28

def build_select_statement(table_name, reputation_name, select=nil, srn=nil, normalize=false)
  select = sanitize_sql_array(["%s.*", table_name]) unless select
  if normalize
    sanitize_sql_array(["%s, %s AS normalized_%s", select, normalized_field_string(srn), reputation_name.to_s])
  else
    sanitize_sql_array(["%s, %s AS %s", select, REPUTATION_FIELD_STRING, reputation_name.to_s])
  end
end

#build_select_statement_with_reputation_only(table_name, reputation_name, srn = nil, normalize = false) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/reputation_system/query_builder.rb', line 37

def build_select_statement_with_reputation_only(table_name, reputation_name, srn=nil, normalize=false)
  if normalize
    sanitize_sql_array(["%s AS normalized_%s", normalized_field_string(srn), reputation_name.to_s])
  else
    sanitize_sql_array(["%s AS %s", REPUTATION_FIELD_STRING, reputation_name.to_s])
  end
end