Skip to content

Commit

Permalink
Introduce partial search similar to Rails
Browse files Browse the repository at this point in the history
  • Loading branch information
goosys committed Oct 4, 2024
1 parent ff962c3 commit 2084605
Show file tree
Hide file tree
Showing 19 changed files with 86 additions and 84 deletions.
10 changes: 9 additions & 1 deletion app/helpers/administrate/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ def application_title

def render_field(field, locals = {})
locals[:field] = field
render locals: locals, partial: field.to_partial_path
if (prefix = find_partial_prefix(field))
render locals: locals, partial: "#{prefix}/#{field.page}"
end
end

def find_partial_prefix(field)
field._partial_prefixes.detect do |prefix|
lookup_context.template_exists?(field.page, [prefix], true)
end
end

def requireness(field)
Expand Down
17 changes: 15 additions & 2 deletions lib/administrate/field/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ def self.permitted_attribute(attr, _options = nil)
attr
end

def self._partial_prefixes
@_partial_prefixes ||= if superclass == Administrate::Field::Base
local_partial_prefixes
else
local_partial_prefixes + superclass._partial_prefixes
end
end

def self.local_partial_prefixes
["fields/#{field_type}"]
end
private_class_method :local_partial_prefixes

def initialize(attribute, data, page, options = {})
@attribute = attribute
@data = data
Expand All @@ -52,8 +65,8 @@ def name
attribute.to_s
end

def to_partial_path
"/fields/#{self.class.field_type}/#{page}"
def _partial_prefixes
self.class._partial_prefixes
end

def required?
Expand Down
9 changes: 6 additions & 3 deletions spec/administrate/views/fields/has_one/_show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
name: "simple_string_field",
truncate: "string value",
html_class: "string",
to_partial_path: "fields/string/index"
page: "index",
_partial_prefixes: ["fields/string"]
)

nested_show_page_for_has_one = instance_double(
Expand Down Expand Up @@ -123,7 +124,8 @@ def render_field
data: nested_collection,
html_class: "has-many",
name: "payments",
to_partial_path: "fields/has_many/index"
page: "index",
_partial_prefixes: ["fields/has_many"]
)

nested_show_page_for_nested_has_one = instance_double(
Expand All @@ -141,7 +143,8 @@ def render_field
linkable?: true,
nested_show: nested_show_page_for_nested_has_one,
html_class: "has-one",
to_partial_path: "fields/has_one/show",
page: "show",
_partial_prefixes: ["fields/has_one"],
display_associated_resource: "Resource Doubly Nested with HasOne",
name: "page"
)
Expand Down
13 changes: 0 additions & 13 deletions spec/example_app/app/views/fields/has_many_variant/_form.html.erb

This file was deleted.

This file was deleted.

23 changes: 0 additions & 23 deletions spec/example_app/app/views/fields/has_many_variant/_show.html.erb

This file was deleted.

18 changes: 18 additions & 0 deletions spec/helpers/administrate/application_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
require "rails_helper"
require "administrate/field/has_many"
require "administrate/field/has_many_variant"

RSpec.describe Administrate::ApplicationHelper do
describe "#find_partial_prefix" do
context "when the field has a partial" do
it "returns the prefix" do
field = Administrate::Field::HasMany.new(:name, "hello", :show)
expect(find_partial_prefix(field)).to eq("fields/has_many")
end
end

context "when the field does not have a partial and the superclass does" do
it "returns the superclass prefix" do
field = Administrate::Field::HasManyVariant.new(:name, "hello", :show)
expect(find_partial_prefix(field)).to eq("fields/has_many")
end
end
end

describe "#display_resource_name" do
it "defaults to the plural of the model name" do
displayed = display_resource_name(:customer)
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/fields/belongs_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
end
end

describe "#to_partial_path" do
describe "#_partial_prefixes" do
it "returns a partial based on the page being rendered" do
page = :show
owner = double
field = Administrate::Field::BelongsTo.new(:owner, owner, page)

path = field.to_partial_path
prefixes = field._partial_prefixes

expect(path).to eq("/fields/belongs_to/#{page}")
expect(prefixes).to eq(["fields/belongs_to", "fields/associative"])
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/lib/fields/boolean_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
describe Administrate::Field::Boolean do
include FieldMatchers

describe "#to_partial_path" do
describe "#_partial_prefixes" do
it "returns a partial based on the page being rendered" do
page = :show
boolean = double
field = Administrate::Field::Boolean.new(:price, boolean, page)

path = field.to_partial_path
prefixes = field._partial_prefixes

expect(path).to eq("/fields/boolean/#{page}")
expect(prefixes).to eq(["fields/boolean"])
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/lib/fields/email_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require "administrate/field/email"

describe Administrate::Field::Email do
describe "#to_partial_path" do
describe "#_partial_prefixes" do
it "returns a partial based on the page being rendered" do
page = :show
field = Administrate::Field::Email.new(:email, "[email protected]", page)

path = field.to_partial_path
prefixes = field._partial_prefixes

expect(path).to eq("/fields/email/#{page}")
expect(prefixes).to eq(["fields/email"])
end
end
end
6 changes: 3 additions & 3 deletions spec/lib/fields/has_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
end
end

describe "#to_partial_path" do
describe "#_partial_prefixes" do
it "returns a partial based on the page being rendered" do
page = :show
items = double
field = Administrate::Field::HasMany.new(:items, items, page)

path = field.to_partial_path
prefixes = field._partial_prefixes

expect(path).to eq("/fields/has_many/#{page}")
expect(prefixes).to eq(["fields/has_many", "fields/associative"])
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/lib/fields/has_one_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
end
end

describe "#to_partial_path" do
describe "#_partial_prefixes" do
it "returns a partial based on the page being rendered" do
resource = double
page = :show
Expand All @@ -89,9 +89,9 @@
resource: resource
)

path = field.to_partial_path
prefixes = field._partial_prefixes

expect(path).to eq("/fields/has_one/#{page}")
expect(prefixes).to eq(["fields/has_one", "fields/associative"])
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/lib/fields/number_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
describe Administrate::Field::Number do
include FieldMatchers

describe "#to_partial_path" do
describe "#_partial_prefixes" do
it "returns a partial based on the page being rendered" do
page = :show
number = double
field = Administrate::Field::Number.new(:price, number, page)

path = field.to_partial_path
prefixes = field._partial_prefixes

expect(path).to eq("/fields/number/#{page}")
expect(prefixes).to eq(["fields/number"])
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/lib/fields/password_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
describe Administrate::Field::Password do
include FieldMatchers

describe "#to_partial_path" do
describe "#_partial_prefixes" do
it "returns a partial based on the page being rendered" do
page = :show
field = Administrate::Field::Password.new(:password, "my_password", page)

path = field.to_partial_path
prefixes = field._partial_prefixes

expect(path).to eq("/fields/password/#{page}")
expect(prefixes).to eq(["fields/password"])
end
end

Expand Down
8 changes: 5 additions & 3 deletions spec/lib/fields/polymorphic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
end
end

describe "#to_partial_path" do
describe "#_partial_prefixes" do
it "returns a partial based on the page being rendered" do
page = :show
field = Administrate::Field::Polymorphic.new(:foo, "hello", page)

path = field.to_partial_path
prefixes = field._partial_prefixes

expect(path).to eq("/fields/polymorphic/#{page}")
expect(prefixes).to eq(
["fields/polymorphic", "fields/belongs_to", "fields/associative"]
)
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/lib/fields/rich_text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
)
end

describe "#to_partial_path" do
describe "#_partial_prefixes" do
it "returns a partial based on the page being rendered" do
page = :show
action_text = ActionText::RichText.new(
body: "<div class='trix'><p>Foo</p></div>"
)
field = Administrate::Field::RichText.new(:document, action_text, page)

path = field.to_partial_path
prefixes = field._partial_prefixes

expect(path).to eq("/fields/rich_text/#{page}")
expect(prefixes).to eq(["fields/rich_text"])
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/lib/fields/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
describe Administrate::Field::String do
include FieldMatchers

describe "#to_partial_path" do
describe "#_partial_prefixes" do
it "returns a partial based on the page being rendered" do
page = :show
field = Administrate::Field::String.new(:string, "hello", page)

path = field.to_partial_path
prefixes = field._partial_prefixes

expect(path).to eq("/fields/string/#{page}")
expect(prefixes).to eq(["fields/string"])
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/lib/fields/time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
require "administrate/field/time"

describe Administrate::Field::Time do
describe "#to_partial_path" do
describe "#_partial_prefixes" do
it "returns a partial based on the page being rendered" do
page = :show
time = Time.zone.local(2000, 1, 1, 15, 45, 33)
field = Administrate::Field::Time.new(:time, time, page)

path = field.to_partial_path
prefixes = field._partial_prefixes

expect(path).to eq("/fields/time/#{page}")
expect(prefixes).to eq(["fields/time"])
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/lib/fields/url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
end
end

describe "#to_partial_path" do
describe "#_partial_prefixes" do
it "returns a partial based on the page being rendered" do
page = :show
field = Administrate::Field::Url.new(:url, "https://thoughtbot.com", page)

path = field.to_partial_path
prefixes = field._partial_prefixes

expect(path).to eq("/fields/url/#{page}")
expect(prefixes).to eq(["fields/url"])
end
end

Expand Down

0 comments on commit 2084605

Please sign in to comment.