Skip to content

Commit

Permalink
Add simple search form to activities#index.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Polito committed Aug 19, 2020
1 parent e57127c commit 5f796fa
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ gem 'devise-bootstrapped', github: 'king601/devise-bootstrapped', branch: 'boots
gem "pundit", "~> 2.1"

gem "pagy", "~> 3.8"

gem "ransack", "~> 2.3"
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ GEM
orm_adapter (0.5.0)
pagy (3.8.3)
pg (1.2.3)
polyamorous (2.3.2)
activerecord (>= 5.2.1)
public_suffix (4.0.5)
puma (4.3.5)
nio4r (~> 2.0)
Expand Down Expand Up @@ -159,6 +161,11 @@ GEM
rake (>= 0.8.7)
thor (>= 0.20.3, < 2.0)
rake (13.0.1)
ransack (2.3.2)
activerecord (>= 5.2.1)
activesupport (>= 5.2.1)
i18n
polyamorous (= 2.3.2)
rb-fsevent (0.10.4)
rb-inotify (0.10.1)
ffi (~> 1.0)
Expand Down Expand Up @@ -245,6 +252,7 @@ DEPENDENCIES
puma (~> 4.1)
pundit (~> 2.1)
rails (~> 6.0.3, >= 6.0.3.2)
ransack (~> 2.3)
sass-rails (>= 6)
selenium-webdriver
sendgrid-ruby (~> 6.3)
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ class ActivitiesController < ApplicationController
before_action :set_activity, only: [:show, :edit, :update, :destroy]

def index
@pagy, @activities = pagy(current_user.activities)
@q = current_user.activities.ransack(params[:q])
@pagy, @activities = pagy(@q.result(distinct: true))
end

def show
Expand Down
22 changes: 22 additions & 0 deletions app/views/activities/_search_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<%= search_form_for @q do |f| %>
<div class="form-group">
<%= f.label :difficulty_eq, "Difficuly" %>
<%= f.collection_select :difficulty_eq, Activity.difficulties.keys.each_with_index.map{ |activity,index| [index, activity.humanize] }, :first, :second, {prompt: true}, class: "custom-select" %>
</div>
<div class="form-group">
<%= f.label :category_eq, "Category" %>
<%= f.collection_select :category_eq, Activity.categories.keys.each_with_index.map{ |activity,index| [index, activity.humanize] }, :first, :second, {prompt: true}, class: "custom-select" %>
</div>
<div class="form-group">
<%= f.label :date_lteq %>
<%= f.date_field :date_lteq, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :date_gteq %>
<%= f.date_field :date_gteq, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
<%= link_to "Reset", activities_path %>
</div>
<% end %>
7 changes: 6 additions & 1 deletion app/views/activities/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<%= render "search_form" %>
<% unless @activities.empty? %>
<div class="table-responsive">
<table class="table">
Expand All @@ -21,5 +22,9 @@
<%== pagy_bootstrap_nav(@pagy) %>
</div>
<% else %>
<p><%= link_to "Add Your First Activity", new_activity_path %></p>
<% if params[:q].nil? %>
<p><%= link_to "Add Your First Activity", new_activity_path %></p>
<% else %>
<p>No results.</p>
<% end %>
<% end %>
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<main class="container">
<%= render partial: "shared/flash" %>
<%= yield %>
<%= debug(params) %>
</main>
</body>
</html>
5 changes: 5 additions & 0 deletions test/fixtures/action_text/rich_texts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
# record: name_of_fixture (ClassOfFixture)
# name: content
# body: <p>In a <i>million</i> stars!</p>

confirmed_user_with_searchable_activity_text:
record: confirmed_user_with_searchable_activity_4 (Activity)
name: description
body: <p>In a <i>million</i> stars!</p>
46 changes: 45 additions & 1 deletion test/fixtures/activities.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,48 @@ another_confirmed_user_activity_<%= i %>:
seconds: <%= rand(1..59) %>
distance: <%= rand(1..26) %>
unit: miles
<% end %>
<% end %>

confirmed_user_with_searchable_activity_1:
date: <%= Time.zone.now %>
user: confirmed_user_with_searchable_activities
hours: <%= rand(0..2) %>
minutes: <%= rand(1..59) %>
seconds: <%= rand(1..59) %>
distance: <%= rand(1..26) %>
unit: miles
difficulty: easy
category: run

confirmed_user_with_searchable_activity_2:
date: <%= 1.month.ago %>
user: confirmed_user_with_searchable_activities
hours: <%= rand(0..2) %>
minutes: <%= rand(1..59) %>
seconds: <%= rand(1..59) %>
distance: <%= rand(1..26) %>
unit: miles
difficulty: moderate
category: long_run

confirmed_user_with_searchable_activity_3:
date: <%= 1.month.from_now %>
user: confirmed_user_with_searchable_activities
hours: <%= rand(0..2) %>
minutes: <%= rand(1..59) %>
seconds: <%= rand(1..59) %>
distance: <%= rand(1..26) %>
unit: miles
difficulty: hard
category: workout

confirmed_user_with_searchable_activity_4:
date: <%= 1.year.ago %>
user: confirmed_user_with_searchable_activities
hours: <%= rand(0..2) %>
minutes: <%= rand(1..59) %>
seconds: <%= rand(1..59) %>
distance: <%= rand(1..26) %>
unit: miles
difficulty: hard
category: race
7 changes: 6 additions & 1 deletion test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ confirmed_user_with_activities:
another_confirmed_user_with_activities:
email: another_confirmed_user_with_activities@example.com
confirmed_at: <%= Time.zone.now %>
encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>

confirmed_user_with_searchable_activities:
email: confirmed_user_with_searchable_activities@example.com
confirmed_at: <%= Time.zone.now %>
encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>

0 comments on commit 5f796fa

Please sign in to comment.