Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Address table [IN PROGRESS]#39
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
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
6baae1be57e4519b1846d15c635a986f2ffd6a1325File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Place all the behaviors and hooks related to the matching controller here. | ||
| # All this logic will automatically be available in application.js. | ||
| # You can use CoffeeScript in this file: http://coffeescript.org/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // Place all the styles related to the Addresses controller here. | ||
| // They will automatically be included in application.css. | ||
| // You can use Sass (SCSS) here: http://sass-lang.com/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| body { | ||
| background-color: #fff; | ||
| color: #333; | ||
| margin: 33px; | ||
| font-family: verdana, arial, helvetica, sans-serif; | ||
| font-size: 13px; | ||
| line-height: 18px; | ||
| } | ||
| p, ol, ul, td { | ||
| font-family: verdana, arial, helvetica, sans-serif; | ||
| font-size: 13px; | ||
| line-height: 18px; | ||
| } | ||
| pre { | ||
| background-color: #eee; | ||
| padding: 10px; | ||
| font-size: 11px; | ||
| } | ||
| a { | ||
| color: #000; | ||
| &:visited { | ||
| color: #666; | ||
| } | ||
| &:hover { | ||
| color: #fff; | ||
| background-color: #000; | ||
| } | ||
| } | ||
| th { | ||
| padding-bottom: 5px; | ||
| } | ||
| td { | ||
| padding: 0 5px 7px; | ||
| } | ||
| div { | ||
| &.field, &.actions { | ||
| margin-bottom: 10px; | ||
| } | ||
| } | ||
| #notice { | ||
| color: green; | ||
| } | ||
| .field_with_errors { | ||
| padding: 2px; | ||
| background-color: red; | ||
| display: table; | ||
| } | ||
| #error_explanation { | ||
| width: 450px; | ||
| border: 2px solid red; | ||
| padding: 7px 7px 0; | ||
| margin-bottom: 20px; | ||
| background-color: #f0f0f0; | ||
| h2 { | ||
| text-align: left; | ||
| font-weight: bold; | ||
| padding: 5px 5px 5px 15px; | ||
| font-size: 12px; | ||
| margin: -7px -7px 0; | ||
| background-color: #c00; | ||
| color: #fff; | ||
| } | ||
| ul li { | ||
| font-size: 12px; | ||
| list-style: square; | ||
| } | ||
| } | ||
| label { | ||
| display: block; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| class AddressesController < ApplicationController | ||
| before_action :set_address, only: [:show, :edit, :update, :destroy] | ||
| # GET /addresses | ||
| # GET /addresses.json | ||
| def index | ||
| @addresses = Address.all | ||
| end | ||
| # GET /addresses/1 | ||
| # GET /addresses/1.json | ||
| def show | ||
| end | ||
| # GET /addresses/new | ||
| def new | ||
| @address = Address.new | ||
| end | ||
| # GET /addresses/1/edit | ||
| def edit | ||
| end | ||
| # POST /addresses | ||
| # POST /addresses.json | ||
| def create | ||
| @address = Address.new(address_params) | ||
| respond_to do |format| | ||
| if @address.save | ||
| format.html { redirect_to @address, notice: 'Address was successfully created.' } | ||
| format.json { render :show, status: :created, location: @address } | ||
| else | ||
| format.html { render :new } | ||
| format.json { render json: @address.errors, status: :unprocessable_entity } | ||
| end | ||
| end | ||
| end | ||
| # PATCH/PUT /addresses/1 | ||
| # PATCH/PUT /addresses/1.json | ||
| def update | ||
| respond_to do |format| | ||
| if @address.update(address_params) | ||
| format.html { redirect_to @address, notice: 'Address was successfully updated.' } | ||
| format.json { render :show, status: :ok, location: @address } | ||
| else | ||
| format.html { render :edit } | ||
| format.json { render json: @address.errors, status: :unprocessable_entity } | ||
| end | ||
| end | ||
| end | ||
| # DELETE /addresses/1 | ||
| # DELETE /addresses/1.json | ||
| def destroy | ||
| @address.destroy | ||
| respond_to do |format| | ||
| format.html { redirect_to addresses_url, notice: 'Address was successfully destroyed.' } | ||
| format.json { head :no_content } | ||
| end | ||
| end | ||
| private | ||
| # Use callbacks to share common setup or constraints between actions. | ||
| def set_address | ||
| @address = Address.find(params[:id]) | ||
| end | ||
| # Never trust parameters from the scary internet, only allow the white list through. | ||
| def address_params | ||
| params.require(:address).permit(:location_id, :address_1, :address_2, :city, :state_province, :postal_code, :country) | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| module AddressesHelper | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class Address < ApplicationRecord | ||
| validates :location_id, presence: true | ||
| validates :address_1, presence: true | ||
| validates :city, presence: true | ||
| validates :state_provice, presence: true | ||
| validates :postal_code, presence: true | ||
| validates_length_of :copostal_codeuntry, :maximum => 5 | ||
| validates_length_of :postal_code, :minimum => 5 | ||
| validates :country, presence: true | ||
| validates_length_of :country, :maximum => 2 | ||
| validates_length_of :country, :minimum => 2 | ||
| def validate_postal_code | ||
| regex = /\A\d{5}(-\d{4})?\z/ | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -32,6 +32,14 @@ def self.load!(data, **kwargs) | ||
| location_id: location.id | ||
| ) | ||
| end | ||
| # unless record[:address].nil? | ||
| # formatted_address = AddressNormalizer.normalize(address_string: record[:address]) | ||
| # Address.find_or_create_by!( | ||
| # location_id: location.id, | ||
| # address_1: formatted_address[:address_1], | ||
| # address_2: formatted_address[:address_2] | ||
| # ) | ||
| # end | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need the loader to support addresses? | ||
| end | ||
| end | ||
| rescue ActiveRecord::RecordInvalid | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| class AddressNormalizer | ||
| def self.normalize(address_string: raise) | ||
| query = Geocoder.search(address_string).first | ||
| address1,city,state_zip,country = query.data["formatted_address"].split(',') | ||
| state,zip = state_zip.split(' ') | ||
| output = { | ||
| 'address' => query.data["formatted_address"], | ||
| 'address1' => address1, | ||
| 'address2' => nil, | ||
| 'city' => city, | ||
| 'state_province' => state, | ||
| 'postal_code' => zip, | ||
| 'country' => country, | ||
| 'lat' => query.data["geometry"]["location"]["lat"], | ||
| 'lng' => query.data["geometry"]["location"]["lng"] | ||
| } | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| json.extract! address, :id, :location_id, :address_1, :address_2, :city, :state_province, :postal_code, :country, :created_at, :updated_at | ||
| json.url address_url(address, format: :json) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| <%= form_with(model: address, local: true) do |form| %> | ||
| <% if address.errors.any? %> | ||
| <div id="error_explanation"> | ||
| <h2><%= pluralize(address.errors.count, "error") %> prohibited this address from being saved:</h2> | ||
| <ul> | ||
| <% address.errors.full_messages.each do |message| %> | ||
| <li><%= message %></li> | ||
| <% end %> | ||
| </ul> | ||
| </div> | ||
| <% end %> | ||
| <div class="field"> | ||
| <%= form.label :location_id %> | ||
| <%= form.number_field :location_id, id: :address_location_id %> | ||
| </div> | ||
| <div class="field"> | ||
| <%= form.label :address_1 %> | ||
| <%= form.text_field :address_1, id: :address_address_1 %> | ||
| </div> | ||
| <div class="field"> | ||
| <%= form.label :address_2 %> | ||
| <%= form.text_field :address_2, id: :address_address_2 %> | ||
| </div> | ||
| <div class="field"> | ||
| <%= form.label :city %> | ||
| <%= form.text_field :city, id: :address_city %> | ||
| </div> | ||
| <div class="field"> | ||
| <%= form.label :state_province %> | ||
| <%= form.text_field :state_province, id: :address_state_province %> | ||
| </div> | ||
| <div class="field"> | ||
| <%= form.label :postal_code %> | ||
| <%= form.text_field :postal_code, id: :address_postal_code %> | ||
| </div> | ||
| <div class="field"> | ||
| <%= form.label :country %> | ||
| <%= form.text_field :country, id: :address_country %> | ||
| </div> | ||
| <div class="actions"> | ||
| <%= form.submit %> | ||
| </div> | ||
| <% end %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <h1>Editing Address</h1> | ||
| <%= render 'form', address: @address %> | ||
| <%= link_to 'Show', @address %> | | ||
| <%= link_to 'Back', addresses_path %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <p id="notice"><%= notice %></p> | ||
| <h1>Addresses</h1> | ||
| <table> | ||
| <thead> | ||
| <tr> | ||
| <th>Loction</th> | ||
| <th>Address 1</th> | ||
| <th>Address 2</th> | ||
| <th>City</th> | ||
| <th>State province</th> | ||
| <th>Postal code</th> | ||
| <th>Country</th> | ||
| <th colspan="3"></th> | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| <% @addresses.each do |address| %> | ||
| <tr> | ||
| <td><%= address.location_id %></td> | ||
| <td><%= address.address_1 %></td> | ||
| <td><%= address.address_2 %></td> | ||
| <td><%= address.city %></td> | ||
| <td><%= address.state_province %></td> | ||
| <td><%= address.postal_code %></td> | ||
| <td><%= address.country %></td> | ||
| <td><%= link_to 'Show', address %></td> | ||
| <td><%= link_to 'Edit', edit_address_path(address) %></td> | ||
| <td><%= link_to 'Destroy', address, method: :delete, data: { confirm: 'Are you sure?' } %></td> | ||
| </tr> | ||
| <% end %> | ||
| </tbody> | ||
| </table> | ||
| <br> | ||
| <%= link_to 'New Address', new_address_path %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| json.array! @addresses, partial: 'addresses/address', as: :address |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <h1>New Address</h1> | ||
| <%= render 'form', address: @address %> | ||
| <%= link_to 'Back', addresses_path %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <p id="notice"><%= notice %></p> | ||
| <p> | ||
| <strong>Loction:</strong> | ||
| <%= @address.location_id %> | ||
| </p> | ||
| <p> | ||
| <strong>Address 1:</strong> | ||
| <%= @address.address_1 %> | ||
| </p> | ||
| <p> | ||
| <strong>Address 2:</strong> | ||
| <%= @address.address_2 %> | ||
| </p> | ||
| <p> | ||
| <strong>City:</strong> | ||
| <%= @address.city %> | ||
| </p> | ||
| <p> | ||
| <strong>State province:</strong> | ||
| <%= @address.state_province %> | ||
| </p> | ||
| <p> | ||
| <strong>Postal code:</strong> | ||
| <%= @address.postal_code %> | ||
| </p> | ||
| <p> | ||
| <strong>Country:</strong> | ||
| <%= @address.country %> | ||
| </p> | ||
| <%= link_to 'Edit', edit_address_path(@address) %> | | ||
| <%= link_to 'Back', addresses_path %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| json.partial! "addresses/address", address: @address |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
state_provice=>state_province