RoR: Listing of subrecords
Schema:
persons (id, name, birthyear, gender)
pets (id, person_id, name, leg_count)
plants (id, person_id, kind, qty)
I would like to make a read-only report about these things grouped by persons. The listing of personns is done (without the associated records). I would like to have "subtables" per persons. Something like:
Persons
+----+------+-----------+--------+
| id | name | birthyear | gender |
+----+------+-----------+--------+
| 1 | Joe | 1980 | M |
+----+------+-----------+--------+
| Pets |
| +----+------+-----------+ |
| | id | name | Leg count | |
| +----+------+-----------+ |
| | 1 | Rex | 4 | |
| +----+------+-----------+ |
| | 2 | Ka | 0 | |
| +----+------+-----------+ |
| Plants |
| +----+------------+-----+ |
| | id | kind | qty | |
| +----+------------+-----+ |
| | 1 | lemon tree | 2 | |
| +----+------------+-----+ |
+----+------+-----------+--------+
| 2 | Jane | 1982 | F |
+----+------+-----------+--------+
| Pets |
| +----+------+-----------+ |
| | id | name | Leg count | |
| +----+------+-----------+ |
| | 3 | Sue | 6 | |
| +----+------+-----------+ |
| Plants |
| +----+------------+-----+ |
| | id | kind | qty | |
| +----+------------+-----+ |
| | 2 | Oak tree | 1 | |
| +----+------------+-----+ |
+----+------+-----------+--------+
Can you please hel开发者_开发问答p me with some tips where and how to hook to the framework? (JRuby (1.5.0), Ruby on Rails (2.3.4), ActiveRecord (2.3.4) )
What is done
Persons
+----+------+-----------+--------+
| id | name | birthyear | gender |
+----+------+-----------+--------+
| 1 | Joe | 1980 | M |
+----+------+-----------+--------+
| 2 | Jane | 1982 | F |
+----+------+-----------+--------+
It is done using:
class PersonsReportController < PersonsController
layout 'simpreport'
active_scaffold :person do |config|
c.columns = [:name, :birthyear, :gender, :pets, :plants ]
c.label = "Report of people and other living creatures"
[:pets, :plants].each do |col|
columns[col].clear_link
end
c.list.columns.exclude :pets, :plants
c.actions.exclude :show
end
# ...
end
And I also customized _list_header.rhtml
, _list_column_headings.rhtml
, and _list_actions.rhtml
a little bit in order to kill all interactivity (like orderings and so on).
I don't understand the code you have written in your controller, nor why it inherits from your PersonsController. Maybe you can explain a bit more what you are trying to accomplish there?
That being said, I would solve your problem like this:
Models:
person.rb:
class Person < ActiveRecord::Base
has_many :pets
has_many :plants
def has_pets?
self.pets.size > 0
end
def has_plants?
self.plants.size > 0
end
def has_pets_or_plants?
self.has_pets? || self.has_plants?
end
end
pet.rb:
class Pet < ActiveRecord::Base
belongs_to :person
end
plant.rb:
class Plant < ActiveRecord::Base
belongs_to :person
end
Controller:
reports_controller.rb:
class ReportsController < ApplicationController
def index
@persons = Person.find(:all)
end
end
View:
reports/index.html.erb:
Persons
<table border="1">
<tr>
<td>id</td>
<td>name</td>
<td>birthyear</td>
<td>gender</td>
</tr>
<% @persons.each do |person| -%>
<tr>
<td><%= person.id %></td>
<td><%= person.name %></td>
<td><%= person.birthyear %></td>
<td><%= person.gender %></td>
</tr>
<% if person.has_pets_or_plants? -%>
<tr>
<td colspan="4">
<% if person.has_pets? -%>
Pets
<table border="1">
<tr>
<td>id</td>
<td>name</td>
<td>leg count</td>
</tr>
<% person.pets.each do |pet| -%>
<tr>
<td><%= pet.id %></td>
<td><%= pet.name %></td>
<td><%= pet.leg_count %></td>
</tr>
<% end -%>
</table>
<% end -%>
<% if person.has_plants? -%>
Plants
<table border="1">
<tr>
<td>id</td>
<td>kind</td>
<td>qty</td>
</tr>
<% person.plants.each do |plant| -%>
<tr>
<td><%= plant.id %></td>
<td><%= plant.kind %></td>
<td><%= plant.qty %></td>
</tr>
<% end -%>
</table>
<% end -%>
</td>
</tr>
<% end -%>
<% end -%>
</table>
Here's a solution that works inside ActiveScaffold:
app/controllers/people_controller.rb
class PeopleController < ApplicationController
active_scaffold :person do |config|
config.label = "Report of people and other living creatures"
config.actions.exclude :show, :delete, :edit
# this sets up clickable links for pets and plants.
# clicking either link will expand BOTH child object collections.
config.columns[:pets].set_link('nested', :parameters => {:associations => "pets plants"})
config.columns[:plants].set_link('nested', :parameters => {:associations => "pets plants"})
# uncomment these if you want to allow editing of pets and plants
# config.nested.add_link("Person's pets", [:pets])
# config.nested.add_link("Person's plants", [:plants])
end
end
Now the report opens up with the child tables collapsed, so we have to expand them using event.simulate.js from protolicious.
Download protolicious and copy event.simulate.js to your public/javascripts directory.
Now include event.simulate.js in your layout:
<%= javascript_include_tag :defaults %>
<%= javascript_include_tag "event.simulate.js" %>
<%= active_scaffold_includes %>
And add this script tag to the bottom of your view:
<script type="text/javascript">
// iterates through each ActiveScaffold nested item link and clicks it
$$('a.nested').each(function(link, index) {
link.simulate('click');
});
</script>
Given the following models
app/models/person.rb
class Person < ActiveRecord::Base
has_many :pets
has_many :plants
end
app/models/pet.rb
class Pet < ActiveRecord::Base
belongs_to :person
end
app/models/plant.rb
class Plant < ActiveRecord::Base
belongs_to :person
end
精彩评论