Module: Rooftop::Queries::ClassMethods

Defined in:
lib/rooftop/queries/queries.rb

Instance Method Summary collapse

Instance Method Details

#all(args = {}) ⇒ Object

‘all’ needs to have a querystring param passed to really get all. It should be -1 but for some reason that’s not working.



75
76
77
# File 'lib/rooftop/queries/queries.rb', line 75

def all(args = {})
  super({per_page: Rooftop::Queries::PER_PAGE}.merge(args))
end

#find_by!(args) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/rooftop/queries/queries.rb', line 65

def find_by!(args)
  results = find_by(args)
  if results.present?
    results
  else
    raise Rooftop::RecordNotFoundError
  end
end

#where(args) ⇒ Object Also known as: find_by

We need to fix up the ‘where()` filter. WP-API expects a url format for filters like this: /?filter=foo. But we have a magic hash key to allow us to send things which aren’t mangled.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rooftop/queries/queries.rb', line 11

def where(args)
  args = HashWithIndifferentAccess.new(args)
  # the fact that 'slug' is referred to in the db as 'name' is irritating. Let's fix that
  # in queries so we can specify {slug: "foo"}
  if args.keys.collect(&:to_sym).include?(:slug)
    if args[:slug].is_a?(Array)
      args[:post_name__in] ||= []
      args[:slug].each do |slug|
        args[:post_name__in] << slug
      end
    else
      args[:name] = args[:slug]
    end
    args.delete(:slug)
  end

  if args.keys.collect(&:to_sym).include?(:id)
    if args[:id].is_a?(Array)
      args[:post__in] ||= []
      args[:id].each do |id|
        args[:post__in] << id
      end
    else
      args[:page_id] = args[:id]
    end
    args.delete(:id)
  end

  if args.keys.include?('per_page')
    per_page = args['per_page']
    args[:no_filter] ||= []
    args[:no_filter] << :per_page unless args[:no_filter].include?('per_page')
  else
    per_page = Rooftop::Queries::PER_PAGE
  end

  if args.keys.collect(&:to_sym).include?(:no_filter)
    args_to_filter = args.except(*args[:no_filter]).except(:no_filter)
    args_not_to_filter = args.except(args_to_filter).except(:no_filter)
    filters =  args_to_filter.inject({}) {|hash,pair| hash["filter[#{pair.first}]"] = pair.last; hash}
    filters = {per_page: per_page}.merge(filters).merge(args_not_to_filter)
  else
    #TODO DRY
    filters =  args.inject({}) {|hash,pair| hash["filter[#{pair.first}]"] = pair.last; hash}
    filters = {per_page: per_page}.merge(filters)
  end

  # we probably want every result without pagination, unless we specify otherwise
  #Call the Her `where` method with our new filters
  super().where(filters)
end