Skip to content

Commit

Permalink
Paperworks are now searchable by date_min and date_max, #38
Browse files Browse the repository at this point in the history
  • Loading branch information
maugsbur committed Jan 31, 2014
1 parent ac634c3 commit f9600ba
Show file tree
Hide file tree
Showing 24 changed files with 551 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ gem 'sunspot_cell_jars'
gem 'progress_bar'

#Representers
gem 'roar', '~> 0.11.19'
# gem 'roar', '~> 0.11.19'
gem 'roar-rails', "0.1.0"
gem 'billit_representers', '0.8.0', path: '/home/maugsburger/workspace/'
gem 'billit_representers', '0.8.0'
# gem 'billit_representers', '0.7.2'
gem 'will_paginate', '~> 3.0'

Expand Down
17 changes: 6 additions & 11 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ GIT
sunspot_cell (0.1.2)
sunspot (>= 1.3.0)

PATH
remote: /home/maugsburger/workspace
specs:
billit_representers (0.8.0)
activemodel
roar (= 0.11.19)
roar-rails (= 0.1.0)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -45,6 +37,10 @@ GEM
multi_json (~> 1.0)
addressable (2.3.5)
arel (3.0.2)
billit_representers (0.8.0)
activemodel
roar (= 0.11.19)
roar-rails (= 0.1.0)
builder (3.0.4)
crack (0.4.1)
safe_yaml (~> 0.9.0)
Expand Down Expand Up @@ -86,7 +82,7 @@ GEM
origin (~> 1.0)
tzinfo (~> 0.3.22)
moped (1.5.0)
multi_json (1.8.2)
multi_json (1.8.4)
nokogiri (1.6.1)
mini_portile (~> 0.5.0)
options (2.3.0)
Expand Down Expand Up @@ -182,15 +178,14 @@ PLATFORMS
ruby

DEPENDENCIES
billit_representers (= 0.8.0)!
billit_representers (= 0.8.0)
database_cleaner
factory_girl_rails
faker
haml-rails (~> 0.4)
jquery-rails (~> 2.2)
progress_bar
rails (~> 3.2)
roar (~> 0.11.19)
roar-rails (= 0.1.0)
rspec-rails
sunspot_cell!
Expand Down
2 changes: 2 additions & 0 deletions app/assets/javascripts/billit/paperworks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/billit/paperworks.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Place all the styles related to the matching controller here.
They will automatically be included in application.css.
*/
194 changes: 194 additions & 0 deletions app/controllers/paperworks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
require 'billit_representers/representers/paperwork_collection_representer'
class PaperworksController < ApplicationController
respond_to :json, :xml
# GET /paperworks
# GET /paperworks.json
def index
@paperworks = Paperwork.all

respond_to do |format|
format.html # index.html.erb
format.json { render json: @paperworks }
end
end

# GET /paperworks/1
# GET /paperworks/1.json
def show
@paperwork = Paperwork.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render json: @paperwork }
end
end

# GET /paperworks/new
# GET /paperworks/new.json
def new
@paperwork = Paperwork.new

respond_to do |format|
format.html # new.html.erb
format.json { render json: @paperwork }
end
end

# GET /paperworks/1/edit
def edit
@paperwork = Paperwork.find(params[:id])
end

# POST /paperworks
# POST /paperworks.json
def create
@paperwork = Paperwork.new(params[:paperwork])

respond_to do |format|
if @paperwork.save
format.html { redirect_to @paperwork, notice: 'Paperwork was successfully created.' }
format.json { render json: @paperwork, status: :created, location: @paperwork }
else
format.html { render action: "new" }
format.json { render json: @paperwork.errors, status: :unprocessable_entity }
end
end
end

# PUT /paperworks/1
# PUT /paperworks/1.json
def update
@paperwork = Paperwork.find(params[:id])

respond_to do |format|
if @paperwork.update_attributes(params[:paperwork])
format.html { redirect_to @paperwork, notice: 'Paperwork was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @paperwork.errors, status: :unprocessable_entity }
end
end
end

# DELETE /paperworks/1
# DELETE /paperworks/1.json
def destroy
@paperwork = Paperwork.find(params[:id])
@paperwork.destroy

respond_to do |format|
format.html { redirect_to paperworks_url }
format.json { head :no_content }
end
end

# GET paperworks/search.json?q=search_string
def search
require 'will_paginate/array'
# Sunspot.remove_all(Paperwork) # descomentar para reindexar,
# Sunspot.index!(Paperwork.all) # en caso de cambio en modelo
search = search_for(params)
# @paperwork = search.results
puts "params"
puts params
puts "/params"
@paperworks = search.results
@paperworks.extend(Billit::PaperworkCollectionRepresenter)
respond_with @paperworks.to_json(params), represent_with: Billit::PaperworkCollectionRepresenter
# @paperwork.extend(Billit::BillCollectionPageRepresenter)
#respond_with @paperwork.to_json(params)#, represent_with: Billit::BillCollectionPageRepresenter
end

def filter_conditions(conditions)
@mongoid_attribute_names = ["_id", "created_at"] #FIX should probably have a greater scope
@search_attribute_names = ["q", "bill_id", "law_text"]
@range_field_types = [DateTime]
@range_modifier_min = "_min"
@range_modifier_max = "_max"

bill_range_fields = Paperwork.fields.dup
@range_field_types.each do |type|
bill_range_fields.reject! {|field_name, metadata| metadata.options[:type]!= type}
end
bill_range_attributes = bill_range_fields.keys

bill_public_attributes = Paperwork.attribute_names - @mongoid_attribute_names

equivalence_attributes = bill_public_attributes + @search_attribute_names
range_attributes_min = bill_range_attributes.map {|attribute| attribute + @range_modifier_min}
range_attributes_max = bill_range_attributes.map {|attribute| attribute + @range_modifier_max}

filtered_conditions = {}
equivalence_conditions = {}
range_conditions_min = {}
range_conditions_max = {}
conditions.each do |key, value|
next if value.nil?() || value == ""
if equivalence_attributes.include?(key)
equivalence_conditions[key] = value
elsif range_attributes_min.include?(key)
range_conditions_min[key.gsub(@range_modifier_min, "")] = value
elsif range_attributes_max.include?(key)
range_conditions_max[key.gsub(@range_modifier_max, "")] = value
end
end

return {equivalence_conditions: equivalence_conditions,\
range_conditions_min: range_conditions_min, range_conditions_max: range_conditions_max}
end

def search_for(conditions)
filtered_conditions = filter_conditions(conditions)

search = Sunspot.search(Paperwork) do
# FIX the equivalence conditions settings should be in a conf file
# search over all fields
if filtered_conditions[:equivalence_conditions].key?("q")
fulltext filtered_conditions[:equivalence_conditions]["q"] do
# boost_fields :tags => 3.0
# boost_fields :subject_areas => 2.9
# boost_fields :title => 2.5
# boost_fields :abstract => 2.0
end
filtered_conditions[:equivalence_conditions].delete("q")
end
# search over bill identifiers, both uid and short uid
if filtered_conditions[:equivalence_conditions].key?("bill_id")
text_fields do
any_of do
with(:uid, filtered_conditions[:equivalence_conditions]["bill_id"])
with(:short_uid, filtered_conditions[:equivalence_conditions]["bill_id"])
end
end
filtered_conditions[:equivalence_conditions].delete("bill_id")
end
#search over specific fields
text_fields do
all_of do
filtered_conditions[:equivalence_conditions].each do |key, value|
any_of do
value.split("|").each do |term|
with(key, term)
end
end
end
end
end

all_of do
#range_conditions_min might be asdf_min instead of asdf
filtered_conditions[:range_conditions_min].each do |key, value|
with(key).greater_than(value)
end
filtered_conditions[:range_conditions_max].each do |key, value|
with(key).less_than(value)
end
end

paginate page:conditions[:page], per_page:conditions[:per_page]
end
search
end

end
2 changes: 2 additions & 0 deletions app/helpers/billit/paperworks_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Billit::PaperworksHelper
end
14 changes: 8 additions & 6 deletions app/models/bill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ class Bill
include Mongoid::Document
include Mongoid::Timestamps

# include Billit::BillRepresenter

validates_presence_of :uid
validates_uniqueness_of :uid

before_save :standardize_tags, :set_law_link

has_many :paperworks, autosave: true, class_name: "Billit::Paperwork"
has_many :paperworks, autosave: true, class_name: "Paperwork"
# embeds_many :paperworks
has_many :priorities, autosave: true, class_name: "Billit::Priority"
has_many :reports, autosave: true, class_name: "Billit::Report"
has_many :documents, autosave: true, class_name: "Billit::Document"
has_many :directives, autosave: true, class_name: "Billit::Directive"
has_many :remarks, autosave: true, class_name: "Billit::Remark"
has_many :priorities, autosave: true, class_name: "Priority"
has_many :reports, autosave: true, class_name: "Report"
has_many :documents, autosave: true, class_name: "Document"
has_many :directives, autosave: true, class_name: "Directive"
has_many :remarks, autosave: true, class_name: "Remark"
embeds_many :revisions

field :uid, type: String
Expand Down
2 changes: 1 addition & 1 deletion app/models/billit/directive.rb → app/models/directive.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Billit::Directive
class Directive
include Mongoid::Document
include Mongoid::Timestamps

Expand Down
2 changes: 1 addition & 1 deletion app/models/billit/document.rb → app/models/document.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Billit::Document
class Document
include Mongoid::Document
include Mongoid::Timestamps

Expand Down
12 changes: 11 additions & 1 deletion app/models/billit/paperwork.rb → app/models/paperwork.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Billit::Paperwork
class Paperwork
# class Papperwork
include Mongoid::Document
include Mongoid::Timestamps

Expand All @@ -9,4 +10,13 @@ class Billit::Paperwork
field :description, :type => String
field :stage, :type => String
field :chamber, :type => String

include Sunspot::Mongoid2
searchable do
text :session
time :date
text :description
text :stage
text :chamber
end
end
2 changes: 1 addition & 1 deletion app/models/billit/priority.rb → app/models/priority.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Billit::Priority
class Priority
include Mongoid::Document
include Mongoid::Timestamps

Expand Down
2 changes: 1 addition & 1 deletion app/models/billit/remark.rb → app/models/remark.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Billit::Remark
class Remark
include Mongoid::Document
include Mongoid::Timestamps

Expand Down
2 changes: 1 addition & 1 deletion app/models/billit/report.rb → app/models/report.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Billit::Report
class Report
include Mongoid::Document
include Mongoid::Timestamps

Expand Down
File renamed without changes.
25 changes: 25 additions & 0 deletions app/views/billit/paperworks/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
= form_for @billit_paperwork do |f|
- if @billit_paperwork.errors.any?
#error_explanation
%h2= "#{pluralize(@billit_paperwork.errors.count, "error")} prohibited this billit_paperwork from being saved:"
%ul
- @billit_paperwork.errors.full_messages.each do |msg|
%li= msg

.field
= f.label :session
= f.text_field :session
.field
= f.label :date
= f.datetime_select :date
.field
= f.label :description
= f.text_field :description
.field
= f.label :stage
= f.text_field :stage
.field
= f.label :chamber
= f.text_field :chamber
.actions
= f.submit 'Save'
7 changes: 7 additions & 0 deletions app/views/billit/paperworks/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%h1 Editing billit_paperwork

= render 'form'

= link_to 'Show', @billit_paperwork
\|
= link_to 'Back', billit_paperworks_path
Loading

0 comments on commit f9600ba

Please sign in to comment.