Class: Api::Vendor::V1::ReviewsFilter

Inherits:
Object
  • Object
show all
Includes:
Api::V5::PaginationConcernFilter
Defined in:
app/filters/api/vendor/v1/reviews_filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Api::V5::PaginationConcernFilter

#page_number, #per_page

Instance Attribute Details

#branch_idObject

Returns the value of attribute branch_id.



8
9
10
# File 'app/filters/api/vendor/v1/reviews_filter.rb', line 8

def branch_id
  @branch_id
end

#collectionsObject

Returns the value of attribute collections.



8
9
10
# File 'app/filters/api/vendor/v1/reviews_filter.rb', line 8

def collections
  @collections
end

#error_messageObject

Returns the value of attribute error_message.



8
9
10
# File 'app/filters/api/vendor/v1/reviews_filter.rb', line 8

def error_message
  @error_message
end

#restaurant_idObject

Returns the value of attribute restaurant_id.



8
9
10
# File 'app/filters/api/vendor/v1/reviews_filter.rb', line 8

def restaurant_id
  @restaurant_id
end

Instance Method Details

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



80
81
82
83
84
85
86
87
# File 'app/filters/api/vendor/v1/reviews_filter.rb', line 80

def as_json(context, _version, options = {})
  options[:each_serializer] = Api::Vendor::V1::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



18
19
20
21
22
# File 'app/filters/api/vendor/v1/reviews_filter.rb', line 18

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

#global_scopeObject



12
13
14
15
16
# File 'app/filters/api/vendor/v1/reviews_filter.rb', line 12

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

#metaObject



89
90
91
92
93
94
95
# File 'app/filters/api/vendor/v1/reviews_filter.rb', line 89

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

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



24
25
26
27
28
29
30
31
32
# File 'app/filters/api/vendor/v1/reviews_filter.rb', line 24

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(branch_id:, restaurant_id:) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/filters/api/vendor/v1/reviews_filter.rb', line 34

def scope_by_blogger(branch_id:, restaurant_id:)
  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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/filters/api/vendor/v1/reviews_filter.rb', line 62

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

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

  self
end

#stars_countObject



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

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



50
51
52
53
54
55
56
57
58
59
60
# File 'app/filters/api/vendor/v1/reviews_filter.rb', line 50

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

  self.collections = collections.order("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")
  self
end