From b3bb44cce15212790a4cd310db8ca84f0a27f99a Mon Sep 17 00:00:00 2001 From: Nelson Saloj <110138549+nsaloj@users.noreply.github.com> Date: Wed, 20 Dec 2023 15:24:22 -0600 Subject: [PATCH 1/2] Fix humanization for add nested element button Converting `nestable_element` to a symbol allows us to humanize the nestable element name. (cherry picked from commit 1281ae795899cafe39de9db6823b1da3c23aaa65) --- .../admin/elements/_add_nested_element_form.html.erb | 10 +++++----- spec/features/admin/edit_elements_feature_spec.rb | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/views/alchemy/admin/elements/_add_nested_element_form.html.erb b/app/views/alchemy/admin/elements/_add_nested_element_form.html.erb index 6f49ce4b79..b04270c092 100644 --- a/app/views/alchemy/admin/elements/_add_nested_element_form.html.erb +++ b/app/views/alchemy/admin/elements/_add_nested_element_form.html.erb @@ -1,7 +1,7 @@ <%= content_tag :div, class: 'add-nested-element', data: { element_id: element.id } do %> <% if element.expanded? || element.fixed? %> - <% if element.nestable_elements.length == 1 && - (nestable_element = element.nestable_elements.first) && + <% if element.nestable_elements.length == 1 && + (nestable_element = element.nestable_elements.first) && Alchemy::Element.all_from_clipboard_for_parent_element(get_clipboard("elements"), element).none? %> <%= form_for [:admin, Alchemy::Element.new(name: nestable_element)], @@ -10,11 +10,11 @@ <%= f.hidden_field :page_version_id, value: element.page_version_id %> <%= f.hidden_field :parent_element_id, value: element.id %> <% end %> <% else %> - <%= link_to_dialog (nestable_element ? Alchemy.t(:add_nested_element, name: Alchemy.t(nestable_element, scope: 'element_names')) : Alchemy.t("New Element")), + <%= link_to_dialog (nestable_element ? Alchemy.t(:add_nested_element, name: Alchemy.t(nestable_element.to_sym, scope: 'element_names')) : Alchemy.t("New Element")), alchemy.new_admin_element_path( parent_element_id: element.id, page_version_id: element.page_version_id @@ -24,4 +24,4 @@ }, class: "button add-nestable-element-button" %> <% end %> <% end %> -<% end %> \ No newline at end of file +<% end %> diff --git a/spec/features/admin/edit_elements_feature_spec.rb b/spec/features/admin/edit_elements_feature_spec.rb index e809544575..a7464a99bb 100644 --- a/spec/features/admin/edit_elements_feature_spec.rb +++ b/spec/features/admin/edit_elements_feature_spec.rb @@ -65,7 +65,7 @@ scenario "the add button opens add element form with the clipboard tab" do visit alchemy.admin_elements_path(page_version_id: element.page_version_id) button = page.find(".add-nestable-element-button") - expect(button).to have_content "Add slide" + expect(button).to have_content "Add Slide" button.click expect(page).to have_select("Element") expect(page).to have_link("Paste from clipboard") @@ -76,7 +76,7 @@ scenario "the add element button immediately creates the nested element." do visit alchemy.admin_elements_path(page_version_id: element.page_version_id) button = page.find("button.add-nestable-element-button") - expect(button).to have_content "Add slide" + expect(button).to have_content "Add Slide" button.click expect(page).to have_selector(".element-editor[data-element-name='slide']") end From c1426ba81a76730e5cb421d0ec4ce6255d0d9cac Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Thu, 28 Dec 2023 13:52:09 +0100 Subject: [PATCH 2/2] Fix latest Rubocop performance offenses Symbols should be used to `send` or `define_method`s. --- app/controllers/alchemy/admin/resources_controller.rb | 10 +++++----- app/models/alchemy/element/element_ingredients.rb | 4 ++-- app/models/alchemy/ingredient.rb | 6 +++--- app/models/alchemy/ingredient_validator.rb | 4 ++-- app/models/alchemy/page.rb | 2 +- app/models/concerns/alchemy/picture_thumbnails.rb | 2 +- lib/alchemy/resource.rb | 4 ++-- lib/alchemy/resources_helper.rb | 6 +++--- spec/features/admin/node_select_feature_spec.rb | 4 ++-- 9 files changed, 21 insertions(+), 21 deletions(-) diff --git a/app/controllers/alchemy/admin/resources_controller.rb b/app/controllers/alchemy/admin/resources_controller.rb index d8813ff9d2..430fb21c6c 100644 --- a/app/controllers/alchemy/admin/resources_controller.rb +++ b/app/controllers/alchemy/admin/resources_controller.rb @@ -34,16 +34,16 @@ def index respond_to do |format| format.html do items = items.page(params[:page] || 1).per(items_per_page) - instance_variable_set("@#{resource_handler.resources_name}", items) + instance_variable_set(:"@#{resource_handler.resources_name}", items) end format.csv do - instance_variable_set("@#{resource_handler.resources_name}", items) + instance_variable_set(:"@#{resource_handler.resources_name}", items) end end end def new - instance_variable_set("@#{resource_handler.resource_name}", resource_handler.model.new) + instance_variable_set(:"@#{resource_handler.resource_name}", resource_handler.model.new) end def show @@ -54,7 +54,7 @@ def edit end def create - instance_variable_set("@#{resource_handler.resource_name}", resource_handler.model.new(resource_params)) + instance_variable_set(:"@#{resource_handler.resource_name}", resource_handler.model.new(resource_params)) resource_instance_variable.save render_errors_or_redirect( resource_instance_variable, @@ -169,7 +169,7 @@ def alchemy_module end def load_resource - instance_variable_set("@#{resource_handler.resource_name}", resource_handler.model.find(params[:id])) + instance_variable_set(:"@#{resource_handler.resource_name}", resource_handler.model.find(params[:id])) end def authorize_resource diff --git a/app/models/alchemy/element/element_ingredients.rb b/app/models/alchemy/element/element_ingredients.rb index 4dd0c5d1f3..6cd581971b 100644 --- a/app/models/alchemy/element/element_ingredients.rb +++ b/app/models/alchemy/element/element_ingredients.rb @@ -158,8 +158,8 @@ def ingredient_error_messages "#{name}.#{role}.#{error}", scope: "ingredient_validations", default: [ - "fields.#{role}.#{error}".to_sym, - "errors.#{error}".to_sym + :"fields.#{role}.#{error}", + :"errors.#{error}" ], field: Alchemy::Ingredient.translated_label_for(role, name) ) diff --git a/app/models/alchemy/ingredient.rb b/app/models/alchemy/ingredient.rb index 7228abe8e0..c29504902c 100644 --- a/app/models/alchemy/ingredient.rb +++ b/app/models/alchemy/ingredient.rb @@ -45,14 +45,14 @@ class << self # @param [String] The class name of the related object def related_object_alias(name, class_name:) alias_method name, :related_object - alias_method "#{name}=", :related_object= + alias_method :"#{name}=", :related_object= # Somehow Rails STI does not allow us to use `alias_method` for the related_object_id - define_method "#{name}_id" do + define_method :"#{name}_id" do related_object_id end - define_method "#{name}_id=" do |id| + define_method :"#{name}_id=" do |id| self.related_object_id = id self.related_object_type = class_name end diff --git a/app/models/alchemy/ingredient_validator.rb b/app/models/alchemy/ingredient_validator.rb index e7a7689f96..4822739b2c 100644 --- a/app/models/alchemy/ingredient_validator.rb +++ b/app/models/alchemy/ingredient_validator.rb @@ -51,10 +51,10 @@ def validate(ingredient) validations.each do |validation| if validation.respond_to?(:keys) validation.map do |key, value| - send("validate_#{key}", value) + send(:"validate_#{key}", value) end else - send("validate_#{validation}") + send(:"validate_#{validation}") end end end diff --git a/app/models/alchemy/page.rb b/app/models/alchemy/page.rb index 6493bb3db1..4b4e00a51c 100644 --- a/app/models/alchemy/page.rb +++ b/app/models/alchemy/page.rb @@ -581,7 +581,7 @@ def menus def set_fixed_attributes fixed_attributes.all.each do |attribute, value| - send("#{attribute}=", value) + send(:"#{attribute}=", value) end end diff --git a/app/models/concerns/alchemy/picture_thumbnails.rb b/app/models/concerns/alchemy/picture_thumbnails.rb index 2f6477952f..ef3a234e81 100644 --- a/app/models/concerns/alchemy/picture_thumbnails.rb +++ b/app/models/concerns/alchemy/picture_thumbnails.rb @@ -168,7 +168,7 @@ def inferred_dimensions_from_string(string) def fix_crop_values %i[crop_from crop_size].each do |crop_value| if public_send(crop_value).is_a?(String) - public_send("#{crop_value}=", normalize_crop_value(crop_value)) + public_send(:"#{crop_value}=", normalize_crop_value(crop_value)) end end end diff --git a/lib/alchemy/resource.rb b/lib/alchemy/resource.rb index b8a7ec4602..7cb6bc7448 100644 --- a/lib/alchemy/resource.rb +++ b/lib/alchemy/resource.rb @@ -108,7 +108,7 @@ class Resource def initialize(controller_path, module_definition = nil, custom_model = nil) @controller_path = controller_path @module_definition = module_definition - @model = (custom_model || guess_model_from_controller_path) + @model = custom_model || guess_model_from_controller_path if model.respond_to?(:alchemy_resource_relations) if !model.respond_to?(:reflect_on_all_associations) raise MissingActiveRecordAssociation @@ -320,7 +320,7 @@ def map_relations model.alchemy_resource_relations.each do |name, options| relation_name = name.to_s.gsub(/_id$/, "") # ensure that we don't have an id association = association_from_relation_name(relation_name) - foreign_key = association.options[:foreign_key] || "#{association.name}_id".to_sym + foreign_key = association.options[:foreign_key] || :"#{association.name}_id" collection = options[:collection] || resource_relation_class(association).all resource_relations[foreign_key] = options.merge( model_association: association, diff --git a/lib/alchemy/resources_helper.rb b/lib/alchemy/resources_helper.rb index ff919143b3..6ae7aa3cff 100644 --- a/lib/alchemy/resources_helper.rb +++ b/lib/alchemy/resources_helper.rb @@ -14,11 +14,11 @@ def resource_window_size end def resource_instance_variable - instance_variable_get("@#{resource_handler.resource_name}") + instance_variable_get(:"@#{resource_handler.resource_name}") end def resources_instance_variable - instance_variable_get("@#{resource_handler.resources_name}") + instance_variable_get(:"@#{resource_handler.resources_name}") end def resource_url_proxy @@ -46,7 +46,7 @@ def new_resource_path(options = {}) end def edit_resource_path(resource = nil, options = {}) - path_segments = (resource_scope + [resource] || resource_handler.resource_array) + path_segments = resource_scope + [resource] || resource_handler.resource_array edit_polymorphic_path path_segments, options end diff --git a/spec/features/admin/node_select_feature_spec.rb b/spec/features/admin/node_select_feature_spec.rb index 9861cd73a6..6a8c375c83 100644 --- a/spec/features/admin/node_select_feature_spec.rb +++ b/spec/features/admin/node_select_feature_spec.rb @@ -16,8 +16,8 @@ %w[english klingon].each do |language| context language do - let(:element) { send "#{language}_element" } - let(:node) { send "#{language}_node" } + let(:element) { send :"#{language}_element" } + let(:node) { send :"#{language}_node" } it "restricts to the site/language of the page the element is on" do visit alchemy.admin_elements_path(page_version_id: element.page_version_id)