Skip to content

Commit

Permalink
inertia - new bill creator layout
Browse files Browse the repository at this point in the history
  • Loading branch information
dcordz committed Dec 8, 2024
1 parent 3cb080e commit 817ae29
Show file tree
Hide file tree
Showing 77 changed files with 2,725 additions and 2,236 deletions.
33 changes: 17 additions & 16 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ GEM
httpclient (2.8.3)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
inertia_rails (3.4.0)
inertia_rails (3.5.0)
railties (>= 6)
io-console (0.7.2)
io-console (0.8.0)
irb (1.14.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
Expand All @@ -192,7 +192,7 @@ GEM
base64
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
logger (1.6.1)
logger (1.6.2)
lograge (0.14.0)
actionpack (>= 4)
activesupport (>= 4)
Expand All @@ -211,7 +211,7 @@ GEM
method_source (1.1.0)
mini_mime (1.1.5)
mini_portile2 (2.8.7)
minitest (5.25.1)
minitest (5.25.4)
msgpack (1.7.3)
multi_json (1.15.0)
mutex_m (0.2.0)
Expand All @@ -229,13 +229,13 @@ GEM
netrc (0.11.0)
newrelic_rpm (9.15.0)
nio4r (2.7.4)
nokogiri (1.16.7-aarch64-linux)
nokogiri (1.16.8-aarch64-linux)
racc (~> 1.4)
nokogiri (1.16.7-arm64-darwin)
nokogiri (1.16.8-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-darwin)
nokogiri (1.16.8-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-linux)
nokogiri (1.16.8-x86_64-linux)
racc (~> 1.4)
openssl (3.2.0)
openssl-signature_algorithm (1.3.0)
Expand All @@ -249,7 +249,8 @@ GEM
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
psych (5.2.0)
psych (5.2.1)
date
stringio
public_suffix (6.0.1)
puma (6.4.3)
Expand All @@ -262,7 +263,7 @@ GEM
rack (>= 3.0.0)
rack-test (2.1.0)
rack (>= 1.3)
rackup (2.2.0)
rackup (2.2.1)
rack (>= 3)
rails (7.2.2)
actioncable (= 7.2.2)
Expand All @@ -286,9 +287,9 @@ GEM
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.6.0)
rails-html-sanitizer (1.6.1)
loofah (~> 2.21)
nokogiri (~> 1.14)
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
railties (7.2.2)
actionpack (= 7.2.2)
activesupport (= 7.2.2)
Expand All @@ -302,10 +303,10 @@ GEM
rbi (0.2.1)
prism (~> 1.0)
sorbet-runtime (>= 0.5.9204)
rdoc (6.7.0)
rdoc (6.8.1)
psych (>= 4.0.0)
regexp_parser (2.9.2)
reline (0.5.11)
reline (0.5.12)
io-console (~> 0.5)
representable (3.2.0)
declarative (< 0.1.0)
Expand Down Expand Up @@ -371,7 +372,7 @@ GEM
rubyzip (2.3.2)
safety_net_attestation (0.4.0)
jwt (~> 2.0)
securerandom (0.3.1)
securerandom (0.4.0)
selenium-webdriver (4.26.0)
base64 (~> 0.2)
logger (~> 1.4)
Expand Down Expand Up @@ -453,7 +454,7 @@ GEM
uber (0.1.0)
unicode-display_width (2.6.0)
uri (1.0.1)
useragent (0.16.10)
useragent (0.16.11)
vite_rails (3.0.19)
railties (>= 5.1, < 9)
vite_ruby (~> 3.0, >= 3.2.2)
Expand Down
99 changes: 71 additions & 28 deletions app/controllers/bills_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ def new
l.to_builder.attributes!
end,
legislatorVotes: [],
positions: [],
organizations: [],
tabKey: params[:tabKey]
})
end

# GET /bills/1/edit
def edit
return if @bill.blank?
redirect_to new_bill_path if @bill.blank?

T.unsafe(self).render_bill_creator({
bills: (current_sway_locale&.bills || []).map { |b| b.to_builder.attributes! },
Expand All @@ -52,38 +52,55 @@ def edit
l.to_builder.attributes!
end,
legislatorVotes: @bill.legislator_votes.map { |lv| lv.to_builder.attributes! },
positions: @bill.organization_bill_positions.map do |obp|
obp.to_builder.attributes!
end,
organizations: @bill.organizations.map do |organization|
organization.to_builder(with_positions: true).attributes!
end,
tabKey: params[:tabKey]
})
end

# POST /bills or /bills.json
def create
b = Bill.find_or_create_by!(
**bill_params,
sway_locale: current_sway_locale
b = Bill.find_or_initialize_by(
external_id: bill_params[:external_id],
sway_locale_id: bill_params[:sway_locale_id] || current_sway_locale.presence&.id
)

create_vote(b)
b.assign_attributes(**bill_params.except(*vote_params))

render json: b.to_builder.attributes!, status: :ok
b.legislator = Legislator.find(bill_params[:legislator_id])

if b.save
create_vote(b)

redirect_to edit_bill_path(b.id, {saved: "Bill Created", event_key: "legislator_votes"}), inertia: {errors: {}}
else
redirect_to new_bill_path({event_key: "bill"}), inertia: {
errors: b.errors
}
end
rescue Exception => e # rubocop:disable Lint/RescueException
Rails.logger.error(e)
redirect_to new_bill_path({event_key: "bill"}), inertia: {errors: {external_id: e}}
end

# PATCH/PUT /bills/1 or /bills/1.json
def update
render json: {success: false, message: @bill.errors.join(", ")}, status: :ok if @bill.blank?
if @bill.blank?
return redirect_to new_bill_path({event_key: "bill"})
end

current_audio_path = @bill.audio_bucket_path.freeze
if @bill.update(bill_params)
if @bill.update(bill_params.except(*vote_params))
remove_audio(current_audio_path)

create_vote(@bill)

render json: @bill.to_builder.attributes!, status: :ok
redirect_to edit_bill_path(@bill.id, {saved: "Bill Updated", event_key: "legislator_votes"})
else
render json: {success: false, message: @bill.errors.join(", ")}, status: :ok
redirect_to edit_bill_path(@bill.id, {event_key: "bill"}), inertia: {
errors: b.errors
}
end
end

Expand All @@ -96,7 +113,6 @@ def destroy

private

# Use callbacks to share common setup or constraints between actions.
def set_bill
@bill = Bill.includes(:legislator_votes, :organization_bill_positions, :legislator, :sway_locale).find(params[:id])
end
Expand All @@ -111,23 +127,16 @@ def remove_audio(audio_path)
end

def create_vote(b)
return unless vote_params[:house_roll_call_vote_number] || vote_params[:senate_roll_call_vote_number]
return unless bill_params[:house_roll_call_vote_number] || bill_params[:senate_roll_call_vote_number]

Vote.find_or_create_by!(bill_id: b.id,
house_roll_call_vote_number: vote_params[:house_roll_call_vote_number],
senate_roll_call_vote_number: vote_params[:senate_roll_call_vote_number])
end

def vote_params
params.permit(
:house_roll_call_vote_number,
:senate_roll_call_vote_number
)
house_roll_call_vote_number: bill_params[:house_roll_call_vote_number],
senate_roll_call_vote_number: bill_params[:senate_roll_call_vote_number])
end

# Only allow a list of trusted parameters through.
def bill_params
params.require(:bill).permit(
params.transform_keys(&:underscore).permit(
:external_id,
:external_version,
:title,
Expand All @@ -136,16 +145,50 @@ def bill_params
:introduced_date_time_utc,
:house_vote_date_time_utc,
:senate_vote_date_time_utc,
:chamber,
:category,
:level,
:summary,
:status,
:active,
:audio_bucket_path,
:audio_by_line,
:sway_locale_id,
:legislator_id,
:sway_locale_id
:house_roll_call_vote_number,
:senate_roll_call_vote_number,
:audio_bucket_path,
:audio_by_line
)
end

def vote_params
[
:house_roll_call_vote_number,
:senate_roll_call_vote_number
]
end

def legislator_vote_params
params.require(:legislator_votes).map do |p|
p.transform_keys(&:underscore).permit(:legislator_id, :bill_id, :support)
end
end

def organizations_params
params.require(:organizations).map do |p|
p.transform_keys(&:underscore).permit(
:id, :sway_locale_id, :name, :icon_path, positions: [:id, :bill_id, :summary, :support]
)
end
end

def params
super.transform_keys(&:underscore)
end

def remove_icon(organization, current_icon_path)
return unless organization.icon_path != current_icon_path

delete_file(bucket_name: SwayGoogleCloudStorage::BUCKETS[:ASSETS], file_name: current_icon_path)
end
end
39 changes: 25 additions & 14 deletions app/controllers/legislator_votes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,45 @@
# typed: true

class LegislatorVotesController < ApplicationController
before_action :verify_is_admin, only: %i[create update]
before_action :verify_is_admin, only: %i[create]
before_action :set_bill, only: %i[show create]

def index
render json: current_user&.legislators(T.cast(current_sway_locale, SwayLocale)), status: :ok
end

def show
render json: LegislatorVote.where(bill_id: legislator_votes_params[:bill_id]).map { |lv|
lv.to_builder.attributes!
}, status: :ok
render json: LegislatorVote.where(bill: @bill).map { |lv| lv.to_builder.attributes! }, status: :ok
end

def create
LegislatorVote.where(bill_id: legislator_votes_params[:votes].first[:bill_id]).destroy_all

render json: LegislatorVote.insert_all!(legislator_votes_params[:votes]), status: :ok # rubocop:disable Rails/SkipsModelValidations
end

def update
LegislatorVote.where(bill: @bill).destroy_all

begin
legislator_votes_params[:legislator_votes].each do |param|
LegislatorVote.create!({
bill_id: @bill.id,
legislator_id: param[:legislator_id].to_i,
support: param[:support]
})
end
rescue Exception => e # rubocop:disable Lint/RescueException
Rails.logger.error(e)
redirect_to edit_bill_path(@bill.id, {event_key: "legislator_votes"}), inertia: {
errors: {legislator_votes: e}
}
else
redirect_to edit_bill_path(@bill.id, {saved: "Legislator Votes Saved", event_key: "organizations"})
end
end

private

def legislator_votes_params
params.require(:legislator_vote).permit(votes: %i[bill_id legislator_id support])
def set_bill
@bill = Bill.includes(:legislator_votes, :sway_locale).find(legislator_votes_params[:bill_id])
end

def legislator_vote_params
params.require(:legislator_vote).permit(:bill_id, :legislator_id, :support)
def legislator_votes_params
params.permit(:bill_id, legislator_votes: %i[legislator_id support])
end
end
Loading

0 comments on commit 817ae29

Please sign in to comment.