Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add User Billing Page for Updating Billing Information #46

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ on:
jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Start MongoDB
uses: supercharge/mongodb-github-action@v1

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ gem "turbo-rails", "1.1.1"
gem "stimulus-rails", "1.0.4"
gem "jbuilder", "2.11.5"
gem "puma", "5.6.4"
gem "mongoid", "9.0.0"
gem "bootsnap", "1.12.0", require: false

group :development, :test do
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ GEM
sassc (>= 2.0.0)
bootstrap-will_paginate (1.0.0)
will_paginate
bson (5.0.0)
builder (3.2.4)
capybara (3.37.1)
addressable
Expand Down Expand Up @@ -183,6 +184,12 @@ GEM
builder
minitest (>= 5.0)
ruby-progressbar
mongo (2.20.0)
bson (>= 4.14.1, < 6.0.0)
mongoid (9.0.0)
activemodel (>= 5.1, < 7.2, != 7.0.0)
concurrent-ruby (>= 1.0.5, < 2.0)
mongo (>= 2.18.0, < 3.0.0)
msgpack (1.6.0)
nenv (0.3.0)
net-imap (0.3.4)
Expand Down Expand Up @@ -331,6 +338,7 @@ DEPENDENCIES
jbuilder (= 2.11.5)
minitest (= 5.15.0)
minitest-reporters (= 1.5.0)
mongoid (= 9.0.0)
pg (= 1.3.5)
puma (= 5.6.4)
rails (= 7.0.4)
Expand Down
21 changes: 20 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,27 @@ def followers
render 'show_follow', status: :unprocessable_entity
end

private
def billing
@user = User.find(params[:id])
@billing_address = UserBilling.find_or_initialize_by(user_id: @user.id)
end

def update_billing
@user = User.find(params[:id])
@billing_address = UserBilling.find_or_initialize_by(user_id: @user.id)
if @billing_address.update(billing_params)
flash[:success] = "Billing address updated"
redirect_to billing_user_path(@user)
else
render 'billing', status: :unprocessable_entity
end
end

private
def billing_params
params.require(:billing).permit(:address, :city, :state, :zip_code)
end

def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
remember_digest
end

def billing_address
UserBilling.find_by(user_id: self.id)
end

# Returns a session token to prevent session hijacking.
# We reuse the remember digest for convenience.
def session_token
Expand All @@ -57,7 +61,7 @@
end

# Activates an account.
def activate

Check warning on line 64 in app/models/user.rb

View workflow job for this annotation

GitHub Actions / appmap-annotations

Data update performed in GET or HEAD request

Data update performed in HTTP request GET /account_activations/{id}/edit: UPDATE "users" SET "updated_at" = ?, "activated" = ? WHERE "users"."id" = ?

Check warning on line 64 in app/models/user.rb

View workflow job for this annotation

GitHub Actions / appmap-annotations

Data update performed in GET or HEAD request

Data update performed in HTTP request GET /account_activations/{id}/edit: UPDATE "users" SET "updated_at" = ?, "activated_at" = ? WHERE "users"."id" = ?
update_attribute(:activated, true)
update_attribute(:activated_at, Time.zone.now)
end
Expand Down
13 changes: 13 additions & 0 deletions app/models/user_billing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class UserBilling
include Mongoid::Document

field :user_id, type: Integer
field :address, type: String
field :city, type: String
field :state, type: String
field :zip_code, type: String

index({ user_id: 1 }, { unique: true, name: "user_id_index" })

validates_presence_of :user_id, :address, :city, :state, :zip_code
end
1 change: 1 addition & 0 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<ul id="dropdown-menu" class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
<li><%= link_to "Billing", billing_user_path(current_user) %></li>
<li class="divider"></li>
<li>
<%= link_to "Log out", logout_path,
Expand Down
26 changes: 26 additions & 0 deletions app/views/users/billing.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<% provide(:title, "Billing") %>
<h1>Manage Billing Settings</h1>

<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_with(url: billing_user_path(@user), local: true, method: :patch) do |f| %>
<%= f.fields_for :billing, @billing_address do |billing_fields| %>

<%= billing_fields.label :address %>
<%= billing_fields.text_field :address, value: @billing_address.address, class: 'form-control' %>

<%= billing_fields.label :city %>
<%= billing_fields.text_field :city, value: @billing_address.city, class: 'form-control' %>

<%= billing_fields.label :state %>
<%= billing_fields.text_field :state, value: @billing_address.state, class: 'form-control' %>

<%= billing_fields.label :zip_code %>
<%= billing_fields.text_field :zip_code, value: @billing_address.zip_code, class: 'form-control' %>

<% end %>

<%= f.submit "Save Billing Info", class: "btn btn-primary" %>
<% end %>
</div>
</div>
1 change: 1 addition & 0 deletions appmap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ name: sample_rails_app
packages:
- path: app
- path: lib
- gem: mongoid
language: ruby
appmap_dir: tmp/appmap
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module SampleApp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 7.0

Mongoid.load!(Rails.root.join("config", "mongoid.yml"))

# Configuration for the application, engines, and railties goes here.
#
Expand Down
25 changes: 25 additions & 0 deletions config/mongoid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# config/mongoid.yml
development:
clients:
default:
database: sample_rails_app_dev
hosts:
- localhost:27017
options:
server_selection_timeout: 5
test:
clients:
default:
database: sample_rails_app_test
hosts:
- localhost:27017
options:
server_selection_timeout: 5
production:
clients:
default:
database: sample_rails_app
hosts:
- localhost:27017
options:
server_selection_timeout: 5
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
resources :users do
member do
get :following, :followers
get :billing
patch 'billing', to: 'users#update_billing'
end
end
resources :account_activations, only: [:edit]
Expand Down
9 changes: 8 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@ services:
ports:
- "3000:3000"
volumes:
- ./tmp/appmap:/app/tmp/appmap
- ./tmp/appmap:/app/tmp/appmap

mongodb:
image: mongo:latest
ports:
- "27017:27017"
volumes:
- ./tmp/mongodb:/data/db
53 changes: 53 additions & 0 deletions test/integration/billing_address_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require "test_helper"

class BillingAddressTest < ActionDispatch::IntegrationTest
def setup
@michael = users(:michael)
@archer = users(:archer)
Mongoid.raise_not_found_error = false
end

test "create new billing address" do
user = @michael
UserBilling.find_by(user_id: user.id)&.destroy
get billing_user_path(user)
assert_template 'users/billing'

patch billing_user_path(user), params: { billing: { address: "123 Main Street", city: "Sample City", state: "SC", zip_code: "12345" } }

assert_redirected_to billing_user_path(user)
follow_redirect!
assert_match "Billing address updated", response.body
user.reload
user_billing = UserBilling.find_by(user_id: user.id)
assert_not_nil user_billing
assert_equal "123 Main Street", user_billing.address
assert_equal "Sample City", user_billing.city
assert_equal "SC", user_billing.state
assert_equal "12345", user_billing.zip_code
end

test "update existing billing address" do
user = @archer
UserBilling.find_by(user_id: user.id)&.destroy
UserBilling.create(user_id: user.id, address: "Initial Address", city: "Initial City", state: "Initial State", zip_code: "00000")

get billing_user_path(user)
assert_template 'users/billing'

patch billing_user_path(user), params: { billing: { address: "456 Another St", city: "Updated City", state: "UC", zip_code: "67890" } }

assert_redirected_to billing_user_path(user)
follow_redirect!
assert_match "Billing address updated", response.body

# Verify the attributes are updated
user.reload
user_billing = UserBilling.find_by(user_id: user.id)
assert_not_nil user_billing
assert_equal "456 Another St", user_billing.address
assert_equal "Updated City", user_billing.city
assert_equal "UC", user_billing.state
assert_equal "67890", user_billing.zip_code
end
end
Loading