diff --git a/app/views/fields/belongs_to/_index.html.erb b/app/views/fields/belongs_to/_index.html.erb index 005dba2b913a51b9c8e29206ae0190344ab82465..ffc479ea4a55eac6e21c6e8c3282e7b7a02001df 100644 --- a/app/views/fields/belongs_to/_index.html.erb +++ b/app/views/fields/belongs_to/_index.html.erb @@ -16,7 +16,7 @@ By default, the relationship is rendered as a link to the associated object. %> <% if field.data %> - <% if valid_action?(:show, field.attribute) %> + <% if valid_action?(:show, field.associated_class) %> <%= link_to( field.display_associated_resource, [namespace, field.data], diff --git a/app/views/fields/belongs_to/_show.html.erb b/app/views/fields/belongs_to/_show.html.erb index eeb0650f0b511010020b7735fdca561b9fc538af..24b47081ae717704e220ce74786e9ca27ee01930 100644 --- a/app/views/fields/belongs_to/_show.html.erb +++ b/app/views/fields/belongs_to/_show.html.erb @@ -16,7 +16,7 @@ By default, the relationship is rendered as a link to the associated object. %> <% if field.data %> - <% if valid_action?(:show, field.attribute) %> + <% if valid_action?(:show, field.associated_class) %> <%= link_to( field.display_associated_resource, [namespace, field.data], diff --git a/docs/customizing_dashboards.md b/docs/customizing_dashboards.md index a9de3b570e6caea8a6989b71ab812c46a7cb4b32..ab9e4accd35cd17d110b9dbfd36360ba728e277e 100644 --- a/docs/customizing_dashboards.md +++ b/docs/customizing_dashboards.md @@ -84,6 +84,9 @@ Defaults to `:#{attribute}_id`. `:scope` - Specifies a custom scope inside a callable. Useful for preloading. Example: `.with_options(scope: -> { MyModel.includes(:rel).limit(5) })` +`:class_name` - Specifies the name of the associated class. +Defaults to `:#{attribute}.to_s.singularize.camelcase`. + **Field::HasMany** `:limit` - Set the number of resources to display in the show view. Default is @@ -97,6 +100,14 @@ Example: `.with_options(scope: -> { MyModel.includes(:rel).limit(5) })` `:foreign_key` - Specifies the name of the foreign key directly. Defaults to `:#{attribute}_id` +`:class_name` - Specifies the name of the associated class. +Defaults to `:#{attribute}.to_s.singularize.camelcase`. + +**Field::HasOne** + +`:class_name` - Specifies the name of the associated class. +Defaults to `:#{attribute}.to_s.singularize.camelcase`. + **Field::Number** `:decimals` - Set the number of decimals to display. Defaults to `0`. diff --git a/gemfiles/rails42.gemfile b/gemfiles/rails42.gemfile index bfdc470a8cbfd68a927a4a64f020ef1e8a70e1dd..ef9c4b963248b26726ed6bf8104f5b806889056b 100644 --- a/gemfiles/rails42.gemfile +++ b/gemfiles/rails42.gemfile @@ -35,7 +35,6 @@ group :test do gem "shoulda-matchers" gem "timecop" gem "webmock" - gem "pundit" end group :staging, :production do diff --git a/gemfiles/rails50.gemfile b/gemfiles/rails50.gemfile index abeadba51939a1acf868fac8c7d593effdecc2c8..dcd97fb8ae25151ab198119dd648da375d24a728 100644 --- a/gemfiles/rails50.gemfile +++ b/gemfiles/rails50.gemfile @@ -35,7 +35,6 @@ group :test do gem "shoulda-matchers" gem "timecop" gem "webmock" - gem "pundit" end group :staging, :production do diff --git a/gemfiles/rails51.gemfile b/gemfiles/rails51.gemfile index 6d7335853bb5a3ffa71c09a513be1cf2431eef3e..7dd598008847921e9e56890b32ddc1f743207b3f 100644 --- a/gemfiles/rails51.gemfile +++ b/gemfiles/rails51.gemfile @@ -35,7 +35,6 @@ group :test do gem "shoulda-matchers" gem "timecop" gem "webmock" - gem "pundit" end group :staging, :production do diff --git a/gemfiles/sass_3_4.gemfile b/gemfiles/sass_3_4.gemfile index 5fc1b80335ced37238a8c7203e960d1dad7996f2..722da65bcdddf75c69c42915829b62af8ecaaf4f 100644 --- a/gemfiles/sass_3_4.gemfile +++ b/gemfiles/sass_3_4.gemfile @@ -33,7 +33,6 @@ group :test do gem "shoulda-matchers" gem "timecop" gem "webmock" - gem "pundit" end group :staging, :production do diff --git a/lib/administrate/field/associative.rb b/lib/administrate/field/associative.rb index 805ea6671f0c8a4655b40f3493d45787ee3e760a..0ae5e3563974bcdb6685609f8fb84dd54e585d65 100644 --- a/lib/administrate/field/associative.rb +++ b/lib/administrate/field/associative.rb @@ -7,16 +7,16 @@ module Administrate associated_dashboard.display_resource(data) end + def associated_class + associated_class_name.constantize + end + protected def associated_dashboard "#{associated_class_name}Dashboard".constantize.new end - def associated_class - associated_class_name.constantize - end - def associated_class_name options.fetch(:class_name, attribute.to_s.singularize.camelcase) end diff --git a/spec/administrate/views/fields/belongs_to/_index_spec.rb b/spec/administrate/views/fields/belongs_to/_index_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..17f5a539764db527173e765bb0bdb2f7ff58fd0b --- /dev/null +++ b/spec/administrate/views/fields/belongs_to/_index_spec.rb @@ -0,0 +1,39 @@ +require "rails_helper" + +describe "fields/belongs_to/_index", type: :view do + let(:product) { create(:product) } + let(:product_path) { polymorphic_path([:admin, product]) } + let(:link) { "<a href=\"#{product_path}\">#{product.name}</a>" } + let(:associated_class) { "test_associated_class" } + let(:belongs_to) do + instance_double( + "Administrate::Field::BelongsTo", + associated_class: associated_class, + display_associated_resource: product.name, + data: product, + ) + end + + context "if associated resource has a show route" do + it "displays link" do + allow(view).to receive(:valid_action?).and_return(true) + render_belongs_to_index + expect(rendered.strip).to include(link) + end + end + + context "if associated resource has no show route" do + it "displays link" do + allow(view).to receive(:valid_action?).and_return(false) + render_belongs_to_index + expect(rendered.strip).to_not include(link) + end + end + + def render_belongs_to_index + render( + partial: "fields/belongs_to/index.html.erb", + locals: { field: belongs_to, namespace: "admin" }, + ) + end +end diff --git a/spec/administrate/views/fields/belongs_to/_show_spec.rb b/spec/administrate/views/fields/belongs_to/_show_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..62179cf39842ed6d206a31f02e32e8450b565c7c --- /dev/null +++ b/spec/administrate/views/fields/belongs_to/_show_spec.rb @@ -0,0 +1,39 @@ +require "rails_helper" + +describe "fields/belongs_to/_show", type: :view do + let(:product) { create(:product) } + let(:product_path) { polymorphic_path([:admin, product]) } + let(:link) { "<a href=\"#{product_path}\">#{product.name}</a>" } + let(:associated_class) { "test_associated_class" } + let(:belongs_to) do + instance_double( + "Administrate::Field::BelongsTo", + associated_class: associated_class, + display_associated_resource: product.name, + data: product, + ) + end + + context "if associated resource has a show route" do + it "displays link" do + allow(view).to receive(:valid_action?).and_return(true) + render_belongs_to_show + expect(rendered.strip).to include(link) + end + end + + context "if associated resource has no show route" do + it "displays link" do + allow(view).to receive(:valid_action?).and_return(false) + render_belongs_to_show + expect(rendered.strip).to_not include(link) + end + end + + def render_belongs_to_show + render( + partial: "fields/belongs_to/show.html.erb", + locals: { field: belongs_to, namespace: "admin" }, + ) + end +end