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
<%= form_for(:search, class: 'form-inline', url: users_path, method: :get, remote: true) do |form| %> | |
<div class="form-group"> | |
<%= form.label :dob %> | |
<%= form.text_field :dob, class: 'form-control', data: { behavior: 'daterangepicker' }, value: dob_from_parameters %> | |
</div> | |
<%= form.submit "Search", class: 'btn btn-success' %> | |
<% end %> | |
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
module ApplicationHelper | |
def dob_from_parameters | |
if params[:search].present? | |
params[:search][:dob] | |
end | |
end | |
end |
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
/* globals daterangepicker, $ */ | |
"use strict"; | |
$(function(){ | |
$('[data-behavior=daterangepicker]').daterangepicker({ | |
locale: { format: 'DD/MM/YYYY'}, | |
cancelLabel: 'Clear' | |
}); | |
$('[data-behavior=daterangepicker]').on('cancel.daterangepicker', function(){ |
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
<tr> | |
<td><%= user.name %></td> | |
<td><%= user.dob %></td> | |
<td><%= link_to("Edit", edit_user_path(user)) %></td> | |
</tr> |
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
<%= form_for(:search, class: 'form-inline', url: users_path, method: :get) do |form| %> | |
<div class="form-group"> | |
<%= form.label :dob %> | |
<%= form.text_field :dob, class: 'form-control', data: { behavior: 'daterangepicker' } %> | |
</div> | |
<%= form.submit "Search", class: 'btn btn-success' %> | |
<% end %> | |
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
<h1>User list</h1> | |
<%= render 'filter_form' %> | |
<br> | |
<% if @users.present? %> | |
<table class="table table-bordered"> | |
<thead> | |
<th>Name</th> | |
<th>Date of birth</th> | |
<th>Action</th> | |
</thead> |
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
class User < ApplicationRecord | |
validates :name, presence: true | |
scope :having_dob_between, ->(start_date, end_date) { where(dob: start_date..end_date) } | |
end |
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
class UsersController < ApplicationController | |
before_action :load_user, only: [:edit, :update] | |
def index | |
if params[:search] && params[:search][:dob].present? | |
start_date, end_date = params[:search][:dob].split(' - ') | |
@users = User.having_dob_between(start_date, end_date) | |
else | |
@users = User.all |
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
FactoryBot.define do | |
factory :user do | |
name { Faker::Name.name } | |
end | |
end |
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
require 'rails_helper' | |
RSpec.describe UsersController, type: :controller do | |
describe 'before actions' do | |
describe 'load_user' do | |
it 'is expected to define before action' do | |
is_expected.to use_before_action(:load_user) | |
end | |
end |
NewerOlder