Skip to content

Commit

Permalink
Merge pull request #29 from johnofsydney/adding-lobbyists
Browse files Browse the repository at this point in the history
Adding lobbyists - more turbo_frames, pagination
  • Loading branch information
johnofsydney authored Nov 9, 2024
2 parents d1c0218 + 258cdd7 commit cf2bcb3
Show file tree
Hide file tree
Showing 29 changed files with 1,114 additions and 325 deletions.
21 changes: 21 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,25 @@ button {
border-bottom: 2px solid #007bff;
/* Bootstrap primary color */
padding-bottom: 5px;
}

@media (max-width: 1000px) {
.mobile-only {
/* Add your mobile-specific styles here */
background-color: #f8f9fa;
/* Example: Light background color */
padding: 10px;
/* Example: Padding */
font-size: 14px;
/* Example: Smaller font size */
}

.mobile-display-none {
display: none;
}
}

.grey {
color: grey;
font-style: italic;
}
115 changes: 40 additions & 75 deletions app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
class GroupsController < ApplicationController
include Constants

before_action :set_group, only: %i[ show edit update destroy ]
before_action :set_page, only: %i[ index ]
before_action :authenticate_user!, only: %i[ new edit update destroy ]

layout -> { ApplicationLayout }

# GET /groups or /groups.json
def index
@groups = Rails.cache.fetch("groups_index_#{params[:page]}", expires_in: 12.hours) do

Expand All @@ -15,98 +16,62 @@ def index
render Groups::IndexView.new(groups: @groups, page: @page)
end

# GET /groups/1 or /groups/1.json
def show
render Groups::ShowView.new(group: @group, depth: 6)
render Groups::ShowView.new(group: @group, depth: Constants::MAX_SEARCH_DEPTH)
end

# GET /groups/new
def new
return unless current_user
def affiliated_groups
@group = Group.find(params[:group_id])
@page = params[:page].to_i

@group = Group.new
render Groups::AffiliatedGroups.new(group: @group)
end

# GET /groups/1/edit
def edit
return unless current_user
def money_summary
render Common::MoneySummary.new(entity: Group.find(params[:group_id]))
end

# POST /groups or /groups.json
def create
return unless current_user

@group = Group.new(group_params)

respond_to do |format|
if @group.save
new_membership_params = params.dig(:group, :memberships_attributes, :NEW_RECORD)
if new_membership_params
@group.memberships.create(new_membership_params.permit(:person_id, :title, :start_date, :end_date))
end
format.html { redirect_to group_url(@group), notice: "Group was successfully created." }
format.json { render :show, status: :created, location: @group }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @group.errors, status: :unprocessable_entity }
end
end
end
def group_people
@group = Group.find(params[:group_id])
@page = params[:page].to_i
@pages = (@group.people.count.to_f / page_size).ceil

# PATCH/PUT /groups/1 or /groups/1.json
def update
return unless current_user

respond_to do |format|
if @group.update(group_params)
new_membership_params = params.dig(:group, :memberships_attributes, :NEW_RECORD)
if new_membership_params
@group.memberships.create(new_membership_params.permit(:person_id, :title, :start_date, :end_date))
end
format.html { redirect_to group_url(@group), notice: "Group was successfully updated." }
format.json { render :show, status: :ok, location: @group }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @group.errors, status: :unprocessable_entity }
end
end
@people = @group.people
.order(:name)
.offset(@page * page_size)
.limit(page_size)

render Groups::PeopleTable.new(people: @people, exclude_group: @group, page: @page, pages: @pages)
end

# DELETE /groups/1 or /groups/1.json
def destroy
return unless current_user

@group.destroy
private
# Use callbacks to share common setup or constraints between actions.
def set_group
@group = Group.find(params[:id])
end

respond_to do |format|
format.html { redirect_to groups_url, notice: "Group was successfully destroyed." }
format.json { head :no_content }
end
def set_page
@page = (params[:page] || 0).to_i
end

private
# Use callbacks to share common setup or constraints between actions.
def set_group
@group = Group.find(params[:id])
end
# Only allow a list of trusted parameters through.
def group_params
params.require(:group).permit(:name, memberships_attributes: [:id, :title, :start_date, :end_date, :_destroy])
end

def set_page
@page = (params[:page] || 0).to_i
end
def update_group(group)
people_ids = group_params['people_ids'].select(&:present?).map(&:to_i)

# Only allow a list of trusted parameters through.
def group_params
params.require(:group).permit(:name, memberships_attributes: [:id, :title, :start_date, :end_date, :_destroy])
people_ids.each do |id|
person = Person.find(id)
person.groups << group unless person.groups.include?(group)
end

def update_group(group)
people_ids = group_params['people_ids'].select(&:present?).map(&:to_i)

people_ids.each do |id|
person = Person.find(id)
person.groups << group unless person.groups.include?(group)
end
group
end

group
end
def page_size
@page_size ||= Constants::PAGE_LIMIT
end
end
68 changes: 0 additions & 68 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,13 @@ class PeopleController < ApplicationController

layout -> { ApplicationLayout }

# GET /people or /people.json
def index
people = Person.order(:name)
render People::IndexView.new(people:)
end

# GET /people/1 or /people/1.json
def show
render People::ShowView.new(person: @person)

end

# GET /people/new
def new
return unless current_user
@person = Person.new
end

# GET /people/1/edit
def edit
return unless current_user
# phlex not working yet. form is hard. has nesting
# render People::EditView.new(person: @person)
end

# POST /people or /people.json
def create
return unless current_user

@person = Person.new(person_params)

respond_to do |format|
if @person.save
new_membership_params = params[:person][:memberships_attributes][:NEW_RECORD]
if new_membership_params
@person.memberships.create(new_membership_params.permit(:group_id, :title, :start_date, :end_date))
end
format.html { redirect_to person_url(@person), notice: "Person was successfully created." }
format.json { render :show, status: :created, location: @person }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @person.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /people/1 or /people/1.json
def update
return unless current_user

respond_to do |format|
if @person.update(person_params)
new_membership_params = params[:person][:memberships_attributes][:NEW_RECORD]
if new_membership_params
@person.memberships.create(new_membership_params.permit(:group_id, :title, :start_date, :end_date))
end
format.html { redirect_to person_url(@person), notice: "Person was successfully updated." }
format.json { render :show, status: :ok, location: @person }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @person.errors, status: :unprocessable_entity }
end
end
end

# DELETE /people/1 or /people/1.json
def destroy
return unless current_user

@person.destroy

respond_to do |format|
format.html { redirect_to people_url, notice: "Person was successfully destroyed." }
format.json { head :no_content }
end
end

private
Expand Down
37 changes: 0 additions & 37 deletions app/controllers/transfers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,12 @@ class TransfersController < ApplicationController
def index
transfers = Transfer.order(amount: :desc)
render Transfers::IndexView.new(transfers:)

end

def show
render Transfers::ShowView.new(transfer: @transfer)
end

def new
return unless current_user
end

def edit
return unless current_user
end

def create
return unless current_user
end

def update
return unless current_user
respond_to do |format|
if @transfer.update(transfer_params)
format.html { redirect_to transfer_url(@transfer), notice: "Transfer was successfully updated." }
format.json { render :show, status: :ok, location: @transfer }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @transfer.errors, status: :unprocessable_entity }
end
end
end

# DELETE /transfers/1 or /transfers/1.json
def destroy
return unless current_user
@transfer.destroy

respond_to do |format|
format.html { redirect_to people_url, notice: "Transfer was successfully destroyed." }
format.json { head :no_content }
end
end

private

def set_transfer
Expand Down
5 changes: 5 additions & 0 deletions app/models/concerns/constants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Constants
extend ActiveSupport::Concern
PAGE_LIMIT = 25
MAX_SEARCH_DEPTH = 4
end
11 changes: 6 additions & 5 deletions app/models/concerns/transfer_methods.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module TransferMethods
extend ActiveSupport::Concern
MAX_GROUP_SIZE_TO_FINISH = 15 # TODO: use or discard
MAX_SIZE_TO_ACCEPT_DONATIONS = 550 # TODO: use or discard

included do
# depth is the number of degrees of separation from the invoking node, 0 being the invoking node itself
Expand Down Expand Up @@ -89,9 +87,12 @@ def category_incoming_transfers
def category_outgoing_transfers
return Transfer.none unless self.is_category?

@category_outgoing_transfers ||= Transfer.where(giver_type: 'Group', giver_id: [self.groups.pluck(:id)])
.where.not(taker_id: [self.groups.pluck(:id)])
.or(Transfer.where(giver_type: 'Person', giver_id: [self.people.pluck(:id)]))
group_ids = self.groups.pluck(:id)
people_ids = self.people.pluck(:id)

@category_outgoing_transfers ||= Transfer.where(giver_type: 'Group', giver_id: group_ids)
.where.not(taker_id: group_ids)
.or(Transfer.where(giver_type: 'Person', giver_id: people_ids))
end

private
Expand Down
45 changes: 45 additions & 0 deletions app/services/file_ingestor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,51 @@ def general_upload(file)
end
end

def lobbyists_upload(file)
lobbyists = Group.find_or_create_by(name: 'Lobbyists', category: true)

csv = CSV.read(file, headers: true)
csv.each do |row|
group = RecordGroup.call(row['company'])
person = RecordPerson.call(row['person'])
title = if row['title'].present?
CapitalizeNames.capitalize(row['title'].strip)
.gsub(/\bCEO\b/i) { |word| word.titleize }
end

evidence = 'https://lobbyists.ag.gov.au/register'

# Create a membership for the named person in the named group
# the membership may not exist, if so, we need to create it
membership = Membership.find_or_create_by(
member: person,
group: group
)
# create position for each row, with unique dates and title
position = Position.find_or_create_by(membership:, title:)

membership.update!(evidence:)
position.update!(evidence:)

print "m"

# Create a membership for the named person and group inthe lobbyists category
membership = Membership.find_or_create_by(
member: person,
group: lobbyists
)
membership = Membership.find_or_create_by(
member: group,
group: lobbyists
)

print "l"

rescue => e
p "Lobbyist Upload | Error: #{e} | row#{row.inspect}"
end
end

def affiliations_upload(file)
csv = CSV.read(file, headers: true)
csv.each do |row|
Expand Down
Loading

0 comments on commit cf2bcb3

Please sign in to comment.