Skip to content

Commit

Permalink
Support for models with uncountable inflections (#934)
Browse files Browse the repository at this point in the history
* Uses ActiveModel route_key to determine the correct name.
* Adds an example dashboard/controller, which will throw an
  ActionController::UrlGenerationError when rendering the navigation.
  • Loading branch information
nickcharlton authored Jul 14, 2017
1 parent 67f2691 commit 1148133
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 8 deletions.
13 changes: 9 additions & 4 deletions app/helpers/administrate/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ def render_field(field, locals = {})
render locals: locals, partial: field.to_partial_path
end

def class_from_resource(resource_name)
resource_name.to_s.classify.constantize
end

def display_resource_name(resource_name)
resource_name.
to_s.
classify.
constantize.
class_from_resource(resource_name).
model_name.
human(
count: PLURAL_MANY_COUNT,
Expand All @@ -27,6 +28,10 @@ def sort_order(order)
end
end

def resource_index_route_key(resource_name)
ActiveModel::Naming.route_key(class_from_resource(resource_name))
end

def sanitized_order_params
params.permit(:search, :id, :order, :page, :per_page, :direction, :orders)
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/administrate/application/_navigation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ as defined by the routes in the `admin/` namespace
<% Administrate::Namespace.new(namespace).resources.each do |resource| %>
<%= link_to(
display_resource_name(resource),
[namespace, resource.path],
[namespace, resource_index_route_key(resource)],
class: "navigation__link navigation__link--#{nav_link_state(resource)}"
) %>
<% end %>
Expand Down
4 changes: 4 additions & 0 deletions spec/example_app/app/controllers/admin/series_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Admin
class SeriesController < Admin::ApplicationController
end
end
16 changes: 16 additions & 0 deletions spec/example_app/app/dashboards/series_dashboard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "administrate/base_dashboard"

class SeriesDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
id: Field::Number,
name: Field::String,
}.freeze

COLLECTION_ATTRIBUTES = %i(id name).freeze

SHOW_PAGE_ATTRIBUTES = %i(id name).freeze

FORM_ATTRIBUTES = [
:name,
].freeze
end
3 changes: 3 additions & 0 deletions spec/example_app/app/models/series.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Series < ActiveRecord::Base
validates :name, presence: true
end
6 changes: 3 additions & 3 deletions spec/example_app/config/initializers/inflections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
# Add new inflection rules using the following format. Inflections
# are locale specific, and you may define rules for as many different
# locales as you wish. All of these examples are active by default:
# ActiveSupport::Inflector.inflections(:en) do |inflect|
ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people'
# inflect.uncountable %w( fish sheep )
# end
inflect.uncountable %w(series)
end

# These inflection rules are supported but not enabled by default:
# ActiveSupport::Inflector.inflections(:en) do |inflect|
Expand Down
1 change: 1 addition & 0 deletions spec/example_app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
resources :products
resources :product_meta_tags
resources :payments, only: [:index, :show]
resources :series

namespace :blog do
resources :posts
Expand Down
7 changes: 7 additions & 0 deletions spec/example_app/db/migrate/20160117011028_create_series.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CreateSeries < ActiveRecord::Migration[5.1]
def change
create_table :series do |t|
t.string :name, null: false
end
end
end
4 changes: 4 additions & 0 deletions spec/example_app/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
t.index ["slug"], name: "index_products_on_slug", unique: true
end

create_table "series", force: :cascade do |t|
t.string "name", null: false
end

add_foreign_key "line_items", "orders"
add_foreign_key "line_items", "products"
add_foreign_key "orders", "customers"
Expand Down
3 changes: 3 additions & 0 deletions spec/example_app/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Customer.destroy_all
Product.destroy_all
ProductMetaTag.destroy_all
Series.destroy_all

100.times do
name = "#{Faker::Name.first_name} #{Faker::Name.last_name}"
Expand Down Expand Up @@ -52,3 +53,5 @@
end
end
end

Series.create(name: "An example")
4 changes: 4 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@
sequence(:title) { |n| "Post #{n}" }
body "Empty"
end

factory :series do
sequence(:name) { |n| "Series #{n}" }
end
end
12 changes: 12 additions & 0 deletions spec/helpers/administrate/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,16 @@
end
end
end

describe "#resource_index_route_key" do
it "handles index routes when resource is uncountable" do
route_key = resource_index_route_key(:series)
expect(route_key).to eq("series_index")
end

it "handles normal inflection" do
route_key = resource_index_route_key(:customer)
expect(route_key).to eq("customers")
end
end
end

0 comments on commit 1148133

Please sign in to comment.