Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024 Updates #58

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 2.7.2
86 changes: 86 additions & 0 deletions app/assets/javascripts/addSlideInSlideLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Adds event listener for the 'spree:load' event, triggers actions on 'new_add_slide' click.
*/
document.addEventListener('spree:load', function () {
document
.getElementById('new_add_slide')
.addEventListener('click', function () {
loadAutoComplete();
removeExistingSelects();
updateDefaultValueInFields();
});
});

/**
* Removes existing Select2 instances and image from previous clone element.
*/
function removeExistingSelects() {
const tbodyTr = document.querySelector('tbody tr:last-child');

if (tbodyTr) {
const select2Elements = tbodyTr.querySelectorAll(
'.select2[data-select2-id]'
);
const imageContainer = tbodyTr.querySelector('.image-container');

if (select2Elements.length > 1) {
for (let i = 1; i < select2Elements.length; i++) {
select2Elements[i].remove();
}
}

if (imageContainer) {
imageContainer.remove();
}
}
}

/**
* Loads autocomplete for the last table row.
*/
function loadAutoComplete() {
const tbodyTr = document.querySelector('tbody tr:last-child');

// Add class and unique ID to the last table row
tbodyTr.classList.add('new-slide');
tbodyTr.id = 'new_spree_slide_' + (Number(tbodyTr.rowIndex) + 1);

const slideItemsList = document.querySelectorAll('tbody .new-slide');

if (slideItemsList) {
slideItemsList.forEach((slideItem) => {
const loadParamsElement = slideItem.querySelector(
'select[data-autocomplete-url-value]'
);
// Update data-select2-id to match the element ID
loadParamsElement.setAttribute('data-select2-id', loadParamsElement.id);
// update label for attribute
loadParamsElement.previousElementSibling.setAttribute(
'for',
loadParamsElement.id
);
buildParamsFromDataAttrs(loadParamsElement);
});
}
}

/**
* Updates default values in input fields of the last table row.
*/
function updateDefaultValueInFields() {
const tbodyTr = document.querySelector('tbody tr:last-child');
const positionField = tbodyTr.querySelector(
'input[id^="slide_location_slides_attributes_"][id$="_position"]'
);
const bodyField = tbodyTr.querySelector(
'textarea[id^="slide_location_slides_attributes_"][id$="_body"]'
);
const publishedField = tbodyTr.querySelector(
'input[id^="slide_location_slides_attributes_"][id$="published"]'
);

// Update default values
positionField.value = 0;
bodyField.innerHTML = '';
publishedField.checked = false;
}
39 changes: 39 additions & 0 deletions app/assets/javascripts/productSortable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Added function for multiple table

document.addEventListener('spree:load', function () {
const parentEl = document.getElementsByClassName('sortable')[1];
let element;

if (parentEl) {
element = parentEl.querySelector('tbody');
}

if (element) {
Sortable.create(element, {
handle: '.move-handle',
animation: 550,
ghostClass: 'bg-light',
dragClass: 'sortable-drag-v',
easing: 'cubic-bezier(1, 0, 0, 1)',
swapThreshold: 0.9,
forceFallback: true,
onEnd: function (evt) {
const itemEl = evt.item;
const positions = { authenticity_token: AUTH_TOKEN };
$.each($('tr', element), function (position, obj) {
const reg = /spree_(\w+_?)+_(.*)/;
const parts = reg.exec($(obj).prop('id'));
if (parts) {
positions['positions[' + parts[2] + ']'] = position + 1;
}
});
$.ajax({
type: 'POST',
dataType: 'json',
url: $(itemEl).closest('table.sortable').data('sortable-link'),
data: positions,
});
},
});
}
});
16 changes: 16 additions & 0 deletions app/controllers/spree/admin/slide_locations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,26 @@ module Spree
module Admin
class SlideLocationsController < ResourceController
respond_to :html
before_action :setup_new_slides_value, only: :edit

def update_slide_positions
ApplicationRecord.transaction do
params[:positions].each do |id, index|
Spree::Slide.where(id: id).update_all(position: index)
end
end
end

def index
@slide_locations = Spree::SlideLocation.order(:name)
end

private

def setup_new_slides_value
@slide_location.slides.build if @slide_location.slides.empty?
end

end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Spree
module Api
module V2
module Storefront
class SlideLocationsController < ::Spree::Api::V2::BaseController
def show
render_serialized_payload { serialize_resource(resource) }
end

private

def scope
Spree::SlideLocation
end

def resource
scope.find(params[:id])
end

def resource_serializer
Spree::V2::Storefront::SlideLocationSerializer
end
end
end
end
end
end
14 changes: 14 additions & 0 deletions app/helpers/spree/admin/slides_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ def get_image_link_by_type(slide, type)

return '----'
end

def product_options_for_select(slide)
if slide.product_id.present?
[slide.product]
else
[]
end

end

def product_url(slug)
"/products/#{slug}"
end

end
end
end
13 changes: 12 additions & 1 deletion app/models/spree/slide.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
class Spree::Slide < ActiveRecord::Base
include Spree::Admin::SlidesHelper
include Rails.application.routes.url_helpers


has_and_belongs_to_many :slide_locations,
class_name: 'Spree::SlideLocation',
join_table: 'spree_slide_slide_locations'

belongs_to :product, touch: true, optional: true

acts_as_list

has_one_attached :image

validates :link_url, presence: true, url: true, unless: -> { product }
Expand All @@ -31,7 +37,7 @@ def slide_name
end

def slide_link
link_url.blank? && product.present? ? product : link_url
link_url.blank? && product.present? ? product_url(product.slug) : link_url
end

def slide_image
Expand All @@ -47,6 +53,11 @@ def thumbnail
image_form(:thumbnail)
end

def slide_image_url
# For Api
rails_blob_path(image, only_path: true)
end

private

def image_form(form)
Expand Down
2 changes: 2 additions & 0 deletions app/models/spree/slide_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Spree::SlideLocation < ActiveRecord::Base
has_and_belongs_to_many :slides,
class_name: 'Spree::Slide',
join_table: 'spree_slide_slide_locations'

accepts_nested_attributes_for :slides

validates :name, presence: true
end
26 changes: 26 additions & 0 deletions app/models/spree_slider/admin/main_menu/slider_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module SpreeSlider
module Admin
module MainMenu
class SliderBuilder
include ::Spree::Core::Engine.routes.url_helpers

def build
items = [
Spree::Admin::MainMenu::ItemBuilder.new('spree_slider.config_name', admin_slides_path).
with_availability_check(->(ability, _store) { ability.can?(:manage, ::Spree::Slide) && ability.can?(:index, ::Spree::Slide) }).
with_match_path('/slides').
build,
Spree::Admin::MainMenu::ItemBuilder.new('spree_slider_locations.config_name', admin_slide_locations_path).
with_availability_check(->(ability, _store) { ability.can?(:manage, ::Spree::SlideLocation) }).
with_match_path('/slide_locations').
build
]

Spree::Admin::MainMenu::SectionBuilder.new('slides', 'file-slides-fill.svg').
with_items(items).
build
end
end
end
end
end

This file was deleted.

This file was deleted.

13 changes: 13 additions & 0 deletions app/serializers/spree/v2/storefront/slide_location_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Spree
module V2
module Storefront
class SlideLocationSerializer < BaseSerializer
set_type :slide_location

attributes :name

has_many :slides
end
end
end
end
12 changes: 12 additions & 0 deletions app/serializers/spree/v2/storefront/slide_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Spree
module V2
module Storefront
class SlideSerializer < BaseSerializer
set_type :slide

attributes :name, :body, :slide_name, :slide_link, :slide_image_url

end
end
end
end
2 changes: 2 additions & 0 deletions app/validators/url_validator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'addressable/uri'

class UrlValidator < ActiveModel::EachValidator

def validate_each(record, attribute, value)
Expand Down
64 changes: 64 additions & 0 deletions app/views/spree/admin/slide_locations/_slide_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<tr class="slide px-0" id="spree_<%= dom_id(f.object) %>" data-hook>
<td class="px-0 w-100">
<div class="w-100">
<div class="move-handle d-flex justify-content-between align-items-center px-3">
<%= svg_icon name: "grip-vertical.svg", width: '18', height: '18' %>
<%= link_to_icon_remove_fields f %>
</div>
<hr/>
<div class="row px-3">
<div class="col-md-3">
<%= f.label :position, t(:position) %>
<%= f.number_field :position, class: 'fullwidth form-control' %>
</div>
<div class="col-md-2 d-flex align-items-center mt-2">
<div class="field checkbox d-flex align-items-center mt-4">
<%= f.check_box :published, class: "w-auto mr-2 cursor-pointer" %>
<%= f.label :published, t(:published), class: 'mb-0' %>
</div>
</div>
</div>
<hr/>
<div class="row px-3">
<div class="col-md-12">
<%= f.label :product_id, Spree.t(:product) %>
<%= f.select :product_id, options_from_collection_for_select(f.object.product.present? ? [f.object.product] : [], :id, :name, f.object.product_id.present? ? f.object.product_id : nil), { include_hidden: true }, data: { autocomplete_url_value: 'products_api_v2', autocomplete_return_attr_value: 'name'} %>
</div>
</div>
<hr/>
<div class="row px-3">
<div class="col-md-6">
<%= f.label :name, t(:name) %><br>
<%= f.text_field :name, class: 'fullwidth form-control', placeholder: t('spree_slider.placeholder_name') %>
</div>
<div class="col-md-6">
<%= f.label :link_url, t(:link_url) %><br>
<%= f.text_field :link_url, class: 'fullwidth form-control', placeholder: t('spree_slider.placeholder_link_url') %>
</div>
</div>
<hr/>
<div class="row px-3">
<div class="col-md-12">
<%= f.label :body, t(:body) %><br>
<%= f.text_area :body, { cols: 60, rows: 4, class: 'fullwidth form-control' } %>
</div>
</div>
<hr/>
<div class="row px-3">
<div class="col-md-6">
<%= f.label :image, t(:image) %> <i>(<%= t('spree_slider.image_tip') %>)</i><br>
<%= f.file_field :image %>
</div>
<div class="col-md-6 image-container">
<% if f.object.image.attached? %>
<p>
<%= label_tag t(:preview) %>
<br>
<%= image_tag main_app.url_for(f.object.thumbnail), class: 'img-responsive' %>
</p>
<% end %>
</div>
</div>
</div>
</td>
</tr>
Loading