Skip to content

Commit

Permalink
Linked up reports and users
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerldixon committed Jul 22, 2016
1 parent bc4390f commit 8c43508
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ pickle-email-*.html

# Ignore Byebug command history file.
.byebug_history

# Ignore erd.pdf
erd.pdf
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ gem 'pg', '~> 0.18.4'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'browserify-rails', '~> 3.1.0'
gem 'jquery-rails', '~> 4.1.1'
gem "select2-rails", git: 'https://github.com/argerim/select2-rails'

# Configuration
Expand Down Expand Up @@ -40,6 +41,10 @@ group :development do
gem 'capistrano-rvm', '~> 0.1', require: false
gem 'capistrano-passenger', '~> 0.2.0', require: false
gem 'capistrano-npm', '~> 1.0.2'

# ERD
gem 'ruby-graphviz', '~> 1.2.2'
gem 'rails-erd', '~> 1.4.7'
end

# Debugging
Expand Down
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ GEM
capistrano-rvm (0.1.2)
capistrano (~> 3.0)
sshkit (~> 1.2)
choice (0.2.0)
concurrent-ruby (1.0.2)
debug_inspector (0.0.2)
devise (4.2.0)
Expand Down Expand Up @@ -108,6 +109,10 @@ GEM
globalid (0.3.6)
activesupport (>= 4.1.0)
i18n (0.7.0)
jquery-rails (4.1.1)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.3)
loofah (2.0.3)
nokogiri (>= 1.5.9)
Expand Down Expand Up @@ -147,6 +152,11 @@ GEM
activesupport (>= 4.2.0.beta, < 5.0)
nokogiri (~> 1.6.0)
rails-deprecated_sanitizer (>= 1.0.1)
rails-erd (1.4.7)
activerecord (>= 3.2)
activesupport (>= 3.2)
choice (~> 0.2.0)
ruby-graphviz (~> 1.2)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
railties (4.2.6)
Expand All @@ -158,6 +168,7 @@ GEM
redcarpet (3.3.4)
responders (2.2.0)
railties (>= 4.2.0, < 5.1)
ruby-graphviz (1.2.2)
sass (3.4.22)
sass-rails (5.0.4)
railties (>= 4.0.0, < 5.0)
Expand Down Expand Up @@ -212,9 +223,12 @@ DEPENDENCIES
exception_notification (~> 4.1.4)
factory_girl_rails (~> 4.7.0)
faker (~> 1.6.5)
jquery-rails (~> 4.1.1)
pg (~> 0.18.4)
rails (= 4.2.6)
rails-erd (~> 1.4.7)
redcarpet (~> 3.3.4)
ruby-graphviz (~> 1.2.2)
sass-rails (~> 5.0)
select2-rails!
slack-notifier (~> 1.5.1)
Expand Down
9 changes: 9 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require select2

var app = require("app")
app.start()

$(document).ready(function(){
$(".select2").select2({
tags: true
})
})

1 change: 1 addition & 0 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def edit

def create
@report = Report.new(report_params)
@report.user = current_user

if @report.save
render json: @report, location: reports_path
Expand Down
17 changes: 17 additions & 0 deletions app/models/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,20 @@
# data :json
# created_at :datetime not null
# updated_at :datetime not null
# user_id :integer
#
# Indexes
#
# index_reports_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_c7699d537d (user_id => users.id)
#

class Report < ActiveRecord::Base
belongs_to :user

def self.search(params)
if params
query = self
Expand All @@ -30,6 +41,12 @@ def self.search(params)

query = query.where(created_at: from_date..to_date)
end

if params[:agencies].present?
agencies = params[:agencies].map(&:to_i)
#query = query.where('user.agency.id' IS ANY OF agencies)
end

else
self.all
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ class User < ActiveRecord::Base
belongs_to :role
belongs_to :agency
has_and_belongs_to_many :expertises
has_many :reports

validates :role_id, :agency_id, :first_name, :last_name, :email, :country, presence: true

def full_name
[first_name, last_name].join(" ")
end
end
10 changes: 5 additions & 5 deletions app/views/reports/search.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
<%#<%= text_field_tag :user_name, params[:user_name] %>%>
<%#</div>%>

<%#<div>%>
<%#<%= label_tag :agencies %>%>
<%#<%= select_tag "agencies", options_from_collection_for_select(Agency.all, "id", "name"), multiple: true, class: 'select2' %>%>
<%#</div>%>
<div>
<%= label_tag :agencies %>
<%= select_tag "agencies", options_from_collection_for_select(Agency.all, "id", "name"), multiple: true, class: 'select2' %>
</div>

<div>
<%= label_tag :from_date, 'From' %>
<%= date_select :from_date, params[:from_date] %>
<%= date_select :from_date, params[:from_date], default: Date.new(2016,1,1) %>

<%= label_tag :to_date, 'To' %>
<%= date_select :to_date, params[:to_date] %>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20160722141525_add_user_ref_to_reports.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUserRefToReports < ActiveRecord::Migration
def change
add_reference :reports, :user, index: true, foreign_key: true
end
end
6 changes: 5 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160719162209) do
ActiveRecord::Schema.define(version: 20160722141525) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -44,8 +44,11 @@
t.json "data"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end

add_index "reports", ["user_id"], name: "index_reports_on_user_id", using: :btree

create_table "roles", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
Expand Down Expand Up @@ -83,6 +86,7 @@
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
add_index "users", ["role_id"], name: "index_users_on_role_id", using: :btree

add_foreign_key "reports", "users"
add_foreign_key "users", "agencies"
add_foreign_key "users", "roles"
end
Binary file added erd.pdf
Binary file not shown.

0 comments on commit 8c43508

Please sign in to comment.