Skip to content

Commit

Permalink
Add SitesTags. Add Site Tags to view, tests. Fix multiple select prob…
Browse files Browse the repository at this point in the history
…lem (#1048)
  • Loading branch information
lexiwitch authored Mar 11, 2022
1 parent 14eeab2 commit d90451e
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 48 deletions.
4 changes: 4 additions & 0 deletions app/helpers/sites_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ def options_for_sites_neighbourhoods
@all_neighbourhoods.filter { |e| e.name != '' }
.collect { |e| { name: e.contextual_name, id: e.id } }
end

def options_for_tags
policy_scope(Tag).order(:name).pluck(:name, :id)
end
end
10 changes: 7 additions & 3 deletions app/javascript/src/behaviors/behaviors.partner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ jQuery.extend(Behaviors, {
/* service area bits */

// Attach select2 to the current select2 nodes
$('.select2').each(function () { $(this).select2({ multiple: false }); });
$('.select2').each(function () {
multiple = $(this).hasClass('multi-select');
$(this).select2({ multiple: multiple });
});

// Attach select2 to all future select2 nodes
$('.sites_neighbourhoods').bind('cocoon:after-insert', function (_, element) {
$('.select2', element).select2({ multiple: false });
$('.sites_neighbourhoods').bind('cocoon:after-insert', function (_, __) {
multiple = $('.select2', element).hasClass('multi-select');
$('.select2', element).select2({ multiple: multiple });
});

/* */
Expand Down
9 changes: 7 additions & 2 deletions app/javascript/src/behaviors/behaviors.site.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ jQuery.extend(Behaviors, {
$('.cocoon_delete-this').parents('.nested-fields').remove();

// Attach select2 to the current select2 nodes
$('.select2').each(function () { $(this).select2({ multiple: false }); });
// If it is a multiple select item, then we account for that
$('.select2').each(function () {
multiple = $(this).hasClass('multi-select');
$(this).select2({ multiple: multiple });
});

// Attach select2 to all future select2 nodes
$('.sites_neighbourhoods').bind('cocoon:after-insert', function (_, element) {
$('.select2', element).select2({ multiple: false });
multiple = $('.select2', element).hasClass('multi-select');
$('.select2', element).select2({ multiple: multiple });
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class Site < ApplicationRecord

has_many :neighbourhoods, through: :sites_neighbourhoods

has_many :sites_tag, dependent: :destroy
has_many :tags, through: :sites_tag

has_and_belongs_to_many :supporters

belongs_to :site_admin, class_name: 'User', optional: true
Expand Down
11 changes: 11 additions & 0 deletions app/models/sites_tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class SitesTag < ApplicationRecord
belongs_to :tag
belongs_to :site
validates :tag_id,
uniqueness: {
scope: :site_id,
message: 'Site cannot be assigned more than once to a tag'
}
end
3 changes: 3 additions & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class Tag < ApplicationRecord
has_many :partner_tags, dependent: :destroy
has_many :partners, through: :partner_tags

has_many :sites_tag, dependent: :destroy
has_many :sites, through: :sites_tag

validates :name, :slug, presence: true
validates :name, :slug, uniqueness: true
validates :description,
Expand Down
46 changes: 11 additions & 35 deletions app/policies/site_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,17 @@ def destroy?
end

def permitted_attributes
if user.root?
[
:id,
:name,
:place_name,
:is_published,
:tagline,
:slug,
:description,
:domain,
:badge_zoom_level,
:logo,
:footer_logo,
:theme,
:hero_image,
:hero_image_credit,
:site_admin_id,
sites_neighbourhoods_attributes: %i[_destroy id neighbourhood_id relation_type],
sites_neighbourhood_attributes: %i[_destroy id neighbourhood_id relation_type]
]
else
[
:id,
:name,
:place_name,
:is_published,
:tagline,
:description,
:badge_zoom_level,
:hero_image,
:hero_image_credit,
sites_neighbourhoods_attributes: %i[_destroy id neighbourhood_id relation_type],
sites_neighbourhood_attributes: %i[_destroy id neighbourhood_id relation_type]
]
end
attrs = %i[id name place_name is_published tagline description
badge_zoom_level hero_image hero_image_credit]
.push(sites_neighbourhoods_attributes: %i[_destroy id neighbourhood_id relation_type],
sites_neighbourhood_attributes: %i[_destroy id neighbourhood_id relation_type],
tag_ids: [])

root_attrs = %i[slug domain logo footer_logo theme site_admin_id]

return root_attrs + attrs if user.root?

attrs
end

class Scope < Scope
Expand Down
3 changes: 1 addition & 2 deletions app/views/admin/partners/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@
<%= f.association :tags,
label: false,
collection: options_for_tags,
as: :check_boxes,
input_html: { class: 'form-check' } %>
input_html: { class: 'form-check select2 multi-select' } %>

<div id="map-pin-div" data-url="<%= image_path('icons/map/map-marker.png') %>"></div>
</div>
Expand Down
15 changes: 15 additions & 0 deletions app/views/admin/sites/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@
<br></br>
</div>

<h2>Tags information</h2>

<div class="row">
<div class="col-md-6">
<p class="font-weight-light">These tags allow filtering for the partners that appear on a site</p>
<%= f.association :tags,
label: false,
collection: options_for_tags,
input_html: { class: 'form-check select2 multi-select' } %>
</div>
</div>
<br>
<br>


<%= f.button :submit, class: "btn btn-primary btn-lg" %>
<% unless @site.new_record? %>
<%= link_to "Destroy Site", admin_site_path(@site), method: :delete, class: "btn btn-danger btn-lg" %>
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20220310125155_add_sites_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class AddSitesTags < ActiveRecord::Migration[6.1]
def change
create_table :sites_tags do |t|
t.references :site, foreign_key: true
t.references :tag, foreign_key: true

t.timestamps
end

add_index :sites_tags, %i[site_id tag_id], unique: true
end
end
14 changes: 13 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: 2022_02_25_162314) do
ActiveRecord::Schema.define(version: 2022_03_10_125155) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -278,6 +278,16 @@
t.index ["supporter_id", "site_id"], name: "index_sites_supporters_on_supporter_id_and_site_id"
end

create_table "sites_tags", force: :cascade do |t|
t.bigint "site_id"
t.bigint "tag_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["site_id", "tag_id"], name: "index_sites_tags_on_site_id_and_tag_id", unique: true
t.index ["site_id"], name: "index_sites_tags_on_site_id"
t.index ["tag_id"], name: "index_sites_tags_on_tag_id"
end

create_table "supporters", force: :cascade do |t|
t.string "name"
t.string "url"
Expand Down Expand Up @@ -368,4 +378,6 @@
add_foreign_key "service_areas", "neighbourhoods"
add_foreign_key "service_areas", "partners"
add_foreign_key "sites", "users", column: "site_admin_id"
add_foreign_key "sites_tags", "sites"
add_foreign_key "sites_tags", "tags"
end
8 changes: 3 additions & 5 deletions test/integration/admin/partner_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,9 @@ class PartnerIntegrationTest < ActionDispatch::IntegrationTest
get new_admin_partner_path(@partner)
assert_response :success

assert_select 'div.tags > fieldset.check_boxes' do |checkbox|
tag = assert_select checkbox, 'div.form-check', 1 # We have one tag
tag_options = assert_select 'div.partner_tags option', count: 1, text: @tag.name

assert_select tag, 'label', text: @tag.name
assert_select tag, 'input:match("checked", ?)', 'checked'
end
tag = tag_options.first
assert tag.attributes.key?('selected')
end
end
19 changes: 19 additions & 0 deletions test/integration/admin/sites_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
class AdminSitesIntegrationTest < ActionDispatch::IntegrationTest
setup do
@root = create(:root)

@site = create(:site)
@site_admin = @site.site_admin

@neighbourhoods = create_list(:neighbourhood, 5)
@number_of_neighbourhoods = Neighbourhood.all.length

@tag = create(:tag)

host! 'admin.lvh.me'
end

Expand Down Expand Up @@ -74,4 +79,18 @@ class AdminSitesIntegrationTest < ActionDispatch::IntegrationTest
neighbourhoods_shown = cocoon_select_template.scan(/(option value=)/).size
assert neighbourhoods_shown == 2
end

test 'site tags show up' do
@site.tags << @tag
@site_admin.tags << @tag

sign_in(@site_admin)
get edit_admin_site_path(@site)
assert_response :success

tag_options = assert_select 'div.site_tags option', count: 1, text: @tag.name

tag = tag_options.first
assert tag.attributes.key?('selected')
end
end

0 comments on commit d90451e

Please sign in to comment.