Rails Administrate - How to Customize Navigation Link Text - ruby-on-rails

In administrate if I have a model called parentequipmenttype how to I customize the Navigation link to display as "Parent Equipment Type" instead of listing as parentequipmenttype?

You will need to customize app/dashboards/parentequipmenttype_dashboard.rb file.
require "administrate/custom_dashboard"
class ParentequipmenttypeDashboard < Administrate::CustomDashboard
resource "Parent Equipment Type"
end

Related

Customizing fields with the administrate gem for rails

I cannot seem to find any documentation on how to modify the administrate gem's default dashboards in order to customize what gets displayed in the index and show pages. Here's my specific goal:
Given that an Article belongs_to an Author
When I create an Article
I want to see the Author's last name in the dropdown list for the associated field
And once saved, I want to see the Author's last name in the Article's index and show pages
Right now, instead, I get a not-so-useful "Author #4" as the record label. Here's the automatically generated dashboard:
class ArticleDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
author: Field::BelongsTo,
id: Field::Number,
title: Field::String,
content: Field::Text,
created_at: Field::DateTime,
updated_at: Field::DateTime,
}.freeze
[snip]
end
The "Customizing Dashboard" documentation page says:
Each of the Field types take a different set of options, which are specified through the .with_options class method.
So I figure that calling with_options on Field::BelongsTo might be the way to go, but what options are available for that field (or for any other, for that matter)?
In administrate, you may customize how a resource is displayed by overwriting the #display_resource method in the resource's dashboard.
All of your dashboards inherit from Administrate::BaseDashboard, which uses the following method to display resources:
def display_resource(resource)
"#{resource.class} ##{resource.id}"
end
You'll want to add something like this to the AuthorDashboard to overwrite the default:
def display_resource(author)
author.last_name
end

How to add a report/custom page to ActiveAdmin?

I am using ActiveAdmin for my Ruby on Rails application. My model consists of a couple entites such as "Plan", "Country", etc. I would like to have custom page for reporting purposes. This page should work on a collection of selected plans. So, I need scopes and filters. Currently one page associated with "Plan" model for CRUD operations. I don't want to add anything else to my data model or database since I have everything I need in my current model. How could I make such a page and access it through the menu?
Could I associate another page with my "Plan" model as follows?
ActiveAdmin.register Plan do
menu label: 'Report', parent: 'Planning', priority: 2
...
end
Could I have a custom page working on set of plans selected using scopes and filters?
ActiveAdmin.register_page 'Report' do
menu parent: 'Planning', priority: 2
...
end
Or, how could I add a new page/route to the currently associated page with "Plan" model for reporting? Can collection_action help me? If yes, how could I exactly bring a collection of Plans and show them?
You can register your ActiveRecord model several times in ActiveAdmin as a new resource:
ActiveAdmin.register Plan, as: 'PlanReport' do
menu label: 'Report', parent: 'Planning', priority: 2
...
end
In this file you can use all of the ActiveAdmin features like scope and filters.
https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#rename-the-resource

rails+rails_admin customize content of a specific field

I'm using rails_admin gem.
I'm customizing the edit action of a model, so there are many fields that display by default rails_admin.
There is a field to select some value using a select box for multiple value.
Is possible to customize the content of this field? by default rails_admin show me the name of the attribute, but i want to display the name and other attribute like: id and created_at.
Is possible?
SOLUTION
in the model of the field to customize:
#app/models/my_model.rb
rails_admin do
object_label_method :to_label
end
def to_label
"#{name}-#{attribute}-#{other-attribute}"
end

What is the best way to model a restaurant menu, capable of future editing?

I am creating a website with Active Admin to allow the owners future control over the menu. If we are working on the lunch menu, I have structured the models as a section that has_many items shown below.
class Section < ActiveRecord::Base
attr_accessible :id, :name
has_many :items
end
class Item < ActiveRecord::Base
attr_accessible :desc, :id, :name, :price
belongs_to :section
end
Creating a "section" works fine in Active Admin, but I am receiving an error in when trying to create an "item".
Error:
NoMethodError in Admin/items#new
undefined method `section_id' for #<Item:0xb5460b44>
Thanks.
For the sake of flexibility you should consider not creating categories as models but as mere attributes. Once you create all the classes(ex. LunchSection, DinnerSection, WineSection) it's impossible to create a new one without programmer's participation.
What I would go for is create following classes:
Menu - representing a menu as in a separate piece of paper (so there could be a wine menu and dish menu for ex.). The "type" of the menu should be designated only by name.
Group or Category is a container for diffrent dishes of the same type like lunch, dessert, wine... as wel, theres just one Group class and there's an instance for each single group
MenuItem (or simply Item) is any element of a menu, belonging to group or menu (this is a design decision - items should not belong to both menu and group because that would cause conflict if you wanted to get all items in a menu)

Cucumber and webrat - How to handle dynamic URLs in the paths.rb?

I am using Cucumber for BDD development in my Ruby on Rails project and I'm running into some confusion on how the path.rb handles paths used in rails applications.
Given I have:
class Parent < ActiveRecord::Base
has_many :children
end
class Child < ActiveRecord::Base
belongs_to :parent
end
and I have the following Cucumber feature:
Scenario: A test feature
Given I am on the parent page
When I follow "Link to Children"
Then I should be on the children list page
with the path defined as:
def path_to(page_name)
case page_name
when /the children list page/
'/parents/:id/children'
end
The problem I come across is the following error when running the feature:
Spec::Expectations::ExpectationNotMetError: expected: "/parents/:id/children",
got: "/parents/1726/children" (using ==)
I don't really care what the :id is. What should I do instead? Is this even possible with the default web steps? Am I thinking about the problem in the wrong way?
The way I do it, which may not be the best way is as follows:
when /the children list page for "(.+)"/
p = Parent.find_by_name($1)
parent_children_path(p)
In our app, we always wanted a new record in the database whenever a user clicked the "new" button. Thus, our controller's new action automatically calls create and then redirects to the edit action.
We faced a similar problem in testing, when we didn't care so much about what the ID was -- just that it got to the edit page for the app.
Here's what I came up with.
(NOTE: The step definition is written using capybara, but it shouldn't be too different from webrat)
Then /^(?:|I )should now be editing the (.*)$/ do |model|
id = find_by_id("#{model}_id").value
Then "I should be on the edit #{model} page for \"#{id}\""
end
The basic premise is that when you're on a Rails edit page, there will be a form for the model you're editing. That form always contains a hidden field with the ID of the specific record you're editing.
The step finds the hidden field, extracts the ID from it, and then looks for a web_step to resolve the path for that model.
Just make sure you have a path that matches for the model you're looking up.
when /the edit person page for "([^\"]*)"/
edit_person_path($1)

Resources