Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Has one translations #2200

Merged
merged 1 commit into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/views/fields/has_one/_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ All show page attributes of has_one relationship would be rendered
<div>
<dt class="attribute-label">
<%= t(
"helpers.label.#{resource_name}.#{attribute.name}",
"helpers.label.#{field.associated_class_name.underscore}.#{attribute.name}",
default: attribute.name.titleize,
) %>
</dt>
Expand Down
12 changes: 6 additions & 6 deletions lib/administrate/field/associative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ def associated_class
end
end

private

def associated_dashboard
"#{associated_class_name}Dashboard".constantize.new
end

def associated_class_name
if option_given?(:class_name)
deprecated_option(:class_name)
Expand All @@ -48,6 +42,12 @@ def associated_class_name
end
end

private

def associated_dashboard
"#{associated_class_name}Dashboard".constantize.new
end

def primary_key
if option_given?(:primary_key)
deprecated_option(:primary_key)
Expand Down
250 changes: 142 additions & 108 deletions spec/administrate/views/fields/has_one/_show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

describe "fields/has_one/_show", type: :view do
before do
view.extend Administrate::ApplicationHelper
allow(view).to receive(:accessible_action?).and_return(true)
allow(view).to receive(:namespace).and_return(:admin)
end

context "without an associated record" do
context "without a persisted record" do
it "displays nothing" do
has_one = Administrate::Field::HasOne.new(
:product_meta_tag,
Expand All @@ -23,124 +25,156 @@
end
end

context "with an associated record" do
context "when linking the record is allowed" do
it "renders a link to the record" do
product = create(:product)
product_path = polymorphic_path([:admin, product])
nested_show = instance_double(
"Administrate::Page::Show",
resource: double(
class: ProductMetaTag,
),
attributes: [],
resource_name: "Product Tag",
)
has_one = instance_double(
"Administrate::Field::HasOne",
display_associated_resource: product.name,
data: product,
linkable?: true,
nested_show: nested_show,
)

render(
partial: "fields/has_one/show",
locals: {
field: has_one,
namespace: :admin,
resource_name: "product_meta_tag",
},
)
context "with a persisted record" do
before do
field_resource = create(:product_meta_tag)
@path_to_field_resource = polymorphic_path([:admin, field_resource])

nested_simple_field = instance_double(
"Administrate::Field::String",
name: "simple_string_field",
truncate: "string value",
html_class: "string",
to_partial_path: "fields/string/index",
)

link = "<a href=\"#{product_path}\">#{product.name}</a>"
expect(rendered.strip).to include(link)
end
nested_show_page_for_has_one = instance_double(
"Administrate::Page::Show",
resource: double(
class: ProductMetaTag,
),
attributes: [
nested_simple_field,
],
)

@has_one_field = instance_double(
"Administrate::Field::HasOne",
display_associated_resource: "The Nested Resource",
data: field_resource,
linkable?: true,
nested_show: nested_show_page_for_has_one,
associated_class_name: "NestedHasOne",
)

@page_double = instance_double("Administrate::Page::Show")
end

it "renders nested attribute relationships" do
view.extend Administrate::ApplicationHelper

product = create(:product)
page = create(:page, product: product)

nested_has_many = instance_double(
"Administrate::Field::HasMany",
associated_collection: [page],
attribute: :page,
data: [page],
resources: [page],
html_class: "has-many",
name: "Page",
to_partial_path: "fields/has_many/index",
order_from_params: {},
)

nested_show = instance_double(
"Administrate::Page::Show",
resource: double(
class: ProductMetaTag,
),
attributes: [nested_has_many],
resource_name: "Product Tag",
)

has_one = instance_double(
"Administrate::Field::HasOne",
display_associated_resource: product.name,
data: product,
linkable?: true,
nested_show: nested_show,
)

page_double = instance_double("Administrate::Page::Show")

render(
partial: "fields/has_one/show",
locals: {
field: has_one,
namespace: :admin,
page: page_double,
resource_name: "product_meta_tag",
def render_field
render(
partial: "fields/has_one/show",
locals: {
field: @has_one_field,
page: @page_double,
resource_name: "parent_resource",
},
)
end

it "uses the correct labels for fields" do
I18n.backend.translations(do_init: true)[:en].deep_merge!(
helpers: {
label: {
nested_has_one: {
simple_string_field: "Just a Simple String",
},
},
)
},
)

render_field
expect(rendered.strip).to include("Just a Simple String")
end

has_many_count = "1 page"
expect(rendered.strip).to include(has_many_count)
context "when linking the record is allowed" do
it "renders a link to the record" do
render_field
link = "<a href=\"#{@path_to_field_resource}\">The Nested Resource</a>"
expect(rendered.strip).to include(link)
end
end

context "when linking the record is not allowed" do
it "displays the record without a link" do
allow(view).to receive(:accessible_action?).and_return(false)
product = create(:product)
nested_show = instance_double(
"Administrate::Page::Show",
resource: double(
class: ProductMetaTag,
),
attributes: [],
resource_name: "Product Tag",
)
has_one = instance_double(
"Administrate::Field::HasOne",
display_associated_resource: product.name,
data: product,
linkable?: true,
nested_show: nested_show,
)

render(
partial: "fields/has_one/show",
locals: {
field: has_one,
namespace: :admin,
resource_name: "product_meta_tag",
},
)

expect(rendered.strip).to include(product.name)
expect(rendered.strip).not_to include("<a ")
render_field
expect(rendered.strip).to include("The Nested Resource")
expect(rendered.strip).not_to include("<a")
end
end
end

context "when there are nested associations" do
it "renders them" do
# Let's pretend that a Customer has these associations:
#
# has_one :page # The "nested_resource"
# has_many :payments # The "nested_collection"
#
# Here we render a HasOne field (a Customer)
# that in turn has a HasOne and a HasMany
field_resource = create(:customer)
nested_resource = create(:page)
nested_collection = create_list(:payment, 2)

nested_has_many = instance_double(
"Administrate::Field::HasMany",
attribute: :payments,
data: nested_collection,
html_class: "has-many",
name: "payments",
to_partial_path: "fields/has_many/index",
)

nested_show_page_for_nested_has_one = instance_double(
"Administrate::Page::Show",
resource: double(
class: ProductMetaTag,
),
attributes: [],
)

nested_has_one = instance_double(
"Administrate::Field::HasOne",
attribute: :page,
data: nested_resource,
linkable?: true,
nested_show: nested_show_page_for_nested_has_one,
html_class: "has-one",
to_partial_path: "fields/has_one/show",
display_associated_resource: "Resource Doubly Nested with HasOne",
name: "page",
)

nested_show_page_for_top_has_one = instance_double(
"Administrate::Page::Show",
attributes: [nested_has_one, nested_has_many],
)

has_one_field = instance_double(
"Administrate::Field::HasOne",
display_associated_resource: "Resource Nested with HasOne",
data: field_resource,
linkable?: true,
nested_show: nested_show_page_for_top_has_one,
associated_class_name: "NameOfAssociatedClass",
)

page_double = instance_double("Administrate::Page::Show")

render(
partial: "fields/has_one/show",
locals: {
field: has_one_field,
page: page_double,
resource_name: "product_meta_tag",
},
)

expect(rendered.strip).to include("Resource Nested with HasOne")
expect(rendered.strip).to include("Resource Doubly Nested with HasOne")
expect(rendered.strip).to include("Payments")
expect(rendered.strip).to include("2 payments")
end
end
end