-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Guess correct name for namespaced associations (#2235)
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
Showing
14 changed files
with
120 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
spec/example_app/app/controllers/admin/blog/tags_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module Blog | ||
class TagPolicy < ApplicationPolicy | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
namespace :blog do | ||
resources :posts | ||
resources :tags | ||
end | ||
|
||
resources :stats, only: [:index] | ||
|
9 changes: 9 additions & 0 deletions
9
spec/example_app/db/migrate/20220804132651_create_blog_tags.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
9 changes: 9 additions & 0 deletions
9
spec/example_app/db/migrate/20220804133503_create_blog_posts_tags.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |