-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from johnofsydney/june-new-version
June new version
- Loading branch information
Showing
23 changed files
with
255 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,68 @@ | ||
class LazyLoadGroupsController < ApplicationController | ||
def show | ||
@group = Group.find(params[:id]) | ||
|
||
# call the methods to save the instance variables | ||
@giver_colors = colors(transfers_as_giver, giver: true) | ||
@transfers_as_giver_by_year = group_by_year(transfers_as_giver) | ||
@transfers_as_giver_by_name = group_by_name(transfers_as_giver, giver: true) | ||
@taker_colors = colors(transfers_as_taker) | ||
@transfers_as_taker_by_year = group_by_year(transfers_as_taker) | ||
@transfers_as_taker_by_name = group_by_name(transfers_as_taker) | ||
end | ||
|
||
def transfers_as_giver | ||
@transfers_as_giver ||= Transfer.where(giver_type: @group.class.name, giver_id: @group.id) | ||
end | ||
|
||
def transfers_as_taker | ||
@transfers_as_taker ||= Transfer.where(taker_type: @group.class.name, taker_id: @group.id) | ||
end | ||
|
||
def group_by_year(query) | ||
query.group(:effective_date) | ||
.sum(:amount) | ||
.sort_by{|k, _v| k } | ||
.to_h | ||
.transform_keys{ |key| key.year } | ||
end | ||
|
||
def group_by_name(query, giver: false) | ||
if giver | ||
all_the_groups = query.group(:taker_id, :taker_type) | ||
.sum(:amount) | ||
.transform_keys{ |key| key[1].constantize.find(key[0]).name } | ||
.sort_by{|k, v| v} | ||
else | ||
all_the_groups = query.group(:giver_id, :giver_type) | ||
.sum(:amount) | ||
.transform_keys{ |key| key[1].constantize.find(key[0]).name } | ||
.sort_by{|k, v| v} | ||
end | ||
|
||
|
||
last_five = all_the_groups.last(5) | ||
sum_others = (all_the_groups - last_five).map{|a| a.last}.sum | ||
|
||
if sum_others.zero? | ||
last_five.to_h | ||
else | ||
last_five.to_h.merge('Others' => sum_others).sort_by { |_k, value| value } | ||
end | ||
end | ||
|
||
def colors(query, giver: false) | ||
if giver | ||
query.group(:taker_id, :taker_type) | ||
.sum(:amount) | ||
.transform_keys{ |key| key[1].constantize.find(key[0]).name } | ||
.map{|name, v| "#" + Digest::MD5.hexdigest(name)[0..5]} | ||
else | ||
query.group(:giver_id, :giver_type) | ||
.sum(:amount) | ||
.transform_keys{ |key| key[1].constantize.find(key[0]).name } | ||
.map{|name, v| "#" + Digest::MD5.hexdigest(name)[0..5]} | ||
end | ||
|
||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
class RecordGroup | ||
|
||
|
||
attr_reader :name | ||
|
||
def initialize(name) | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
class RecordPersonOrGroup | ||
def self.call(name) | ||
new(name).call | ||
end | ||
|
||
def call | ||
return nil unless name | ||
|
||
if person_or_group == 'person' | ||
RecordPerson.call(first_name_last_name) | ||
elsif person_or_group == 'group' | ||
RecordGroup.call(name) | ||
elsif person_or_group == 'couple' | ||
RecordGroup.call(name) | ||
# TODO: create memberships for each person in the couple | ||
else | ||
RecordGroup.call(name) | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :name | ||
|
||
def initialize(name) | ||
@name = name.strip | ||
end | ||
|
||
def person_or_group | ||
regex_for_3_or_4_capitals = /HCF|INPEX|CMAX|SDA|ONA|SPP|ACCI|ACTU/i | ||
regex_for_company_words_1 = /Corporation|Transport|Tax Aid|Outcomes|Lifestyle/i | ||
regex_for_company_words_2 = /business|technology|shopping|toyota|bank|promotions|publications/i | ||
regex_for_company_words_3 = /institute|horticultural|cleaning|technologies|centre/i | ||
regex_for_company_words_4 = /Services|investments|entertainment|Insurance|Commerce/i | ||
regex_for_company_words_5 = /Public|affairs|nimbin hemp|company|workpac|wren oil/i | ||
regex_for_company_words_6 = /plumbing|division|federal|office|advisory|deloitte touche/i | ||
regex_for_company_words_7 = /company|events|commerce|webdrill|private|restaurant/i | ||
regex_for_company_words_8 = /enterprise|lendlease|party|healthcare|agency|team|lawyers/i | ||
regex_for_company_words_9 = /national/i | ||
|
||
return 'group' if name.match?(regex_for_3_or_4_capitals) # Check for acronyms | ||
return 'group' if name.match?(regex_for_company_words_1) # Check for company names | ||
return 'group' if name.match?(regex_for_company_words_2) # Check for company names | ||
return 'group' if name.match?(regex_for_company_words_3) # Check for company names | ||
return 'group' if name.match?(regex_for_company_words_4) # Check for company names | ||
return 'group' if name.match?(regex_for_company_words_5) # Check for company names | ||
return 'group' if name.match?(regex_for_company_words_6) # Check for company names | ||
return 'group' if name.match?(regex_for_company_words_7) # Check for company names | ||
return 'group' if name.match?(regex_for_company_words_8) # Check for company names | ||
return 'group' if name.match?(regex_for_company_words_9) # Check for company names | ||
|
||
|
||
return 'group' if name.match?(/(PricewaterhouseCoopers|MSD)/) | ||
return 'group' if name.match?(/Democratic Labour Party/i) | ||
return 'group' if name.match?(/One Nation/i) | ||
return 'group' if name.match?(/Kim For Canberra/i) | ||
return 'group' if name.match?(/Get Up|Getup/i) | ||
return 'group' if name.match?(/ALP-|ALP -|Alp Bruce Fea/i) | ||
return 'group' if name.match?(/\bGrn\b/i) | ||
return 'group' if name.match?(/\bKap\b/i) | ||
return 'group' if name.match?(/\bWa-Alp\b/i) | ||
return 'group' if name.match?(/ACP-VIC/i) | ||
return 'group' if name.match?(/\bThe Nationals\b/i) | ||
return 'group' if name.match?(/Independents/i) | ||
return 'group' if name.match?(/Develco|Ecovis Clark Jacobs|Rapidplas|Rendition Homes/i) | ||
return 'person' if name.match?(/(?:MP|OAM|AO)$/) # Check for individuals with MP or OAM | ||
return 'person' if name.match?(/\bMP\b|\bDr\b/) # Check for individuals with MP or OAM | ||
return 'group' if name.match?(/(limited|incorporated|ltd|government|associat|management|group|trust)/i) # Check for company names | ||
return 'group' if name.match?(/(australia|management|capital|windfarm|engineering|energy)/i) # Check for company names | ||
return 'group' if name.match?(/(guild|foundation|trust|retail|council|union|club|alliance)/i) # Check for company names | ||
return 'group' if name.match?(/(nsw|queensland|state|tasmania|south|northern|territory|western)/i) # Check for states names | ||
return 'group' if name.match?(/(n\.s\.w|qld|s\.a\.|n\.t\.|w\.a\.)/i) # Check for states abbreviations | ||
return 'group' if name.match?(/( pl$|t\/as|trading as| p\/l)/i) # Check for company endings | ||
return 'goup' if name.match?(/&|\(/) # Check for entries with ampersands (considered as companies) | ||
return 'goup' if name.match?(/\d/) # Check for entries with numbers (considered as companies) | ||
return 'couple' if name.match(/ and /) # Check for couples | ||
return 'person' if name.match?(/^[A-Z][a-z]+, [A-Z][a-z]+$/) # Check for names in the format "Lastname, Firstname" | ||
|
||
'person' # default | ||
end | ||
|
||
def first_name_last_name | ||
# handle last_name, first_name if in that format | ||
name.include?(',') ? name.split(',').reverse.join(' ') : name | ||
end | ||
end | ||
|
||
|
This file contains 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
Oops, something went wrong.