Skip to content

Commit

Permalink
#46: add a navigation label override for models
Browse files Browse the repository at this point in the history
  • Loading branch information
danielboggs committed Mar 28, 2016
1 parent f029f8a commit f3ccf31
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 17 deletions.
6 changes: 3 additions & 3 deletions app/controllers/tolaria/resource_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def create
display_name = Tolaria.display_name(@resource)

if @resource.save
flash[:success] = "#{random_blingword} You created the #{@managed_class.model_name.human}#{display_name}”."
flash[:success] = "#{random_blingword} You created the #{@managed_class.navigation_label.singularize}#{display_name}”."
return redirect_to form_completion_redirect_path(@managed_class, @resource)
else
log_validation_errors!
Expand All @@ -54,7 +54,7 @@ def update
display_name = Tolaria.display_name(@resource)

if @resource.save
flash[:success] = "#{random_blingword} You updated the #{@managed_class.model_name.human.downcase}#{display_name}”."
flash[:success] = "#{random_blingword} You updated the #{@managed_class.navigation_label.singularize.downcase}#{display_name}”."
return redirect_to form_completion_redirect_path(@managed_class, @resource)
else
log_validation_errors!
Expand All @@ -76,7 +76,7 @@ def destroy
return redirect_to form_completion_redirect_path(@managed_class, @resource)
end

flash[:destructive] = "You deleted the #{@managed_class.model_name.human.downcase}#{display_name}”."
flash[:destructive] = "You deleted the #{@managed_class.navigation_label.singularize.downcase}#{display_name}”."
return redirect_to form_completion_redirect_path(@managed_class)

end
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/shared/_navigation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<% if managed_class.allows?(:index) %>
<li>
<%= tolaria_navigation_link(
managed_class.model_name.human.pluralize.titleize,
managed_class.navigation_label,
managed_class.icon,
url_for(action:"index", controller:managed_class.plural)
) %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/admin/tolaria_resource/_index_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<div class="blank-slate">
<span class="blank-slate-icons"><%= fontawesome_icon @managed_class.icon %></span>
<h3 class="blank-slate-header">
No <%= @managed_class.model_name.human.pluralize.titleize %> live here.
No <%= @managed_class.navigation_label %> live here.
</h3>
<% if @managed_class.allows?(:new) %>
<p class="blank-slate-text">Get started on the first one:</p>
<%= link_to url_for(action:"new"), class:"button -primary" do %>
<%= fontawesome_icon :plus %>
New <%= @managed_class.model_name.human.titleize %>
New <%= @managed_class.navigation_label.singularize %>
<% end %>
<% end %>
</div>
Expand All @@ -23,7 +23,7 @@
<div class="blank-slate">
<span class="blank-slate-icons"><%= fontawesome_icon :search %></span>
<h3 class="blank-slate-header">
Your search didn't find any <%= @managed_class.model_name.human.pluralize.titleize %>.
Your search didn't find any <%= @managed_class.navigation_label %>.
</h3>
<p class="blank-slate-text">
You can <%= link_to "clear the search form", url_for([:admin, @managed_class.klass]) %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/tolaria_resource/_search_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div class="search-form-controlls">
<%= f.button type:"submit", class:"button -primary", name:nil do %>
<%= fontawesome_icon :search %>
Search <%= @managed_class.model_name.human.pluralize.titleize %>
Search <%= @managed_class.navigation_label %>
<% end %>
<%= link_to url_for([:admin, @managed_class.klass]), class:"button" do %>
<%= fontawesome_icon :close %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/tolaria_resource/_show_buttons.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<% if @managed_class.allows?(:edit) %>
<%= link_to url_for(action:"edit", id:@resource.id), class:"button -primary" do %>
<%= fontawesome_icon :pencil %>
Edit This <%= @managed_class.model_name.human.titleize %>
Edit This <%= @managed_class.navigation_label.singularize %>
<% end %>
<% end %>
6 changes: 3 additions & 3 deletions app/views/admin/tolaria_resource/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% if @resource.persisted? %>
<%= content_for :title, "#{Tolaria.display_name(@resource)}" %>
<% else %>
<%= content_for :title, "New #{@managed_class.model_name.human.titleize}" %>
<%= content_for :title, "New #{@managed_class.navigation_label.singularize}" %>
<% end %>
<%= form_for [:admin, @resource], url:contextual_form_url, builder:Admin::FormBuilder, html:{class:"resource-form"} do |form_builder| %>
Expand All @@ -18,9 +18,9 @@
<span class="crumb">
<%= fontawesome_icon @managed_class.icon %>
<% if @managed_class.allows? :index %>
<%= link_to @managed_class.model_name.human.pluralize.titleize, url_for(action:"index", controller:@managed_class.plural, q:params[:q]) %>
<%= link_to @managed_class.navigation_label, url_for(action:"index", controller:@managed_class.plural, q:params[:q]) %>
<% else %>
<%= @managed_class.model_name.human.pluralize.titleize %>
<%= @managed_class.navigation_label %>
<% end %>
</span>
<%= content_for :title %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/tolaria_resource/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= content_for :title, "#{@managed_class.model_name.human.pluralize.titleize}" %>
<%= content_for :title, "#{@managed_class.navigation_label}" %>

<div class="main-controls">

Expand Down Expand Up @@ -28,7 +28,7 @@
<% if @managed_class.allows?(:new) %>
<%= link_to url_for(action:"new", q:params[:q]), class:"button -primary" do %>
<%= fontawesome_icon :plus %>
New <%= @managed_class.model_name.human.titleize %>
New <%= @managed_class.navigation_label.singularize %>
<% end %>
<% end %>

Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/tolaria_resource/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<span class="crumb">
<%= fontawesome_icon @managed_class.icon %>
<% if @managed_class.allows? :index %>
<%= link_to @managed_class.model_name.human.pluralize.titleize, url_for(action:"index", controller:@managed_class.plural, q:params[:q]) %>
<%= link_to @managed_class.navigation_label, url_for(action:"index", controller:@managed_class.plural, q:params[:q]) %>
<% else %>
<%= @managed_class.model_name.human.pluralize.titleize %>
<%= @managed_class.navigation_label %>
<% end %>
</span>
<%= content_for :title %>
Expand Down
2 changes: 2 additions & 0 deletions lib/tolaria/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class ActiveRecord::Base
# Tolaria will pass this array as the `only:` option to the router.
# The default includes all CRUD actions:
# `[:index, :show, :new, :create, :edit, :update, :destroy]`
# - `:navigation_label` - The navigation label to use for this resource.
# The default is `self.model_name.human.pluralize.titleize`.
#
# #### Example
#
Expand Down
6 changes: 5 additions & 1 deletion lib/tolaria/managed_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ class ManagedClass
# A stored symbol for the `params.permit` key for this resource
attr_accessor :param_key

# A String to override the model's label in the primary admin navigation
attr_accessor :navigation_label

# A factory method that registers a new model in Tolaria and configures
# its menu and param settings. Developers should use `ActiveRecord::Base.manage_with_tolaria`
def self.create(klass, icon:"file-o", permit_params:[], priority:10, category:"Settings", default_order:"id DESC", paginated:true, allowed_actions:[:index, :show, :new, :create, :edit, :update, :destroy])
def self.create(klass, icon:"file-o", permit_params:[], priority:10, category:"Settings", default_order:"id DESC", paginated:true, allowed_actions:[:index, :show, :new, :create, :edit, :update, :destroy], navigation_label: klass.model_name.human.pluralize.titleize)

managed_class = self.new
managed_class.klass = klass
Expand All @@ -49,6 +52,7 @@ def self.create(klass, icon:"file-o", permit_params:[], priority:10, category:"S
managed_class.paginated = paginated.present?
managed_class.permitted_params = permit_params.freeze
managed_class.allowed_actions = allowed_actions.freeze
managed_class.navigation_label = navigation_label.freeze

# Set auto-generated attributes
managed_class.controller_name = "#{managed_class.model_name.collection.camelize}Controller".freeze
Expand Down
4 changes: 4 additions & 0 deletions test/unit/managed_classes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ManagedClassesTest < ActiveSupport::TestCase

class ::Card < ActiveRecord::Base
manage_with_tolaria using:{
navigation_label: "Widgets",
icon: "credit-card",
priority: 5,
category: "Payments",
Expand All @@ -32,6 +33,9 @@ class ::Card < ActiveRecord::Base
assert_equal managed_class.allowed_actions, [:index, :show]
assert managed_class.paginated, true

# Can we override the navigation label?
assert_equal managed_class.navigation_label, "Widgets"

# Can we check action allowances?
assert managed_class.allows?(:index), "should allow `index` action"
assert managed_class.allows?(:show), "should allow `show` action"
Expand Down

0 comments on commit f3ccf31

Please sign in to comment.