Skip to content

Commit

Permalink
Add support for namespaced models (#24)
Browse files Browse the repository at this point in the history
* This moves `Student` into the `Foo` namespace to provide a model to use
  it on,
* Tests that we can create a new school and to create a school with a
  nested student,
* Drops namespaces from association names,
* Allows providing the `class_name`.
  • Loading branch information
sedubois authored Feb 16, 2020
1 parent 95c56e3 commit 743f0a1
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 19 deletions.
1 change: 1 addition & 0 deletions administrate-field-nested_has_many.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Gem::Specification.new do |gem|

gem.add_development_dependency "appraisal"
gem.add_development_dependency "capybara"
gem.add_development_dependency "selenium-webdriver"
gem.add_development_dependency "factory_bot"
gem.add_development_dependency "i18n-tasks"
gem.add_development_dependency "rake"
Expand Down
20 changes: 12 additions & 8 deletions lib/administrate/field/nested_has_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,20 @@ def to_s
data
end

def self.dashboard_for_resource(resource)
"#{resource.to_s.classify}Dashboard".constantize
def self.dashboard_for_resource(resource, options)
class_name = options && options[:class_name] || resource.to_s.classify
"#{class_name}Dashboard".constantize
end

def self.associated_attributes(associated_resource)
DEFAULT_ATTRIBUTES +
dashboard_for_resource(associated_resource).new.permitted_attributes
def self.associated_attributes(associated_resource, options)
dashboard_class = dashboard_for_resource(associated_resource, options)
DEFAULT_ATTRIBUTES + dashboard_class.new.permitted_attributes
end

def self.permitted_attribute(associated_resource, _options = nil)
def self.permitted_attribute(associated_resource, options = nil)
{
"#{associated_resource}_attributes".to_sym =>
associated_attributes(associated_resource),
associated_attributes(associated_resource, options),
}
end

Expand All @@ -65,7 +66,10 @@ def associated_class_name
end

def association_name
associated_class_name.underscore.pluralize
options.fetch(
:association_name,
associated_class_name.underscore.pluralize[/([^\/]*)$/, 1],
)
end

def associated_form
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Admin
class StudentsController < Admin::ApplicationController
class Foo::StudentsController < Admin::ApplicationController
# To customize the behavior of this controller,
# you can overwrite any of the RESTful actions. For example:
#
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "administrate/base_dashboard"

class StudentDashboard < Administrate::BaseDashboard
class Foo::StudentDashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
# a hash that describes the type of each of the model's fields.
#
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/dashboards/school_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SchoolDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
id: Field::Number,
name: Field::String,
students: Field::NestedHasMany,
students: Field::NestedHasMany.with_options(class_name: "Foo::Student"),
created_at: Field::DateTime,
updated_at: Field::DateTime,
}.freeze
Expand Down
3 changes: 3 additions & 0 deletions spec/dummy/app/models/foo/student.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Foo::Student < ApplicationRecord
belongs_to :school
end
2 changes: 1 addition & 1 deletion spec/dummy/app/models/school.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class School < ApplicationRecord
has_many :students
has_many :students, class_name: "Foo::Student"
accepts_nested_attributes_for :students
end
3 changes: 0 additions & 3 deletions spec/dummy/app/models/student.rb

This file was deleted.

4 changes: 3 additions & 1 deletion spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
namespace :admin do
root to: redirect("/admin/schools")
resources :schools
resources :students
namespace :foo do
resources :students
end
end
end
2 changes: 1 addition & 1 deletion spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
sequence(:name) { |i| "School ##{i}" }
end

factory :student do
factory :student, class: Foo::Student do
sequence(:name) { |i| "Student ##{i}" }
end
end
24 changes: 22 additions & 2 deletions spec/features/has_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,33 @@
FactoryBot.create(:student, name: "John Doe", school: school)
end

scenario "index page" do
scenario "index" do
visit admin_schools_path
expect(page).to have_content("School of Life")
end

scenario "show page" do
scenario "show" do
visit admin_school_path(school)
expect(page).to have_content("John Doe")
end

scenario "new" do
visit new_admin_school_path
expect(page).to have_content("New Schools")
end

scenario "create", js: true do
visit new_admin_school_path
expect(page).to have_content("New Schools")
expect(page).to have_content("Add Foo/Student")
fill_in "Name", with: "La Ferme du Bec Hellouin"
click_link "Add Foo/Student"
expect(page).to have_content("Remove Foo/Student")
within(".nested-fields") do
fill_in "Name", with: "Sébastien"
end
click_button "Create School"
expect(page).to have_text("La Ferme du Bec Hellouin")
expect(page).to have_text("Sébastien")
end
end
2 changes: 2 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
require 'factory_bot'
require_relative './factories'

Capybara.server = :webrick

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
Expand Down

0 comments on commit 743f0a1

Please sign in to comment.