From 246d333719cd2a1ad41614f8708b21714920c6c4 Mon Sep 17 00:00:00 2001 From: HieuCR7 Date: Tue, 27 Aug 2019 17:08:09 +0700 Subject: [PATCH] 205: create address --- .gitignore | 1 + .../manager/addresses_controller.rb | 23 +++++++++++++++++++ app/controllers/manager/areas_controller.rb | 5 +++- app/models/address.rb | 2 ++ app/models/application_record.rb | 1 + app/models/area.rb | 4 +--- app/models/location.rb | 3 +-- app/views/manager/addresses/create.js.slim | 7 ++++++ app/views/manager/areas/_address.html.slim | 7 ++++++ .../manager/areas/_new_address.html.slim | 15 ++++++++++++ app/views/manager/areas/edit.html.slim | 17 ++++++++++++++ app/views/manager/locations/edit.html.slim | 1 + config/locales/manager/en.yml | 16 +++++++++++++ config/routes.rb | 4 +++- config/settings.yml | 1 + 15 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 app/controllers/manager/addresses_controller.rb create mode 100644 app/views/manager/addresses/create.js.slim create mode 100644 app/views/manager/areas/_address.html.slim create mode 100644 app/views/manager/areas/_new_address.html.slim diff --git a/.gitignore b/.gitignore index b4bae68..ee42172 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,4 @@ config/environments/*.local.yml # Ignore application configuration /config/application.yml +/public/uploads diff --git a/app/controllers/manager/addresses_controller.rb b/app/controllers/manager/addresses_controller.rb new file mode 100644 index 0000000..2588586 --- /dev/null +++ b/app/controllers/manager/addresses_controller.rb @@ -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 diff --git a/app/controllers/manager/areas_controller.rb b/app/controllers/manager/areas_controller.rb index dbdcbfd..9967f2d 100644 --- a/app/controllers/manager/areas_controller.rb +++ b/app/controllers/manager/areas_controller.rb @@ -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 diff --git a/app/models/address.rb b/app/models/address.rb index bcd854c..1873c1e 100644 --- a/app/models/address.rb +++ b/app/models/address.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Address < ApplicationRecord belongs_to :area diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 508f2ee..fb31f82 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -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 diff --git a/app/models/area.rb b/app/models/area.rb index 457bffa..91aa5b4 100644 --- a/app/models/area.rb +++ b/app/models/area.rb @@ -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 diff --git a/app/models/location.rb b/app/models/location.rb index 9e33ce9..fe5c6ad 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -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 diff --git a/app/views/manager/addresses/create.js.slim b/app/views/manager/addresses/create.js.slim new file mode 100644 index 0000000..06b7090 --- /dev/null +++ b/app/views/manager/addresses/create.js.slim @@ -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]}"); diff --git a/app/views/manager/areas/_address.html.slim b/app/views/manager/areas/_address.html.slim new file mode 100644 index 0000000..2c5698b --- /dev/null +++ b/app/views/manager/areas/_address.html.slim @@ -0,0 +1,7 @@ +tr + td + = address_counter.next + td + = address.name + td + = link_to t(".delete"), "#", class: "btn btn-danger" diff --git a/app/views/manager/areas/_new_address.html.slim b/app/views/manager/areas/_new_address.html.slim new file mode 100644 index 0000000..d59afb5 --- /dev/null +++ b/app/views/manager/areas/_new_address.html.slim @@ -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" diff --git a/app/views/manager/areas/edit.html.slim b/app/views/manager/areas/edit.html.slim index 2aa751d..99d33a2 100644 --- a/app/views/manager/areas/edit.html.slim +++ b/app/views/manager/areas/edit.html.slim @@ -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 diff --git a/app/views/manager/locations/edit.html.slim b/app/views/manager/locations/edit.html.slim index 21cae33..29da3f3 100644 --- a/app/views/manager/locations/edit.html.slim +++ b/app/views/manager/locations/edit.html.slim @@ -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 diff --git a/config/locales/manager/en.yml b/config/locales/manager/en.yml index 7d97fb3..b463df0 100644 --- a/config/locales/manager/en.yml +++ b/config/locales/manager/en.yml @@ -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}." @@ -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: diff --git a/config/routes.rb b/config/routes.rb index b2d902c..f079c79 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/config/settings.yml b/config/settings.yml index 552f04f..3b16349 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -2,3 +2,4 @@ location_per: 10 max_size: 5_242_880 file_path: image/ time_out: 30 +address_per: 10