diff --git a/lib/administrate/field/has_one.rb b/lib/administrate/field/has_one.rb index 2e2f381944..81e03f41f5 100644 --- a/lib/administrate/field/has_one.rb +++ b/lib/administrate/field/has_one.rb @@ -4,7 +4,11 @@ module Administrate module Field class HasOne < Associative def self.permitted_attribute(resource_class, attr, deprecated_options: {}) - resource_path = deprecated_associated_class_name(resource_class, attr, deprecated_options: deprecated_options) + resource_path = deprecated_associated_class_name( + resource_class, + attr, + deprecated_options: deprecated_options, + ) related_dashboard_attributes = Administrate::ResourceResolver.new("admin/#{resource_path}"). dashboard_class.new.permitted_attributes + [:id] @@ -12,7 +16,11 @@ def self.permitted_attribute(resource_class, attr, deprecated_options: {}) { "#{attr}_attributes": related_dashboard_attributes } end - def self.deprecated_associated_class_name(resource_class, attr, deprecated_options: {}) + def self.deprecated_associated_class_name( + resource_class, + attr, + deprecated_options: {} + ) # TODO: deprecate :class_name option. After removal, remove this # method. Callers should use `associated_class_name` directly. deprecated_options.fetch(:class_name) do @@ -30,7 +38,11 @@ def nested_form private def resolver - resource_path = self.class.deprecated_associated_class_name(resource.class, attribute, deprecated_options: options) + resource_path = self.class.deprecated_associated_class_name( + resource.class, + attribute, + deprecated_options: options, + ) @resolver ||= Administrate::ResourceResolver.new("admin/#{resource_path}") end diff --git a/spec/lib/administrate/search_spec.rb b/spec/lib/administrate/search_spec.rb index 07e47f397d..88633cd0bc 100644 --- a/spec/lib/administrate/search_spec.rb +++ b/spec/lib/administrate/search_spec.rb @@ -72,91 +72,83 @@ class FooDashboard describe "#run" do it "returns all records when no search term" do - begin - class User < ApplicationRecord; end - scoped_object = User.default_scoped - search = Administrate::Search.new( - scoped_object, - Administrate::SearchSpecMocks::UserDashboard, - nil, - ) - expect(scoped_object).to receive(:all) - - search.run - ensure - remove_constants :User - end + class User < ApplicationRecord; end + scoped_object = User.default_scoped + search = Administrate::Search.new( + scoped_object, + Administrate::SearchSpecMocks::UserDashboard, + nil, + ) + expect(scoped_object).to receive(:all) + + search.run + ensure + remove_constants :User end it "returns all records when search is empty" do - begin - class User < ApplicationRecord; end - scoped_object = User.default_scoped - search = Administrate::Search.new( - scoped_object, - Administrate::SearchSpecMocks::UserDashboard, - " ", - ) - expect(scoped_object).to receive(:all) - - search.run - ensure - remove_constants :User - end + class User < ApplicationRecord; end + scoped_object = User.default_scoped + search = Administrate::Search.new( + scoped_object, + Administrate::SearchSpecMocks::UserDashboard, + " ", + ) + expect(scoped_object).to receive(:all) + + search.run + ensure + remove_constants :User end it "searches using LOWER + LIKE for all searchable fields" do - begin - class User < ApplicationRecord; end - scoped_object = User.default_scoped - search = Administrate::Search.new( - scoped_object, - Administrate::SearchSpecMocks::UserDashboard, - "test", - ) - expected_query = [ - [ - 'LOWER(CAST("users"."id" AS CHAR(256))) LIKE ?', - 'LOWER(CAST("users"."name" AS CHAR(256))) LIKE ?', - 'LOWER(CAST("users"."email" AS CHAR(256))) LIKE ?', - ].join(" OR "), - "%test%", - "%test%", - "%test%", - ] - expect(scoped_object).to receive(:where).with(*expected_query) - - search.run - ensure - remove_constants :User - end + class User < ApplicationRecord; end + scoped_object = User.default_scoped + search = Administrate::Search.new( + scoped_object, + Administrate::SearchSpecMocks::UserDashboard, + "test", + ) + expected_query = [ + [ + 'LOWER(CAST("users"."id" AS CHAR(256))) LIKE ?', + 'LOWER(CAST("users"."name" AS CHAR(256))) LIKE ?', + 'LOWER(CAST("users"."email" AS CHAR(256))) LIKE ?', + ].join(" OR "), + "%test%", + "%test%", + "%test%", + ] + expect(scoped_object).to receive(:where).with(*expected_query) + + search.run + ensure + remove_constants :User end it "converts search term LOWER case for latin and cyrillic strings" do - begin - class User < ApplicationRecord; end - scoped_object = User.default_scoped - search = Administrate::Search.new( - scoped_object, - Administrate::SearchSpecMocks::UserDashboard, - "Тест Test", - ) - expected_query = [ - [ - 'LOWER(CAST("users"."id" AS CHAR(256))) LIKE ?', - 'LOWER(CAST("users"."name" AS CHAR(256))) LIKE ?', - 'LOWER(CAST("users"."email" AS CHAR(256))) LIKE ?', - ].join(" OR "), - "%тест test%", - "%тест test%", - "%тест test%", - ] - expect(scoped_object).to receive(:where).with(*expected_query) - - search.run - ensure - remove_constants :User - end + class User < ApplicationRecord; end + scoped_object = User.default_scoped + search = Administrate::Search.new( + scoped_object, + Administrate::SearchSpecMocks::UserDashboard, + "Тест Test", + ) + expected_query = [ + [ + 'LOWER(CAST("users"."id" AS CHAR(256))) LIKE ?', + 'LOWER(CAST("users"."name" AS CHAR(256))) LIKE ?', + 'LOWER(CAST("users"."email" AS CHAR(256))) LIKE ?', + ].join(" OR "), + "%тест test%", + "%тест test%", + "%тест test%", + ] + expect(scoped_object).to receive(:where).with(*expected_query) + + search.run + ensure + remove_constants :User end context "when searching through associations" do @@ -208,26 +200,24 @@ class User < ApplicationRecord; end end it "searches using a filter" do - begin - class User < ActiveRecord::Base - scope :vip, -> { where(kind: :vip) } - end - scoped_object = User.default_scoped - search = Administrate::Search.new( - scoped_object, - Administrate::SearchSpecMocks::UserDashboard, - "vip:", - ) - expect(scoped_object).to \ - receive(:where). - with(kind: :vip). - and_return(scoped_object) - expect(scoped_object).to receive(:where).and_return(scoped_object) - - search.run - ensure - remove_constants :User + class User < ApplicationRecord + scope :vip, -> { where(kind: :vip) } end + scoped_object = User.default_scoped + search = Administrate::Search.new( + scoped_object, + Administrate::SearchSpecMocks::UserDashboard, + "vip:", + ) + expect(scoped_object).to \ + receive(:where). + with(kind: :vip). + and_return(scoped_object) + expect(scoped_object).to receive(:where).and_return(scoped_object) + + search.run + ensure + remove_constants :User end end end diff --git a/spec/lib/fields/belongs_to_spec.rb b/spec/lib/fields/belongs_to_spec.rb index 1a87861514..9f588ea6d4 100644 --- a/spec/lib/fields/belongs_to_spec.rb +++ b/spec/lib/fields/belongs_to_spec.rb @@ -85,7 +85,9 @@ :show, resource: line_item, ) - expect(field.display_associated_resource).to match(/^Line Item \#\d\d\d\d$/) + expect(field.display_associated_resource).to match( + /^Line Item \#\d\d\d\d$/, + ) end end