Skip to content

Commit

Permalink
Merge pull request #1062 from texpert/master
Browse files Browse the repository at this point in the history
Fix JS after conversion from CoffeeScript
  • Loading branch information
texpert authored Dec 8, 2023
2 parents 03d9745 + 73abd3d commit 71c864d
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 61 deletions.
37 changes: 18 additions & 19 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-02-08 21:38:48 UTC using RuboCop version 1.44.1.
# on 2023-11-12 20:41:57 UTC using RuboCop version 1.56.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 4
# Offense count: 3
# Configuration parameters: AllowedMethods.
# AllowedMethods: enums
Lint/ConstantDefinitionInBlock:
Exclude:
- 'app/models/concerns/camaleon_cms/user_methods.rb'
- 'config/initializers/active_record_extension.rb'
- 'config/initializers/model_alias.rb'

# Offense count: 3
Expand All @@ -32,13 +31,13 @@ Lint/SuppressedException:
Lint/UnderscorePrefixedVariableName:
Enabled: false

# Offense count: 169
# Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods, CountRepeatedAttributes.
# Offense count: 168
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 143

# Offense count: 18
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, AllowedMethods, AllowedPatterns, IgnoredMethods, inherit_mode.
# Offense count: 17
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode.
# AllowedMethods: refine
Metrics/BlockLength:
Max: 125
Expand All @@ -48,18 +47,18 @@ Metrics/BlockLength:
Metrics/BlockNesting:
Max: 5

# Offense count: 20
# Offense count: 21
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 369

# Offense count: 53
# Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods.
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/CyclomaticComplexity:
Max: 62

# Offense count: 145
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, AllowedMethods, AllowedPatterns, IgnoredMethods.
# Offense count: 146
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Max: 335

Expand All @@ -74,8 +73,8 @@ Metrics/ParameterLists:
MaxOptionalParameters: 7
Max: 8

# Offense count: 45
# Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods.
# Offense count: 46
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/PerceivedComplexity:
Max: 63

Expand All @@ -95,6 +94,7 @@ Naming/AccessorMethodName:
- 'lib/plugin_routes.rb'

# Offense count: 8
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyleForLeadingUnderscores.
# SupportedStylesForLeadingUnderscores: disallowed, required, optional
Naming/MemoizedInstanceVariableName:
Expand All @@ -107,7 +107,7 @@ Naming/MemoizedInstanceVariableName:
- 'app/models/concerns/camaleon_cms/user_methods.rb'

# Offense count: 10
# Configuration parameters: EnforcedStyle, AllowedPatterns, IgnoredPatterns.
# Configuration parameters: EnforcedStyle, AllowedPatterns.
# SupportedStyles: snake_case, camelCase
Naming/MethodName:
Exclude:
Expand Down Expand Up @@ -151,7 +151,7 @@ Naming/PredicateName:
# Offense count: 5
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns.
# SupportedStyles: snake_case, normalcase, non_integer
# AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339
# AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64
Naming/VariableNumber:
Exclude:
- 'app/controllers/camaleon_cms/frontend_controller.rb'
Expand Down Expand Up @@ -196,13 +196,12 @@ Style/GuardClause:
- 'app/validators/camaleon_cms/post_uniq_validator.rb'
- 'app/validators/camaleon_cms/uniq_validator.rb'

# Offense count: 33
# Offense count: 32
# This cop supports safe autocorrection (--autocorrect).
Style/IfUnlessModifier:
Exclude:
- 'app/apps/plugins/visibility_post/visibility_post_helper.rb'
- 'app/controllers/camaleon_cms/admin/appearances/widgets/sidebar_controller.rb'
- 'app/controllers/camaleon_cms/frontend_controller.rb'
- 'app/helpers/camaleon_cms/admin/menus_helper.rb'
- 'app/helpers/camaleon_cms/admin/post_type_helper.rb'
- 'app/helpers/camaleon_cms/comment_helper.rb'
Expand All @@ -219,9 +218,9 @@ Style/IfUnlessModifier:
Style/OptionalBooleanParameter:
Enabled: false

# Offense count: 277
# Offense count: 261
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns.
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 2326
13 changes: 7 additions & 6 deletions app/assets/javascripts/camaleon_cms/admin/_posttype.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,29 @@ window.cama_init_posttype_form = function() {

item.parent().siblings().find('input').prop('disabled', $(this).is(':checked'))
if ($(this).is(':checked'))
return item.prop('checked', true).prop('disabled', false)
item.prop('checked', true).prop('disabled', false)
else
return item.prop('disabled', true)
item.prop('disabled', true)
}).trigger('change')

form.find('[name="meta[has_picture]"]').change(function() {
const items = form.find('.picture_settings input')

if ($(this).is(':checked'))
return items.prop('disabled', false)
items.prop('disabled', false)
else
return items.prop('disabled', true)
items.prop('disabled', true)
}).trigger('change')

// toggle single and multiple categories checkbox
const catChecks = form.find(
'input:checkbox[name="meta[has_category]"], input:checkbox[name="meta[has_single_category]"]'
)

return catChecks.change(
catChecks.change(
function() {
if ($(this).is(':checked')) return catChecks.not(this).prop('checked', false)
if ($(this).is(':checked'))
catChecks.not(this).prop('checked', false)
}
).filter(':checked').trigger('change')
}
49 changes: 20 additions & 29 deletions app/assets/javascripts/camaleon_cms/admin/nav_menu.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/

/* eslint-env jquery */
$(function() {
const panel = $('#menu_content')
Expand All @@ -21,25 +15,22 @@ $(function() {
return

panel.find('#menu_reoreder_loading').show()
return $.post(listPanel.attr('data-reorder_url'), data, function(res) {
$.post(listPanel.attr('data-reorder_url'), data, function(res) {
lastData = data
panel.find('#menu_reoreder_loading').hide()
if (res)
return alert(res)
alert(res)
})
})

// save a new menu item into DB
const saveMenu = function(data) {
showLoading()

return $.post(
listPanel.attr('data-url'),
data,
function(res) {
listPanel.children('.dd-list').append($(res).children())
return hideLoading()
})
$.post(listPanel.attr('data-url'), data, function(res) {
listPanel.children('.dd-list').append($(res).children())
hideLoading()
})
}

// add menu items (non-external)
Expand All @@ -48,7 +39,7 @@ $(function() {
let flag = false
$(this).closest('.panel').find('input:checkbox:checked').each(function() {
flag = true
return data.items.push({ id: $(this).val(), kind: $(this).closest('.class_type').attr('data-type') })
data.items.push({ id: $(this).val(), kind: $(this).closest('.class_type').attr('data-type') })
}).prop('checked', false)

if (!flag)
Expand All @@ -64,7 +55,7 @@ $(function() {
let flag = false
$(this).closest('.panel').find('input:checkbox:checked').each(function() {
flag = true
return data.custom_items.push({ url: $(this).val(), kind: $(this).attr('data-kind'), label: $(this).attr('data-label') })
data.custom_items.push({ url: $(this).val(), kind: $(this).attr('data-kind'), label: $(this).attr('data-label') })
}).prop('checked', false)

if (!flag)
Expand Down Expand Up @@ -93,18 +84,18 @@ $(function() {
title: link.attr('data-original-title') || link.attr('title'),
url: link.attr('href'),
mode: 'ajax',
callback(modal) {
callback: (modal) => {
const form = modal.find('form')
init_form_validations(form)
return form.submit(function() {
form.submit(function() {
if (!form.valid())
return false

showLoading()
$.post(form.attr('action'), form.serialize(), function(res) {
link.closest('li').replaceWith($(res).html())
modal.modal('hide')
return hideLoading()
hideLoading()
})
return false
})
Expand All @@ -122,39 +113,39 @@ $(function() {
showLoading()
$.get(link.attr('href'), function() {
link.closest('.dd-item').remove()
return hideLoading()
hideLoading()
})
return false
})

// new menu
panel.find('.new_menu_link, .edit_menu_link').ajax_modal({
callback(modal) {
callback: (modal) => {
const form = modal.find('form')

return setTimeout(() => init_form_validations(form), 1000)
setTimeout(() => init_form_validations(form), 1000)
}
})

// menus list - change dropdown
panel.find('#menu_items #switch_nav_menuForm select').change(function() {
panel.find('#menu_items #switch_nav_menu_form select').change(function() {
if (!$(this).val())
return

return $(this).closest('form').submit()
$(this).closest('form').submit()
})

// custom fields
return listPanel.on('click', '.custom_settings_link', function() {
listPanel.on('click', '.custom_settings_link', function() {
const link = $(this)
open_modal({
title: link.attr('data-original-title') || link.attr('title'),
url: link.attr('href'),
mode: 'ajax',
callback(modal) {
callback: (modal) => {
const form = modal.find('form')
init_form_validations(form)
return form.submit(function() {
form.submit(function() {
if (!form.valid())
return false

Expand All @@ -164,7 +155,7 @@ $(function() {
alert(res)

modal.modal('hide')
return hideLoading()
hideLoading()
})
return false
})
Expand Down
3 changes: 2 additions & 1 deletion app/decorators/camaleon_cms/category_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def the_categories
# return a child category from this category with id (integer) or by slug (string)
def the_category(slug_or_id)
return object.categories.where(id: slug_or_id).first if slug_or_id.is_a?(Integer)
return object.categories.find_by_slug(slug_or_id) if slug_or_id.is_a?(String)

object.categories.find_by_slug(slug_or_id) if slug_or_id.is_a?(String)
end

# ---------------------
Expand Down
3 changes: 2 additions & 1 deletion app/decorators/camaleon_cms/post_type_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def the_full_categories
# return a category from this post_type with id (integer) or by slug (string)
def the_category(slug_or_id)
return the_categories.where(id: slug_or_id).first if slug_or_id.is_a?(Integer)
return the_categories.find_by_slug(slug_or_id) if slug_or_id.is_a?(String)

the_categories.find_by_slug(slug_or_id) if slug_or_id.is_a?(String)
end

# return all post_tags for the post_type (active_record) filtered by permissions + hidden posts + roles + etc...
Expand Down
3 changes: 2 additions & 1 deletion app/decorators/camaleon_cms/site_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def the_post(slug_or_id)
# slug_or_id: string => return all main categories of the post_type with slug = slug_or_id
def the_categories(slug_or_id = nil)
return the_post_type(slug_or_id).the_categories if slug_or_id.present?
return object.categories unless slug_or_id.present?

object.categories unless slug_or_id.present?
end

# return the category object with id or slug = slug_or_id from this site
Expand Down
7 changes: 4 additions & 3 deletions app/validators/camaleon_cms/post_uniq_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ def validate(record)
return unless ptype.present? # only for posts that belongs to a post type model

posts = ptype.site.posts
.where("(#{slug_array.map do |s|
"#{CamaleonCms::Post.table_name}.slug LIKE '%-->#{s}<!--%'"
end.join(' OR ')} ) OR #{CamaleonCms::Post.table_name}.slug = ?", record.slug)
.where(
"(#{slug_array.map { |s| "#{CamaleonCms::Post.table_name}.slug LIKE '%-->#{s}<!--%'" }
.join(' OR ')} ) OR #{CamaleonCms::Post.table_name}.slug = ?", record.slug
)
.where.not(id: record.id)
.where.not(status: %i[draft draft_child trash])
if posts.size.positive?
Expand Down
42 changes: 41 additions & 1 deletion spec/features/admin/menus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,47 @@
end
end

it 'Menus Create and Delete' do
it 'creates and selects menus' do
admin_sign_in
visit "#{cama_root_relative_path}/admin/appearances/nav_menus"

page.execute_script('jQuery("#switch_nav_menu_form .new_menu_link").click()')

within '#ow_inline_modal' do
fill_in 'nav_menu_name', with: 'Second menu'
fill_in 'nav_menu_slug', with: 'second-menu'
click_button 'Create Menu'
wait_for_ajax
end

within '#menu_items' do
select('Second menu', from: 'menu')

check('Sample Post')
page.execute_script('$("#acc-post input").prop("checked", true)')
page.execute_script('$("#acc-post").prev().find("input").prop("checked", true)')
page.execute_script('$("#acc-post .add_links_to_menu").click()')
wait_for_ajax

select('Main Menu', from: 'menu')
wait_for_ajax
end

within '#menus_list' do
find('.dd-item', text: 'Welcome')
end

within '#menu_items' do
select('Second menu', from: 'menu')
wait_for_ajax
end

within '#menus_list' do
find('.dd-item', text: 'Uncategorized')
end
end

it 'deletes menus' do
admin_sign_in
visit "#{cama_root_relative_path}/admin/appearances/nav_menus"
page.execute_script('jQuery("#switch_nav_menu_form .btn-danger").click()')
Expand Down

0 comments on commit 71c864d

Please sign in to comment.