-
-
Save phensalves/2f62f6dcf7c36706468fe6dd7a929f6b to your computer and use it in GitHub Desktop.
N + 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def list | |
## FILTER | |
@dealweekdays = DealWeekday.active | |
if params[:search_term] | |
search_name = @dealweekdays.like_name(params[:search_term]) | |
search_district = @dealweekdays.district_by_name(params[:search_term]) | |
joined = search_name + search_district | |
joined_ids = joined.uniq.map{|bar| bar.id} | |
@dealweekdays = DealWeekday.where(id:joined_ids) | |
end | |
@dealweekdays = @dealweekdays.by_city(params[:city_id]) if params[:city_id].present? | |
@dealweekdays = @dealweekdays.by_district(params[:bairro]) if params[:bairro].present? | |
if params[:beers].present? | |
beer_array = params[:beers].split(',') | |
@dealweekdays = @dealweekdays.by_beer(beer_array) | |
end | |
## ORDER | |
me = [params[:lat],params[:lng]] | |
me = [params[:centerlatitude],params[:centerlongitude]] if me.include?(nil) | |
if !me.include?(nil) | |
#@bars = @bars.by_distance(origin: me) | |
end | |
resp_json = [] | |
response = @dealweekdays.group_by{|e| e.deal} | |
response.each do |deal_group| | |
internal = deal_group[1].group_by{|e| e.bar} | |
internal.each do |bar_group| | |
begin | |
array = bar_group[1] | |
deal = array[0].deal | |
today = Weekday.today(deal.city) | |
dealweekday_ids = array.map{|bar| bar.id} | |
deal_weekdays = DealWeekday.where(id:dealweekday_ids) | |
deal_today = deal_weekdays.where(weekday:today).first | |
bar = array[0].bar | |
bar_hash = Hash.new | |
bar_hash = { | |
id: bar.id, | |
name: bar.name, | |
bairro: bar.district.name, | |
telefone: bar.phone, | |
address: bar.address, | |
latitude: bar.latitude, | |
longitude: bar.longitude | |
} | |
hash = Hash.new | |
hash = { | |
id: deal.id, | |
title: deal.title, | |
image: deal.image.home.url, | |
discount: deal.discount, | |
available: deal_today.nil? ? false : true, | |
consumption_time: deal_today.mobile_formatted_consumption_time, | |
bar: bar_hash | |
} | |
rescue | |
puts "ERROR BUILDING HASH" | |
end | |
resp_json << hash | |
end | |
end | |
respond_to do |format| | |
format.json { render :json => resp_json} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment