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

HER- 66 creating address controller #58

Merged
merged 8 commits into from
Jan 14, 2025

Conversation

dewi-anggraini
Copy link
Collaborator

@dewi-anggraini dewi-anggraini commented Jan 13, 2025

HER-66 Creating Address Controller

Issue

https://raquelanaroman.atlassian.net/browse/HER-66

Description
Enhance the address management system to allow users to create and edit addresses for themselves and their organization in their profile.

Acceptance Criteria
Users can create and edit addresses in their profiles for themselves and their organization.
Implementation
Generate a migration to add a save_to_user attribute to the Address model.

rails generate migration AddSaveToUserToAddresses save_to_user:boolean
rails db:migrate

Ensure the Address model includes the save_to_user attribute.
attribute :save_to_user, :boolean, default: false

Generate the Addresses controller.
rails generate controller Addresses

Set up routes to nest addresses under users and organizations.
resources :users do
resources :addresses, only: [ :create, :update, :destroy ]
end

Implement action for creating, updating, and destroying addresses.
class Api::V1::AddressesController < ApplicationController
before_action :set_addressable

def create
@address = @addressable.addresses.build(address_params)
if @address.save
render json: @address, status: :created
else
render json: @address.errors, status: :unprocessable_entity
end
end

def update
@address = @addressable.addresses.find(params[:id])
if @address.update(address_params)
render json: @address, status: :ok
else
render json: @address.errors, status: :unprocessable_entity
end
end

def destroy
@address = @addressable.addresses.find(params[:id])
@address.destroy
head :no_content
end

private

def set_addressable
@Addressable = if params[:user_id]
User.find(params[:user_id])
elsif params[:organization_id]
Organization.find(params[:organization_id])
end
end

def address_params
params.require(:address).permit(:street_address, :city, :state, :postal_code, :save_to_user)
end
end
Linked work items

#HER-71

Changes

  1. Generate a migration to add a save_to_user attribute to the Address model.
  2. Updated Address model
  3. Generate address controller
  4. Set up routes to nest addresses under users
  5. Implemented the action (create, update, destroy) in the addresses controller

Review Checklist

  • [x ] I have documented my code with code comments.

Copy link
Collaborator

@trca831 trca831 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dewi-anggraini dewi-anggraini merged commit 010336a into main Jan 14, 2025
3 checks passed
@dewi-anggraini dewi-anggraini deleted the HER-66-creating-address-controller branch January 14, 2025 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants