Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a dummy event to left-align results in slider #67

Merged
merged 6 commits into from
Nov 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/admin/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
end
column :event_type
column :location do |event|
link_to event.location.name, admin_location_path(event.location.id), :target => :blank
if event.location.present?
link_to event.location.name, admin_location_path(event.location.id), :target => :blank
else
''
end
end
column :start_date
column :end_date
Expand Down
12 changes: 12 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@ def authenticate_admin_user!
def after_sign_in_path_for(resource_or_scope)
current_user.admin == true ? admin_root_path : root_url
end

def fill_with_dummies(events)
event_count = events.count
if event_count != 0 && event_count < 3
if event_count == 1
events << Event.dummy
end
events << Event.dummy
events = Kaminari.paginate_array(events)
end
events
end
end
6 changes: 4 additions & 2 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ def show

def scope
if params[:location_id]
Location.find(params[:location_id]).events.current
events = Location.find(params[:location_id]).events.current
fill_with_dummies(events)
elsif params[:latitude] && params[:longitude]
if params[:radius].nil?
params[:radius] = 10
end
Event.by_distance(params[:latitude], params[:longitude], params[:radius]).current
events = Event.by_distance(params[:latitude], params[:longitude], params[:radius]).current
fill_with_dummies(events)
else
Event.for_date(params[:year], params[:month], params[:day])
end
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/favorites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ class FavoritesController < ApplicationController
respond_to :json

def index
favorites = current_user.favorites.for_current_events.order('created_at desc').
page(page_param).per(per_page_param)

favorites = current_user.favorites.for_current_events.order('created_at desc')
favorites = fill_with_dummies(favorites).page(page_param).per(per_page_param)
render_json_with_pagination_for(favorites)
end

Expand Down
5 changes: 5 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,9 @@ def get_and_process_entities(ner_path)
case_insensitive_admin_entities.distinct.map {|admin_entity| admin_entity.add_event(self)}
end
end

def self.dummy
new(id: -1, name: "_DUMMY", url: "#",
image: "http://images.artbotapp.com.s3-website-us-west-2.amazonaws.com/3b3b3b.png")
end
end
3 changes: 2 additions & 1 deletion app/models/event_linker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ def get_results

def get_scored_results(limit=5)
get_results.map {|result| score_result(result)}
.map {|result| limit_events(result, 6)}
.sort {|x,y| y[:score] <=> x[:score]}
.take(limit)
.map {|result| limit_events(result, 6)}
end

def limit_events(result, count)
result[:events] = result[:events].take(count)
result[:events] << Event.dummy while result[:events].count < 3
result
end

Expand Down
16 changes: 12 additions & 4 deletions spec/features/api/user_requests_events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@

expect(first_event_from_json['location']['name']).to eq location_name
expect(first_event_from_json['id']).to eq first_event.id
expect(event_ids).to match_array([first_event.id, last_event.id])
expect(event_ids).to include(first_event.id, last_event.id)
# Includes dummy events
expect(event_ids).to eq [first_event.id, last_event.id, -1]
expect(event_ids).not_to include(past_event.id)
end

Expand All @@ -58,7 +60,9 @@

json_response = parse_response_from(curb)
event_ids = json_response['events'].map {|e| e['id']}
expect(event_ids).to eq [nearby_event.id]
expect(event_ids).to include(nearby_event.id)
# Includes dummy events
expect(event_ids).to eq [nearby_event.id, -1, -1]
expect(event_ids).not_to include(distant_event.id, past_event.id)
end

Expand Down Expand Up @@ -131,8 +135,12 @@
ordered_event_ids = results.map {|result| result['events'].map {|event| event['id']}}.flatten

expect(ordered_tag_names).to eq [admin_tag.name, tag.name, entity.name]
expect(ordered_event_ids).to eq [top_related_event.id, lesser_related_event.id,
entity_related_event.id]
expect(ordered_event_ids).to include(top_related_event.id, lesser_related_event.id,
entity_related_event.id)
# Events with dummy ids are -1
expect(ordered_event_ids).to eq [top_related_event.id, -1, -1,
lesser_related_event.id, -1, -1,
entity_related_event.id, -1, -1]
expect(ordered_event_ids).not_to include(event.id, unrelated_event.id, past_event.id)
end

Expand Down