Skip to content

Instantly share code, notes, and snippets.

@plicjo
Created April 17, 2016 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plicjo/ebf8026d3a8b37142645bd2ca2f6cfa2 to your computer and use it in GitHub Desktop.
Save plicjo/ebf8026d3a8b37142645bd2ca2f6cfa2 to your computer and use it in GitHub Desktop.
A Simple Rails Search Engine
module ApplicationHelper
YEAR_DESC_OPENED = 1996
OLDEST_SUPPORTED_BIRTH_YEAR = 1910
def years_in_business
(YEAR_DESC_OPENED..current_year).to_a.reverse
end
def current_year
Date.current.year
end
def twelve_months
1..12
end
def month_days
1..31
end
def birth_years
(OLDEST_SUPPORTED_BIRTH_YEAR..current_year).to_a.reverse
end
end
class ClothingMicroReport
include ActiveModel::Model
attr_reader :year, :month
def initialize(params)
@year = params['year'] || Date.today.year
@month = params['month'] || Date.current.month
end
def call
Clothing.where("extract(year from created_at) = ? AND extract(month from created_at) = ?", year, month)
end
end
require 'rails_helper'
describe ClothingMicroReport do
let(:created_at) { Time.current }
let(:params) do
{ 'year' => "#{created_at.year}",
'month' => "#{created_at.month}"
}
end
describe '#call' do
subject { described_class.new(params).call }
let!(:clothing) do
Fabricate(:clothing, current_adult: Fabricate(:adult), created_at: created_at)
end
it 'queries Clothings based on created_at' do
expect(subject).to include clothing
expect(subject).to be_kind_of Clothing::ActiveRecord_Relation
end
end
end
Given(/^there is a clothing from (\d+)\/(\d+)$/) do |month, year|
adult = Fabricate(:adult)
Fabricate(:clothing, created_at: "#{year}-#{month}-01", current_adult: adult.id)
end
When(/^I select "(.*?)" in the micro report "(.*?)" dropdown$/) do |number, type|
select number, from: "clothing_micro_report_#{type}"
end
Then(/^I should see the clothing from (\d+)\/(\d+)$/) do |month, year|
expect(page).to have_content /#{month}\/(\d+)\/#{year}/
end
Then(/^I should not see the clothing from (\d+)\/(\d+)$/) do |month, year|
expect(page).to_not have_content /#{month}\/(\d+)\/#{year}/
end
Feature: Clothing micro reports
Volunteers will be able to get a list of all clothing objects that occurred in a month.
Scenario: Get a micro report of all clothing transacations in a month
Given the user is signed in
And there is a clothing from 6/2015
And there is a clothing from 7/2015
When I click "Clothing Micro-Reports"
And I select "6" in the micro report "month" dropdown
And I select "2015" in the micro report "year" dropdown
And I click "Get Report"
Then I should see the clothing from 6/2015
And I should not see the clothing from 7/2015
class ClothingMicroReportsController < ApplicationController
before_action :authenticate_user!, :restrict_ip_address
def index
@clothing_micro_report = ClothingMicroReport.new(clothing_micro_report_params)
end
private
def clothing_micro_report_params
params['clothing_micro_report'] || {}
end
end
.row
.col-xs-12
%span.h1
Clothing Micro-Reports
.hidden-print.top-10px.bottom-10px
= form_for @clothing_micro_report, method: :get do |f|
= f.select :month, twelve_months, selected: @clothing_micro_report.month
= f.select :year, years_in_business, selected: @clothing_micro_report.year
= f.submit 'Get Report', class: 'btn btn-info btn-xs top-10pxleft-10px'
.row
.col-xs-12
.panel.panel-primary
.panel-heading
= "#{@clothing_micro_report.month}/#{@clothing_micro_report.year}"
%table.table.table-striped.table-bordered.table-hover
%thead
%tr
%th Date
%th Adults Clothed
%th Children Clothed
%th Articles of Clothing
%tbody
- @clothing_micro_report.call.each do |clothing|
%tr
%td= clothing.created_at.strftime('%m/%d/%Y')
%td= clothing.num_adults_clothed
%td= clothing.num_children_clothed
%td= clothing.articles_of_clothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment