Class: Api::V5::ReviewsFilter

Inherits:
Object
  • Object
show all
Includes:
PaginationConcernFilter, ElasticAPM::SpanHelpers
Defined in:
app/filters/api/v5/reviews_filter.rb

Overview

Responsible to filter reviews data from db

Direct Known Subclasses

Aoa::V1::ReviewsFilter

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PaginationConcernFilter

#page_number, #per_page

Instance Attribute Details

#branch_idObject

Returns the value of attribute branch_id.



10
11
12
# File 'app/filters/api/v5/reviews_filter.rb', line 10

def branch_id
  @branch_id
end

#collectionsObject

Returns the value of attribute collections.



10
11
12
# File 'app/filters/api/v5/reviews_filter.rb', line 10

def collections
  @collections
end

#error_messageObject

Returns the value of attribute error_message.



10
11
12
# File 'app/filters/api/v5/reviews_filter.rb', line 10

def error_message
  @error_message
end

#restaurant_idObject

Returns the value of attribute restaurant_id.



10
11
12
# File 'app/filters/api/v5/reviews_filter.rb', line 10

def restaurant_id
  @restaurant_id
end

Instance Method Details

#as_json(context, _version, options = {}) ⇒ Object

Parameters:

  • context (Struct)

    Serialization Context

  • version (Integer)

    ApiV5::Constants::DEFAULT_MINOR_VERSION



97
98
99
100
101
102
103
104
# File 'app/filters/api/v5/reviews_filter.rb', line 97

def as_json(context, _version, options = {})
  options[:each_serializer] = Api::V5::ReviewSerializer if options[:each_serializer].blank?
  options[:meta] = meta if options[:meta] != false
  ActiveModelSerializers::SerializableResource.new(collections,
                                                   { serialization_context: context,
                                                     serializer: ActiveModel::Serializer::CollectionSerializer,
                                                     adapter: :json_api }.merge(options)).as_json
end

#filter_by_date_range(start_date, end_date) ⇒ Object



21
22
23
24
25
# File 'app/filters/api/v5/reviews_filter.rb', line 21

def filter_by_date_range(start_date, end_date)
  @start_date = start_date
  @end_date = end_date
  @filter_date_range = start_date.present? && end_date.present? ? true : false
end

#filter_rating(rating:) ⇒ Object



39
40
41
# File 'app/filters/api/v5/reviews_filter.rb', line 39

def filter_rating(rating:)
  self.collections = collections.where("reviews.rating = #{rating}")
end

#global_scopeObject



14
15
16
17
18
# File 'app/filters/api/v5/reviews_filter.rb', line 14

def global_scope
  self.collections = Review.joins(:reservation).where(reservations: { active: true })
  use_default_includes
  self
end

#metaObject



107
108
109
110
111
112
113
# File 'app/filters/api/v5/reviews_filter.rb', line 107

def meta
  {
    total: total,
    average: average,
    stars_count: stars_count,
  }
end

#scope_by(restaurant_id:, branch_id: nil) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'app/filters/api/v5/reviews_filter.rb', line 28

def scope_by(restaurant_id:, branch_id: nil)
  if branch_id.present?
    self.branch_id = branch_id
    scope_by_branch_id(branch_id, restaurant_id)
  else
    scope_by_restaurant_id(restaurant_id)
  end
  self
end

#scope_by_blogger(restaurant_id:, branch_id: nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/filters/api/v5/reviews_filter.rb', line 44

def scope_by_blogger(restaurant_id:, branch_id: nil)
  data = Review.blogger_scope.active_scope
  if branch_id.present? && restaurant_id.present?
    self.branch_id = branch_id
    self.collections = data.where(branch_id: branch_id).union_all(data.where(restaurant_id: restaurant_id))
  elsif restaurant_id.present?
    self.collections = data.where(restaurant_id: restaurant_id).includes(:restaurant)
  elsif branch_id.present?
    self.branch_id = branch_id
    self.collections = data.where(branch_id: branch_id).includes(:branch)
  else
    self.collections = data
  end
  self
end

#sort_by(key) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/filters/api/v5/reviews_filter.rb', line 77

def sort_by(key)
  return self if key.blank?

  key = key.to_s
  self.collections = if key.casecmp('rating asc').zero?
                       collections.reorder('rating ASC')
                     elsif key.casecmp('rating desc').zero?
                       collections.reorder('rating DESC')
                     elsif key.casecmp('priority asc').zero?
                       collections.reorder('priority ASC')
                     else
                       collections
                     end

  self
end

#stars_countObject



116
117
118
119
120
121
122
123
124
125
# File 'app/filters/api/v5/reviews_filter.rb', line 116

def stars_count
  {

    one: review_stat.one_star,
    two: review_stat.two_star,
    three: review_stat.three_star,
    four: review_stat.four_star,
    five: review_stat.five_star,
  }
end

#use_default_orderObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/filters/api/v5/reviews_filter.rb', line 61

def use_default_order
  raise 'Undefined collections' if collections.nil?

  default_order_query = ['reviews.featured = 1 DESC',
                         "reviews.review_photos_count > 0 AND reviews.review IS NOT NULL AND reviews.review <> '' DESC",
                         "reviews.review IS NOT NULL AND reviews.review <> '' DESC",
                         'reviews.rating DESC',
                         'reviews.created_at DESC']
  if branch_id.present? && restaurant_id.present?
    default_order_query.unshift("reviews.restaurant_id = #{restaurant_id} DESC")
  end
  self.collections = collections.order(default_order_query.join(', '))
  self
end