Skip to content

Commit

Permalink
Merge pull request #57 from hieuhv07/212/create_and_update_location_f…
Browse files Browse the repository at this point in the history
…avorites

212: create, update location favorites
  • Loading branch information
N-Viet-ruby-dev authored Sep 4, 2019
2 parents 51373be + fbca2f6 commit 0d2a20d
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ gem "cocoon"
gem "figaro"
gem "carrierwave"
gem "mini_magick", "~> 4.3"
gem "select2-rails"

group :development, :test do
gem "byebug", platforms: %i[mri mingw x64_mingw]
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ GEM
sprockets (> 3.0)
sprockets-rails
tilt
select2-rails (4.0.3)
thor (~> 0.14)
selenium-webdriver (3.142.3)
childprocess (>= 0.5, < 2.0)
rubyzip (~> 1.2, >= 1.2.2)
Expand Down Expand Up @@ -349,6 +351,7 @@ DEPENDENCIES
rspec-rails (~> 3.8)
rubocop (~> 0.74.0)
sass-rails (~> 5.0)
select2-rails
selenium-webdriver
shoulda-matchers (~> 3.0)
slim-rails
Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/manager/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//= require datatable
//= require manager/custom
//= require cocoon
//= require select2

/*global toastr*/
toastr.options = {
Expand Down
5 changes: 5 additions & 0 deletions app/assets/javascripts/manager/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,10 @@ $(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();

$('.preview-image').on("change", previewImages);

$(".select2").select2({
placeholder: "Select a favorite spaces",
allowClear: true
})
});

1 change: 1 addition & 0 deletions app/assets/stylesheets/manager/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
@import "font-awesome";
@import "toastr";
@import "datatable";
@import "select2";
2 changes: 1 addition & 1 deletion app/controllers/manager/locations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def destroy
private

def location_params
params.require(:location).permit :name
params.require(:location).permit :name, favorite_space_ids: []
end

def load_location
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/managers_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ def build_location_for_rooms
def bill_vouchers(price_bill)
(price_bill.price.cost - ((price_bill.price.cost * price_bill.voucher.sale) / 100)) + price_bill.price.cleaning_fee
end

def select_favorite_space
FavoriteSpace.all.map {|f| [f.name, f.id]}
end
end
3 changes: 3 additions & 0 deletions app/models/favorite_space.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

class FavoriteSpace < ApplicationRecord
has_many :location_favorites, dependent: :destroy
has_many :locations, through: :location_favorites

validates :name, length: { in: 3..50 }, uniqueness: { case_sensitive: false }
end
2 changes: 2 additions & 0 deletions app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
class Location < ApplicationRecord
has_many :rooms, dependent: :destroy
has_many :areas, dependent: :destroy
has_many :location_favorites, dependent: :destroy
has_many :favorite_spaces, through: :location_favorites

validates :name, presence: true, uniqueness: { case_sensitive: false }
end
6 changes: 6 additions & 0 deletions app/models/location_favorite.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class LocationFavorite < ApplicationRecord
belongs_to :location
belongs_to :favorite_space
end
4 changes: 4 additions & 0 deletions app/views/manager/locations/_form.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
= f.label t ".name", class: "control-label"
= f.text_field :name, class: "form-control", required: true

.form-group
= f.label t(".favorite"), class: "control-label"
= f.select(:favorite_space_ids, select_favorite_space, {}, {class: "form-control select2", multiple: true})

= f.submit t(".submit"), class: "btn btn-primary"
1 change: 1 addition & 0 deletions config/locales/manager/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ en:
form:
name: "Name"
submit: "Save changes"
favorite: "Favorite Spaces"
new:
add: "Add Locations"
title: "New locations"
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20190821124656_create_location_favorites.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateLocationFavorites < ActiveRecord::Migration[5.2]
def change
create_table :location_favorites do |t|
t.references :location, foreign_key: true
t.references :favorite_space, foreign_key: true

t.timestamps
end
end
end
11 changes: 11 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@
t.datetime "updated_at", null: false
end

create_table "location_favorites", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.bigint "location_id"
t.bigint "favorite_space_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["favorite_space_id"], name: "index_location_favorites_on_favorite_space_id"
t.index ["location_id"], name: "index_location_favorites_on_location_id"
end

create_table "locations", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
Expand Down Expand Up @@ -136,6 +145,8 @@
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "addresses", "areas"
add_foreign_key "areas", "locations"
add_foreign_key "location_favorites", "favorite_spaces"
add_foreign_key "location_favorites", "locations"
add_foreign_key "rooms", "locations"
add_foreign_key "rooms", "users"
end
4 changes: 4 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@
address: address,
confirmed_at: Time.now)
end

["CĂN HỘ CHUNG CƯ","NHÀ RIÊNG", "CĂN HỘ STUDIO", "KHÁC", "BIỆT THỰ"].each do |favorite_space|
FavoriteSpace.create! name: favorite_space
end

0 comments on commit 0d2a20d

Please sign in to comment.