Skip to content

Commit

Permalink
Merge branch 'master' into sidescroll
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed Nov 20, 2021
2 parents e660464 + f59b32d commit c6bd734
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 12 deletions.
8 changes: 8 additions & 0 deletions app/assets/javascripts/rails_admin/ra.filter-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
)
);
break;
case "citext":
case "string":
case "text":
case "belongs_to_association":
Expand All @@ -180,6 +181,13 @@
.prop("selected", field_operator == "like")
.text(RailsAdmin.I18n.t("contains"))
)
.append(
$(
'<option data-additional-fieldset="additional-fieldset" value="not_like"></option>'
)
.prop("selected", field_operator == "not_like")
.text(RailsAdmin.I18n.t("does_not_contain"))
)
.append(
$(
'<option data-additional-fieldset="additional-fieldset" value="is"></option>'
Expand Down
5 changes: 4 additions & 1 deletion app/assets/javascripts/rails_admin/ra.nested-form-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
content = parent_group.children(".tab-content");
toggler = controls.find(".toggler");
nav.append(new_tab);
$(window.document).trigger("rails_admin.dom_ready", [field, parent_group]);

const event = new CustomEvent("rails_admin.dom_ready", { detail: field });
document.dispatchEvent(event);

new_tab.children("a").tab("show");
if (!one_to_one) {
nav.filter(":hidden").show("slow");
Expand Down
3 changes: 2 additions & 1 deletion app/assets/javascripts/rails_admin/ra.remote-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@
})
.html(saveButtonText);

$(document).trigger("rails_admin.dom_ready", [form]);
const event = new CustomEvent("rails_admin.dom_ready", { detail: form });
document.dispatchEvent(event);

form.bind("ajax:complete", function (event) {
var data = event.detail[0],
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/rails_admin/ra.sidescroll.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function ($) {
"use strict";

$(document).on("rails_admin.dom_ready", () => {
document.addEventListener("rails_admin.dom_ready", () => {
const listForm = document.getElementById("bulk_form");
if (!listForm || !listForm.classList.contains("ra-sidescroll")) {
return;
Expand Down
4 changes: 2 additions & 2 deletions app/assets/javascripts/rails_admin/ra.widgets.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(function ($) {
$(document).on("rails_admin.dom_ready", function (e, content) {
document.addEventListener("rails_admin.dom_ready", function (event) {
var $editors,
array,
config_options,
Expand All @@ -9,7 +9,7 @@
goFroalaWysiwygs,
goSimpleMDEs,
options;
content = content ? content : $("form");
var content = event.detail || $("form");
if (content.length) {
$.fn.datetimepicker.defaults.icons = {
time: "fa fa-clock-o",
Expand Down
9 changes: 6 additions & 3 deletions app/assets/javascripts/rails_admin/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,17 @@
$("html").attr("lang"),
$("#admin-js").data("i18nOptions")
);
$(document).trigger("rails_admin.dom_ready");

const event = new CustomEvent("rails_admin.dom_ready");
document.dispatchEvent(event);
});

$(document).on("pjax:end", function () {
$(document).trigger("rails_admin.dom_ready");
const event = new CustomEvent("rails_admin.dom_ready");
document.dispatchEvent(event);
});

$(document).on("rails_admin.dom_ready", function () {
document.addEventListener("rails_admin.dom_ready", function () {
$(".nav.nav-pills li.active").removeClass("active");
$(
'.nav.nav-pills li[data-model="' + $(".page-header").data("model") + '"]'
Expand Down
1 change: 1 addition & 0 deletions config/locales/rails_admin.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ en:
time: Time ...
number: Number ...
contains: Contains
does_not_contain: Does not contain
is_exactly: Is exactly
starts_with: Starts with
ends_with: Ends with
Expand Down
12 changes: 9 additions & 3 deletions lib/rails_admin/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def build_statement_for_type
case @type
when :boolean then build_statement_for_boolean
when :integer, :decimal, :float then build_statement_for_integer_decimal_or_float
when :string, :text then build_statement_for_string_or_text
when :string, :text, :citext then build_statement_for_string_or_text
when :enum then build_statement_for_enum
when :belongs_to_association then build_statement_for_belongs_to_association
when :uuid then build_statement_for_uuid
Expand Down Expand Up @@ -252,7 +252,7 @@ def build_statement_for_string_or_text

@value = begin
case @operator
when 'default', 'like'
when 'default', 'like', 'not_like'
"%#{@value}%"
when 'starts_with'
"#{@value}%"
Expand All @@ -264,7 +264,13 @@ def build_statement_for_string_or_text
end

if ['postgresql', 'postgis'].include? ar_adapter
["(#{@column} ILIKE ?)", @value]
if @operator == 'not_like'
["(#{@column} NOT ILIKE ?)", @value]
else
["(#{@column} ILIKE ?)", @value]
end
elsif @operator == 'not_like'
["(LOWER(#{@column}) NOT LIKE ?)", @value]
else
["(LOWER(#{@column}) LIKE ?)", @value]
end
Expand Down
2 changes: 2 additions & 0 deletions lib/rails_admin/adapters/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ def build_statement_for_string_or_text
return if @value.blank?
@value = begin
case @operator
when 'not_like'
Regexp.compile("^((?!#{Regexp.escape(@value)}).)*$", Regexp::IGNORECASE)
when 'default', 'like'
Regexp.compile(Regexp.escape(@value), Regexp::IGNORECASE)
when 'starts_with'
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def current_user_method(&block)
end

def default_search_operator=(operator)
if %w(default like starts_with ends_with is =).include? operator
if %w(default like not_like starts_with ends_with is =).include? operator
@default_search_operator = operator
else
raise(ArgumentError.new("Search operator '#{operator}' not supported"))
Expand Down
1 change: 1 addition & 0 deletions lib/rails_admin/config/fields/types/all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@
require 'rails_admin/config/fields/types/json'
require 'rails_admin/config/fields/types/inet'
require 'rails_admin/config/fields/types/uuid'
require 'rails_admin/config/fields/types/citext'
13 changes: 13 additions & 0 deletions lib/rails_admin/config/fields/types/citext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'rails_admin/config/fields/types/text'

module RailsAdmin
module Config
module Fields
module Types
class Citext < Text
RailsAdmin::Config::Fields::Types.register(:citext, self)
end
end
end
end
end
8 changes: 8 additions & 0 deletions spec/rails_admin/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
'(LOWER(field) LIKE ?)'
end
end
let(:not_like) do
if ['postgresql', 'postgis'].include? activerecord_config[:adapter]
'(field NOT ILIKE ?)'
else
'(LOWER(field) NOT LIKE ?)'
end
end

def predicates_for(scope)
scope.where_clause.instance_variable_get(:@predicates)
Expand Down Expand Up @@ -274,6 +281,7 @@ def build_statement(type, value, operator)
expect(build_statement(:string, 'foo', 'was')).to be_nil
expect(build_statement(:string, 'foo', 'default')).to eq([like, '%foo%'])
expect(build_statement(:string, 'foo', 'like')).to eq([like, '%foo%'])
expect(build_statement(:string, 'foo', 'not_like')).to eq([not_like, '%foo%'])
expect(build_statement(:string, 'foo', 'starts_with')).to eq([like, 'foo%'])
expect(build_statement(:string, 'foo', 'ends_with')).to eq([like, '%foo'])
expect(build_statement(:string, 'foo', 'is')).to eq(['(field = ?)', 'foo'])
Expand Down
1 change: 1 addition & 0 deletions spec/rails_admin/adapters/mongoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def parse_value(value)
expect(@abstract_model.send(:build_statement, :field, :string, 'foo', 'was')).to be_nil
expect(@abstract_model.send(:build_statement, :field, :string, 'foo', 'default')).to eq(field: /foo/i)
expect(@abstract_model.send(:build_statement, :field, :string, 'foo', 'like')).to eq(field: /foo/i)
expect(@abstract_model.send(:build_statement, :field, :string, 'foo', 'not_like')).to eq(field: /^((?!foo).)*$/i)
expect(@abstract_model.send(:build_statement, :field, :string, 'foo', 'starts_with')).to eq(field: /^foo/i)
expect(@abstract_model.send(:build_statement, :field, :string, 'foo', 'ends_with')).to eq(field: /foo$/i)
expect(@abstract_model.send(:build_statement, :field, :string, 'foo', 'is')).to eq(field: 'foo')
Expand Down
7 changes: 7 additions & 0 deletions spec/rails_admin/config/fields/types/citext_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'spec_helper'

RSpec.describe RailsAdmin::Config::Fields::Types::Citext do
it_behaves_like 'a generic field type', :string_field

it_behaves_like 'a string-like field type', :string_field
end

0 comments on commit c6bd734

Please sign in to comment.