Skip to content

Commit

Permalink
Merge pull request #46 from hieuhv07/205/create_address
Browse files Browse the repository at this point in the history
205: create address
  • Loading branch information
Lương Việt Dũng authored Aug 29, 2019
2 parents 3e98169 + 246d333 commit bd07a9b
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ config/environments/*.local.yml

# Ignore application configuration
/config/application.yml
/public/uploads
23 changes: 23 additions & 0 deletions app/controllers/manager/addresses_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Manager
class AddressesController < BaseController
def create
@address = Address.new address_params
if @address.save
flash[:success] = t ".success"
else
flash.now[:danger] = t ".danger"
end
respond_to do |format|
format.js
end
end

private

def address_params
params.require(:address).permit :name, :area_id
end
end
end
5 changes: 4 additions & 1 deletion app/controllers/manager/areas_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ def create
end
end

def edit; end
def edit
@address = @area.addresses.build
@addresses = @area.addresses.newest.page(params[:page]).per Settings.address_per
end

def update
if @area.update area_params
Expand Down
2 changes: 2 additions & 0 deletions app/models/address.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Address < ApplicationRecord
belongs_to :area

Expand Down
1 change: 1 addition & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true

scope :sort_by_name, -> { order name: :DESC }
scope :newest, ->{ order created_at: :DESC }
end
4 changes: 1 addition & 3 deletions app/models/area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

class Area < ApplicationRecord
belongs_to :location
has_many :address, dependent: :destroy

scope :newest, -> { order created_at: :desc }
has_many :addresses, dependent: :destroy

validates :name, presence: true, uniqueness: { case_sensitive: false }
end
3 changes: 1 addition & 2 deletions app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
class Location < ApplicationRecord
has_many :rooms, dependent: :destroy
has_many :areas, dependent: :destroy
validates :name, presence: true, uniqueness: { case_sensitive: false }

scope :newest, -> { order created_at: :desc }
validates :name, presence: true, uniqueness: { case_sensitive: false }
end
7 changes: 7 additions & 0 deletions app/views/manager/addresses/create.js.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- if @address.errors.present?
| $("#message_errors").html("#{j(render 'shared/manager/error_messages', object: @address)}");
toastr.error("#{flash[:danger]}");
- else
| $('#address_name').val('');
$('.load-address').load(location.href + ' .load-address');
toastr.success("#{flash[:success]}");
7 changes: 7 additions & 0 deletions app/views/manager/areas/_address.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tr
td
= address_counter.next
td
= address.name
td
= link_to t(".delete"), "#", class: "btn btn-danger"
15 changes: 15 additions & 0 deletions app/views/manager/areas/_new_address.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#myModal.modal.fade
.modal-dialog
.modal-content
.modal-header
h4.modal-title = t ".new"
= button_tag t(".btn"), class: "close", data: {dismiss: "modal"}, type: "button"
.modal-body
= form_for :address, url: manager_area_addresses_path(@area), remote: true do |f|
#message_errors
= f.hidden_field :area_id, value: @area.id
.form-group
= f.text_field :name, class: "form-control", placeholder: t(".enter_name")
= f.submit t(".save"), class: "btn btn-primary"
.modal-footer
= button_tag t(".close"), class: "btn btn-danger", data: {dismiss: "modal"}, type: "button"
17 changes: 17 additions & 0 deletions app/views/manager/areas/edit.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,20 @@
= f.text_field :name, class: "form-control", required: true

= f.submit t(".save"), class: "btn btn-primary"
.row
.col-md-12
.card
.card-header
h4.card-title
= t ".address"
= link_to t(".address"), "#", class: "btn btn-primary btn-xs right btn_right", data: {toggle: "modal", target: "#myModal"}
.card-body.load-address
table.table.table-hover.table-striped
thead
th= t ".key"
th= t ".name"
th= t ".action"
tbody
= render partial: "address", collection: @addresses
= paginate @addresses, theme: "twitter-bootstrap-4", pagination_class: "pagination-sm"
= render "new_address", address: @address
1 change: 1 addition & 0 deletions app/views/manager/locations/edit.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
th= t ".action"
tbody
= render partial: "area", collection: @areas
= paginate @areas, theme: "twitter-bootstrap-4", pagination_class: "pagination-sm"
= render "new_area", area: @area
16 changes: 16 additions & 0 deletions config/locales/manager/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,24 @@ en:
title: "Edit Areas"
name: "Name"
save: "Save Changes"
key: "#"
name: "Name"
action: "Action"
address: "New Address"
update:
success: "Update area successfully!"
danger: "Update area has problem!"
destroy:
success: "Destroy area successfully!"
danger: "Destroy area has problem!"
new_address:
save: "Save Changes"
new: "New Address"
enter_name: "Enter name..."
close: "Close"
btn: "×"
address:
delete: "Delete"
members:
form:
error: "The form contains %{count}."
Expand All @@ -138,6 +150,10 @@ en:
edit:
update: "Update"
edit_member: "Edit Member"
addresses:
create:
success: "Create address successfully!"
danger: "Create address has problem!"
messages:
success:
admins:
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
resources :locations do
resources :areas, only: %i[new create]
end
resources :areas, except: %i[new create]
resources :areas, except: %i[new create]do
resources :addresses, only: %i[new create]
end
end
end
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ location_per: 10
max_size: 5_242_880
file_path: image/
time_out: 30
address_per: 10

0 comments on commit bd07a9b

Please sign in to comment.