Module: RelatedQueryMethods::ClassMethods

Defined in:
lib/relata/related_query_methods.rb

Instance Method Summary collapse

Instance Method Details

#filter_by_date_between(params, relation) ⇒ Object



36
37
38
39
40
# File 'lib/relata/related_query_methods.rb', line 36

def filter_by_date_between(params, relation) 
  relation.where("published_at < ?", params[:before])
  relation.where("published_at > ?", params[:after])
  relation
end

#filter_by_exact(facet, value, relation) ⇒ Object



28
29
30
# File 'lib/relata/related_query_methods.rb', line 28

def filter_by_exact(facet, value, relation) 
  !value.empty? ? relation.where(facet => value) : relation
end

#filter_by_has_many(facet, value, relation) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/relata/related_query_methods.rb', line 9

def filter_by_has_many(facet, value, relation) 
  
  table_name = self.table_name

  if !value.empty?
    relation.preload(facet).select("#{table_name}.*, COUNT(#{facet}.id) AS count").from("#{table_name}, #{facet}").where("#{table_name}.id = #{facet}.post_id").group("#{table_name}.id").having("count > 0") 
  else 
    relation 
  end 
end

#filter_by_published_at(value, relation) ⇒ Object



32
33
34
# File 'lib/relata/related_query_methods.rb', line 32

def filter_by_published_at(value, relation) 
  value ? relation.where("published_at < ?", 1.month.ago) : relation 
end


20
21
22
23
24
25
26
# File 'lib/relata/related_query_methods.rb', line 20

def filter_by_related(facet, value, relation) 
  if value 
    relation.preload(facet).select("posts.*, COUNT(#{facet}.id) AS comment_count").from("posts, #{facet}").group("posts.id").having("comment_count > 0") 
  else 
    relation 
  end 
end