Skip to content

Commit

Permalink
act as taggable fonctionne pour filtrer les event par les hobbies du …
Browse files Browse the repository at this point in the history
…user
  • Loading branch information
yasmine-glitch committed Nov 26, 2021
1 parent 69e5b74 commit 41eedd4
Show file tree
Hide file tree
Showing 17 changed files with 283 additions and 15 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ gem 'pg_search', '~> 2.3.0'
gem "geocoder"
gem 'pundit'
gem 'faker'
gem 'acts-as-taggable-on', '~> 8.1'

group :development, :test do gem 'pry-byebug'
gem 'pry-rails'
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2, >= 2.2.2)
acts-as-taggable-on (8.1.0)
activerecord (>= 5.0, < 6.2)
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
autoprefixer-rails (10.2.5.0)
Expand Down Expand Up @@ -135,6 +137,8 @@ GEM
nokogiri (1.12.5)
mini_portile2 (~> 2.6.1)
racc (~> 1.4)
nokogiri (1.12.5-x86_64-linux)
racc (~> 1.4)
orm_adapter (0.5.0)
pg (1.2.3)
pg_search (2.3.5)
Expand Down Expand Up @@ -268,6 +272,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
acts-as-taggable-on (~> 8.1)
autoprefixer-rails (= 10.2.5)
bootsnap (>= 1.4.2)
byebug
Expand Down
1 change: 1 addition & 0 deletions app/assets/stylesheets/components/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
@import "sweetalert";
@import "calendar";
@import "card-user";
@import "tags";
38 changes: 38 additions & 0 deletions app/assets/stylesheets/components/_tags.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.title{
margin-top: 20px;
margin-bottom: 20px;
}
.wrapper-tags{
-webkit-box-align: center !important;
display: flex !important;
align-items: center !important;
margin-top: 20px;
margin-bottom: 20px;
}

.tags{
display: inline-block !important;
white-space: nowrap !important;
padding-right: 8px !important;
padding-top: 4px !important;
padding-bottom: 4px !important;
width: 100% !important;
}

.tags-precision{
cursor: pointer !important;
text-align: center !important;
border: 1px solid rgb(221, 221, 221) !important;
background-color: rgb(255, 255, 255) !important;
outline: none !important;
margin: 0px !important;
border-radius: 30px !important;
color: rgb(34, 34, 34) !important;
font-family: Circular, -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif !important;
position: relative !important;
padding: 10px 16px !important;
font-size: 12px !important;
line-height: 16px !important;
width: 100% !important;
font-size: larger;
}
10 changes: 8 additions & 2 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
class ActivitiesController < ApplicationController
def index
@activities = policy_scope(Activity).order(created_at: :desc)
# authorize @activity

## RETURN THE RESULTS FROM THE HOMEPAGE SEARCH
# check if the user typed an address in the searchbar
if params[:query].present?
# and corresponding to current user's hobbies (each user has 3 hobbies)
@activities = Activity.where(category: current_user.hobby_list)
# if yes, render all activities located XX km around this address
@activities = Activity.search_by_place(params[:query]).near(params[:query], 100)
# @activities = Activity.near(params[:query], 6)
@activities = @activities.search_by_place(params[:query]).near(params[:query], 100)
# return only the activity which are not full
@activities = @activities.select { |activity| activity.bookings.length <= activity.capacity_max }
@title = "We found #{@activities.length} activities near #{params[:query]}"
Expand All @@ -20,11 +22,15 @@ def index

# if the user didn't typed an address, check if he typed a date
elsif params[:start_date].present?
# and corresponding to current user hobbies (each user has 3 hobbies)
@activities = Activity.where(category: current_user.hobby_list)
# if yes, render all activities with the same start date
@activities = @activities.filter { |activity| activity.start_date >= params[:start_date] }
@title = "We found #{@activities.length} activities in the world"
# if the user didn't type any place or date
else
# and corresponding to current user hobbies (each user has 3 hobbies)
@activities = Activity.where(category: current_user.hobby_list)
# render all activities not fully booked
@activities = @activities.select { |activity| activity.bookings.length <= activity.capacity_max }
# filter activities with a future start date
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class User < ApplicationRecord
has_many :activities, through: :bookings
has_many :travels, dependent: :destroy
has_one_attached :photo
acts_as_taggable_on :hobbies

validates :photo, presence: true
validates :first_name, :last_name, presence: true
Expand Down
22 changes: 22 additions & 0 deletions app/views/activities/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
<!-- affiche une liste de tags à sélectionner-->
<div class="container">
<div class="title">
<h5>Filter all activities by categories 🔥 </h3>
</div>
<div class="wrapper-tags">
<span class="tags-precision">Sports 🏓</span>
<span class="tags-precision">Nature 🏔</span>
<span class="tags-precision">Visiting 🗽</span>
<span class="tags-precision">Food 🍜</span>
<span class="tags-precision">Bars 🍺</span>
<span class="tags-precision">Tech 💾</span>
<span class="tags-precision">Arts 🎨</span>
<span class="tags-precision">Crafts 🪚</span>
<span class="tags-precision">Dancing 💃🏾🕺🏼</span>
<span class="tags-precision">Singing 🎤</span>
<span class="tags-precision">Shopping 🛍</span>
</div>
</div>

<div class="flex-container">
<!-- affiche les résultats d'activity cards correspondant à la recherche-->
<div class="container flex-item">
Expand Down Expand Up @@ -38,3 +58,5 @@
data-mapbox-markers-value="<%= @markers.to_json %>"
data-mapbox-api-key-value="<%= ENV['MAPBOX_API_KEY'] %>">
</div>

<%= console %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This migration comes from acts_as_taggable_on_engine (originally 1)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class ActsAsTaggableOnMigration < ActiveRecord::Migration[4.2]; end
else
class ActsAsTaggableOnMigration < ActiveRecord::Migration; end
end
ActsAsTaggableOnMigration.class_eval do
def self.up
create_table ActsAsTaggableOn.tags_table do |t|
t.string :name
t.timestamps
end

create_table ActsAsTaggableOn.taggings_table do |t|
t.references :tag, foreign_key: { to_table: ActsAsTaggableOn.tags_table }

# You should make sure that the column created is
# long enough to store the required class names.
t.references :taggable, polymorphic: true
t.references :tagger, polymorphic: true

# Limit is created to prevent MySQL error on index
# length for MyISAM table type: http://bit.ly/vgW2Ql
t.string :context, limit: 128

t.datetime :created_at
end

add_index ActsAsTaggableOn.taggings_table, :tag_id
add_index ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :context], name: 'taggings_taggable_context_idx'
end

def self.down
drop_table ActsAsTaggableOn.taggings_table
drop_table ActsAsTaggableOn.tags_table
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This migration comes from acts_as_taggable_on_engine (originally 2)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddMissingUniqueIndices < ActiveRecord::Migration[4.2]; end
else
class AddMissingUniqueIndices < ActiveRecord::Migration; end
end
AddMissingUniqueIndices.class_eval do
def self.up
add_index ActsAsTaggableOn.tags_table, :name, unique: true

remove_index ActsAsTaggableOn.taggings_table, :tag_id if index_exists?(ActsAsTaggableOn.taggings_table, :tag_id)
remove_index ActsAsTaggableOn.taggings_table, name: 'taggings_taggable_context_idx'
add_index ActsAsTaggableOn.taggings_table,
[:tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type],
unique: true, name: 'taggings_idx'
end

def self.down
remove_index ActsAsTaggableOn.tags_table, :name

remove_index ActsAsTaggableOn.taggings_table, name: 'taggings_idx'

add_index ActsAsTaggableOn.taggings_table, :tag_id unless index_exists?(ActsAsTaggableOn.taggings_table, :tag_id)
add_index ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :context], name: 'taggings_taggable_context_idx'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This migration comes from acts_as_taggable_on_engine (originally 3)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddTaggingsCounterCacheToTags < ActiveRecord::Migration[4.2]; end
else
class AddTaggingsCounterCacheToTags < ActiveRecord::Migration; end
end
AddTaggingsCounterCacheToTags.class_eval do
def self.up
add_column ActsAsTaggableOn.tags_table, :taggings_count, :integer, default: 0

ActsAsTaggableOn::Tag.reset_column_information
ActsAsTaggableOn::Tag.find_each do |tag|
ActsAsTaggableOn::Tag.reset_counters(tag.id, ActsAsTaggableOn.taggings_table)
end
end

def self.down
remove_column ActsAsTaggableOn.tags_table, :taggings_count
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This migration comes from acts_as_taggable_on_engine (originally 4)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddMissingTaggableIndex < ActiveRecord::Migration[4.2]; end
else
class AddMissingTaggableIndex < ActiveRecord::Migration; end
end
AddMissingTaggableIndex.class_eval do
def self.up
add_index ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :context], name: 'taggings_taggable_context_idx'
end

def self.down
remove_index ActsAsTaggableOn.taggings_table, name: 'taggings_taggable_context_idx'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This migration comes from acts_as_taggable_on_engine (originally 5)
# This migration is added to circumvent issue #623 and have special characters
# work properly
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class ChangeCollationForTagNames < ActiveRecord::Migration[4.2]; end
else
class ChangeCollationForTagNames < ActiveRecord::Migration; end
end
ChangeCollationForTagNames.class_eval do
def up
if ActsAsTaggableOn::Utils.using_mysql?
execute("ALTER TABLE #{ActsAsTaggableOn.tags_table} MODIFY name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin;")
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This migration comes from acts_as_taggable_on_engine (originally 6)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddMissingIndexesOnTaggings < ActiveRecord::Migration[4.2]; end
else
class AddMissingIndexesOnTaggings < ActiveRecord::Migration; end
end
AddMissingIndexesOnTaggings.class_eval do
def change
add_index ActsAsTaggableOn.taggings_table, :tag_id unless index_exists? ActsAsTaggableOn.taggings_table, :tag_id
add_index ActsAsTaggableOn.taggings_table, :taggable_id unless index_exists? ActsAsTaggableOn.taggings_table, :taggable_id
add_index ActsAsTaggableOn.taggings_table, :taggable_type unless index_exists? ActsAsTaggableOn.taggings_table, :taggable_type
add_index ActsAsTaggableOn.taggings_table, :tagger_id unless index_exists? ActsAsTaggableOn.taggings_table, :tagger_id
add_index ActsAsTaggableOn.taggings_table, :context unless index_exists? ActsAsTaggableOn.taggings_table, :context

unless index_exists? ActsAsTaggableOn.taggings_table, [:tagger_id, :tagger_type]
add_index ActsAsTaggableOn.taggings_table, [:tagger_id, :tagger_type]
end

unless index_exists? ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :tagger_id, :context], name: 'taggings_idy'
add_index ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :tagger_id, :context], name: 'taggings_idy'
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This migration comes from acts_as_taggable_on_engine (originally 7)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddTenantToTaggings < ActiveRecord::Migration[4.2]; end
else
class AddTenantToTaggings < ActiveRecord::Migration; end
end
AddTenantToTaggings.class_eval do
def self.up
add_column :taggings, :tenant, :string, limit: 128
add_index :taggings, :tenant unless index_exists? :taggings, :tenant
end

def self.down
remove_index :taggings, :tenant
remove_column :taggings, :tenant
end
end
5 changes: 5 additions & 0 deletions db/migrate/20211126113845_add_category_to_activity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCategoryToActivity < ActiveRecord::Migration[6.0]
def change
add_column :activities, :category, :string
end
end
33 changes: 32 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_11_25_140327) do
ActiveRecord::Schema.define(version: 2021_11_26_113845) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -49,6 +49,7 @@
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "description"
t.string "category"
t.index ["owner_id"], name: "index_activities_on_owner_id"
end

Expand All @@ -62,6 +63,35 @@
t.index ["user_id"], name: "index_bookings_on_user_id"
end

create_table "taggings", id: :serial, force: :cascade do |t|
t.integer "tag_id"
t.string "taggable_type"
t.integer "taggable_id"
t.string "tagger_type"
t.integer "tagger_id"
t.string "context", limit: 128
t.datetime "created_at"
t.string "tenant", limit: 128
t.index ["context"], name: "index_taggings_on_context"
t.index ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
t.index ["tag_id"], name: "index_taggings_on_tag_id"
t.index ["taggable_id", "taggable_type", "context"], name: "taggings_taggable_context_idx"
t.index ["taggable_id", "taggable_type", "tagger_id", "context"], name: "taggings_idy"
t.index ["taggable_id"], name: "index_taggings_on_taggable_id"
t.index ["taggable_type"], name: "index_taggings_on_taggable_type"
t.index ["tagger_id", "tagger_type"], name: "index_taggings_on_tagger_id_and_tagger_type"
t.index ["tagger_id"], name: "index_taggings_on_tagger_id"
t.index ["tenant"], name: "index_taggings_on_tenant"
end

create_table "tags", id: :serial, force: :cascade do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "taggings_count", default: 0
t.index ["name"], name: "index_tags_on_name", unique: true
end

create_table "travels", force: :cascade do |t|
t.string "country"
t.string "city"
Expand Down Expand Up @@ -100,5 +130,6 @@
add_foreign_key "activities", "users", column: "owner_id"
add_foreign_key "bookings", "activities"
add_foreign_key "bookings", "users"
add_foreign_key "taggings", "tags"
add_foreign_key "travels", "users"
end
Loading

0 comments on commit 41eedd4

Please sign in to comment.