8000 Support id_column and index_column in table_for by c960657 · Pull Request #6461 · activeadmin/activeadmin · GitHub
[go: up one dir, main page]

Skip to content

Support id_column and index_column in table_for #6461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
16 changes: 7 additions & 9 deletions docs/12-arbre-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,23 @@ uses `column` to build the fields to show with the table.

```ruby
table_for order.payments do
index_column
id_column
column(:payment_type) { |payment| payment.payment_type.titleize }
column "Received On", :created_at
column "Details & Notes", :payment_details
column "Amount", :amount_in_dollars
end
```

The `column` method can take a title as its first argument and data
(`:your_method`) as its second (or first if no title provided). Column also
takes a block.
`column`, `index_column`, and `id_column` work like for [index tables](3-index-pages/index-as-table.md).

### Internationalization

To customize the internationalization for the component, specify a resource to
use for translations via the `i18n` named parameter. This is only necessary for
non-`ActiveRecord::Relation` collections:
If `table_for` is called with a non-`ActiveRecord::Relation` collection, you must
specify the resource class in order to use `id_column`. This class is also used
for translations of column headers.

```ruby
table_for payments, i18n: Payment do
table_for payments, resource_class: Payment do
# ...
end
```
Expand Down
12 changes: 6 additions & 6 deletions features/belongs_to.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Feature: Belongs To
"""
ActiveAdmin.register User
ActiveAdmin.register Post do
belongs_to :user
belongs_to :author, class_name: "User", param: "user_id", route_name: "user"
end
"""
When I go to the last author's posts
Expand All @@ -27,7 +27,7 @@ Feature: Belongs To
"""
ActiveAdmin.register User
ActiveAdmin.register Post do
belongs_to :user
belongs_to :author, class_name: "User", param: "user_id", route_name: "user"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these and the other changes in this file related to this PR?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they are needed. I am not too familiar with belongs_to, but AFAICT the arguments are necessary for this to work at all (because the class name, User, does not match the attribute name, author). But I don't understand why the tests did not fail before. I'll try to figure out what is going on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you figure it out @c960657?

permit_params :title, :body, :published_date

form do |f|
Expand Down Expand Up @@ -76,7 +76,7 @@ Feature: Belongs To
"""
ActiveAdmin.register User
ActiveAdmin.register Post do
belongs_to :user
belongs_to :author, class_name: "User", param: "user_id", route_name: "user"
permit_params :title, :body, :published_date

form do |f|
Expand Down Expand Up @@ -125,7 +125,7 @@ Feature: Belongs To
"""
ActiveAdmin.register User
ActiveAdmin.register Post do
belongs_to :user
belongs_to :author, class_name: "User", param: "user_id", route_name: "user"
end
"""
When I go to the last author's posts
Expand All @@ -138,7 +138,7 @@ Feature: Belongs To
"""
ActiveAdmin.register User
ActiveAdmin.register Post do
belongs_to :user, optional: true
belongs_to :author, class_name: "User", param: "user_id", route_name: "user", optional: true
end
"""< 8000 /span>
When I go to the last author's posts
Expand All @@ -153,7 +153,7 @@ Feature: Belongs To
"""
ActiveAdmin.register User
ActiveAdmin.register Post do
belongs_to :user
belongs_to :author, class_name: "User", param: "user_id", route_name: "user"
navigation_menu :user
end
"""
Expand Down
2 changes: 1 addition & 1 deletion features/index/batch_actions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Feature: Batch Actions
"""
ActiveAdmin.register User
ActiveAdmin.register Post do
belongs_to :user
belongs_to :author, class_name: "User", param: "user_id", route_name: "user"
end
"""
When I go to the last author's posts
Expand Down
30 changes: 29 additions & 1 deletion lib/active_admin/views/components/table_for.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ def build(obj, *attrs)
options = attrs.extract_options!
@sortable = options.delete(:sortable)
@collection = obj.respond_to?(:each) && !obj.is_a?(Hash) ? obj : [obj]
@resource_class = options.delete(:i18n)
@resource_class = options.delete(:resource_class)
if options.key? :i18n
Deprecation.warn "The `i18n` parameter is deprecated; use `resource_class` instead."
@resource_class = options.delete(:i18n)
end
@resource_class ||= @collection.klass if @collection.respond_to? :klass

@columns = []
Expand All @@ -27,6 +31,30 @@ def columns(*attrs)
attrs.each { |attr| column(attr) }
end

def index_column(start_value = 1)
column "#", class: "col-index", sortable: false do |resource|
offset_value = @collection.offset_value if @collection.respond_to? :offset_value
offset_value ||= 0
offset_value + @collection.index(resource) + start_value
end
end

def id_column
raise "Resource class not specified!" unless @resource_class
raise "#{@resource_class.name} has no primary_key!" unless @resource_class.primary_key
config = active_admin_resource_for(@resource_class)

column(@resource_class.human_attribute_name(@resource_class.primary_key), sortable: @resource_class.primary_key) do |resource|
if config && config.controller.action_methods.include?("show")
link_to resource.id, config.route_instance_path(resource), class: "resource_id_link"
elsif config && config.controller.action_methods.include?("edit")
link_to resource.id, config.route_edit_instance_path(resource), class: "resource_id_link"
else
resource.id
end
end
end

def column(*args, &block)
options = default_options.merge(args.extract_options!)
title = args[0]
Expand Down
22 changes: 1 addition & 21 deletions lib/active_admin/views/index_as_table.rb
F438
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def build(page_presenter, collection)
id: "index_table_#{active_admin_config.resource_name.plural}",
sortable: true,
class: "index_table index",
i18n: active_admin_config.resource_class,
resource_class: active_admin_config.resource_class,
paginator: page_presenter[:paginator] != false,
row_class: page_presenter[:row_class]
}
Expand Down Expand Up @@ -284,26 +284,6 @@ def selectable_column
end
end

def index_column(start_value = 1)
column "#", class: "col-index", sortable: false do |resource|
@collection.offset_value + @collection.index(resource) + start_value
end
end

# Display a column for the id
def id_column
raise "#{resource_class.name} has no primary_key!" unless resource_class.primary_key
column(resource_class.human_attribute_name(resource_class.primary_key), sortable: resource_class.primary_key) do |resource|
if controller.action_methods.include?("show")
link_to resource.id, resource_path(resource), class: "resource_id_link"
elsif controller.action_methods.include?("edit")
link_to resource.id, edit_resource_path(resource), class: "resource_id_link"
else
resource.id
end
end
end

def default_actions
raise "`default_actions` is no longer provided in ActiveAdmin 1.x. Use `actions` instead."
end
Expand Down
80 changes: 75 additions & 5 deletions spec/unit/views/components/table_for_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
describe "creating with the dsl" do
let(:collection) do
[
Post.new(title: "First Post", starred: true),
Post.new(title: "Second Post"),
Post.new(title: "Third Post", starred: false)
Post.new(id: 1, title: "First Post", starred: true),
Post.new(id: 2, title: "Second Post"),
Post.new(id: 4, title: "Third Post", starred: false)
]
end

Expand Down Expand Up @@ -100,6 +100,54 @@
end
end

context "when creating an id column" do
let(:table) do
render_arbre_component assigns, helpers do
table_for(collection, resource_class: Post) do
id_column
end
end
end

it "should create a table header based on the primary key" do
expect(table.find_by_tag("th").first.content).to eq "Id"
end

it "should create a table row for each element in the collection" do
expect(table.find_by_tag("tr").size).to eq 4 # 1 for head, 3 for rows
end

["1", "2", "4"].each_with_index do |content, index|
it "should create a cell with #{content}" do
expect(table.find_by_tag("td")[index].content).to eq content
end
end
end

context "when creating an index column" do
let(:table) do
render_arbre_component assigns, helpers do
table_for(collection) do
index_column(100)
end
end
end

it "should create a table header based on the primary key" do
expect(table.find_by_tag("th").first.content).to eq "#"
end

it "should create a table row for each element in the collection" do
expect(table.find_by_tag("tr").size).to eq 4 # 1 for head, 3 for rows
end

["100", "101", "102"].each_with_index do |content, index|
it "should create a cell with #{content}" do
expect(table.find_by_tag("td")[index].content).to eq content
end
end
end

context "when creating a column with a symbol" do
let(:table) do
render_arbre_component assigns, helpers do
Expand Down Expand Up @@ -307,13 +355,35 @@
end
end

context "when i18n option is specified" do
context "when resource_class option is specified" do
around do |example|
with_translation %i[activerecord attributes post title], "Name" do
example.call
end
end

let(:table) do
render_arbre_component assigns, helpers do
table_for(collection, resource_class: Post) do
column :title
end
end
end

it "should use localized column key" do
expect(table.find_by_tag("th").first.content).to eq "Name"
end
end

context "when i18n option is specified" do
around do |example|
with_translation(activerecord: { attributes: { post: { title: "Name" } } }) do
ActiveSupport::Deprecation.silence do
example.call
end
end
end

let(:table) do
render_arbre_component assigns, helpers do
table_for(collection, i18n: Post) do
Expand All @@ -327,7 +397,7 @@
end
end

context "when i18n option is not specified" do
context "when neither i18n nor resource_class option is not specified" do
around do |example|
with_translation %i[activerecord attributes post title], "Name" do
example.call
Expand Down
0