Skip to content

Commit

Permalink
Guess correct name for namespaced associations (#2235)
Browse files Browse the repository at this point in the history
Fixes #1978

This includes the namespace of the associated class. If the associated class
is `System::Build`, the previous code would tell us that the name was `Build`.
This code gets the right name.
  • Loading branch information
pablobm authored Aug 8, 2022
1 parent 7cde042 commit ea96c83
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/administrate/field/associative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.associated_class(resource_class, attr)
end

def self.associated_class_name(resource_class, attr)
reflection(resource_class, attr).class_name
associated_class(resource_class, attr).name
end

def self.reflection(resource_class, attr)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Admin
module Blog
class TagsController < Admin::ApplicationController
end
end
end
2 changes: 2 additions & 0 deletions spec/example_app/app/dashboards/blog/post_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class PostDashboard < Administrate::BaseDashboard
title: Field::String,
published_at: Field::DateTime,
body: Field::Text,
tags: Field::HasMany,
}

READ_ONLY_ATTRIBUTES = [
Expand All @@ -20,6 +21,7 @@ class PostDashboard < Administrate::BaseDashboard
COLLECTION_ATTRIBUTES = [
:id,
:title,
:tags,
:published_at,
]

Expand Down
31 changes: 31 additions & 0 deletions spec/example_app/app/dashboards/blog/tag_dashboard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "administrate/base_dashboard"

module Blog
class TagDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
id: Field::Number,
name: Field::String,
created_at: Field::DateTime,
updated_at: Field::DateTime,
posts: Field::HasMany,
}.freeze

COLLECTION_ATTRIBUTES = %i[
id
name
posts
created_at
].freeze

FORM_ATTRIBUTES = %i[
name
posts
].freeze

SHOW_PAGE_ATTRIBUTES = COLLECTION_ATTRIBUTES

def display_resource(resource)
resource.name
end
end
end
2 changes: 2 additions & 0 deletions spec/example_app/app/models/blog/post.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Blog
class Post < ApplicationRecord
has_and_belongs_to_many :tags

validates :title, :body, presence: true
end
end
7 changes: 7 additions & 0 deletions spec/example_app/app/models/blog/tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Blog
class Tag < ApplicationRecord
has_and_belongs_to_many :posts

validates :name, presence: true
end
end
4 changes: 4 additions & 0 deletions spec/example_app/app/policies/blog/tag_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Blog
class TagPolicy < ApplicationPolicy
end
end
1 change: 1 addition & 0 deletions spec/example_app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace :blog do
resources :posts
resources :tags
end

resources :stats, only: [:index]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateBlogTags < ActiveRecord::Migration[6.1]
def change
create_table :blog_tags do |t|
t.string :name

t.timestamps
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateBlogPostsTags < ActiveRecord::Migration[6.1]
def change
create_table :blog_posts_tags do |t|
t.belongs_to :post
t.belongs_to :tag
t.timestamps
end
end
end
21 changes: 18 additions & 3 deletions spec/example_app/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `rails
# db:schema:load`. When creating a new database, `rails db:schema:load` tends to
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_07_14_081950) do
ActiveRecord::Schema.define(version: 2022_08_04_133503) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -23,6 +23,21 @@
t.datetime "updated_at", null: false
end

create_table "blog_posts_tags", force: :cascade do |t|
t.bigint "post_id"
t.bigint "tag_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["post_id"], name: "index_blog_posts_tags_on_post_id"
t.index ["tag_id"], name: "index_blog_posts_tags_on_tag_id"
end

create_table "blog_tags", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

create_table "countries", id: :serial, force: :cascade do |t|
t.string "code", null: false
t.string "name"
Expand Down
20 changes: 13 additions & 7 deletions spec/example_app/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,25 @@

Product.find_each do |p|
Page.create!(
title: "Something about #{p.name}",
title: "Rules of #{p.name}",
body: Faker::Lorem.paragraph,
product: p,
)
Page.create!(
title: "The secrets of the game #{p.name}",
end

tag_secrets = Blog::Tag.create!(name: "secrets")
tag_recommendations = Blog::Tag.create!(name: "recommendations")

Product.find_each do |p|
Blog::Post.create!(
title: "The secrets of #{p.name}",
body: Faker::Lorem.paragraph,
product: p,
tags: [tag_secrets],
)
Page.create!(
title: "If you liked #{p.name}, you will love these games",
Blog::Post.create!(
title: "If you liked #{p.name}, you will love these products",
body: Faker::Lorem.paragraph,
product: p,
tags: [tag_recommendations],
)
end

Expand Down
4 changes: 4 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
body { "Empty" }
end

factory :blog_tag, class: "Blog::Tag" do
name { Faker::NatoPhoneticAlphabet.code_word.downcase }
end

factory :series do
sequence(:name) { |n| "Series #{n}" }
end
Expand Down
13 changes: 13 additions & 0 deletions spec/features/associations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "rails_helper"

describe "Associations" do
it "can associate to namespaced models on dashboards" do
post = create(:blog_post)
tag = create(:blog_tag, name: "foobarisms")
post.tags << tag

visit admin_blog_post_url(post)

expect(page).to have_css(".cell-data", text: "foobarisms")
end
end

0 comments on commit ea96c83

Please sign in to comment.