2

困境:

我处于需要跨多个视图和控制器显示用户活动的情况。代码开始有点味道,所以我非常感谢一些清理它的指导。

我有一个用户控制器,它有一个“显示”(/user_id/)视图,可以在侧边栏中呈现最近的活动,还有一个“活动”(user_id/activity)视图,可以呈现所有用户的活动。我还有一个“页面”控制器,它有一个“仪表板”视图,它也呈现用户的活动。

编码:

users_controller.rb好像:

@activities = []
@activities += @user.skills.map { |skill| Activity.new("skill", skill, skill.created_at) }
@activities += @user.unions.map { |union| Activity.new("union", union, union.created_at) }
@activities += @user.companies.map { |company| Activity.new("company", company, company.created_at) }
@activities += @user.clients.map { |client| Activity.new("client", client, client.created_at) }
@activities += @user.schools.map { |school| Activity.new("school", school, school.created_at) }
@activities += @following_list.map { |following| Activity.new("following", following, following.created_at) }
@activities += @recommended.map { |recommend| Activity.new("recommend", recommend, recommend.created_at) }
@activities += @user.statuses.all(:limit => 5, :order => "created_at DESC").map { |status| Activity.new("status", status, status.created_at) }

sorted_activities = @activities.sort_by(&:date).reverse
@activities = sorted_activities[0..(4)]

pages_controller.rb好像:

@your_activity = []
@your_activity += current_user.skills.map { |skill| ActivityDashboard.new("#{current_user.first_name} #{current_user.last_name}","#{current_user.id}","#{current_user.avatar}","skill", skill, skill.created_at) }
@your_activity += current_user.unions.map { |union| ActivityDashboard.new("#{current_user.first_name} #{current_user.last_name}","#{current_user.id}","#{current_user.avatar}","union", union, union.created_at) }
@your_activity += current_user.companies.map { |company| ActivityDashboard.new("#{current_user.first_name} #{current_user.last_name}","#{current_user.id}","#{current_user.avatar}","company", company, company.created_at) }
@your_activity += current_user.clients.map { |client| ActivityDashboard.new("#{current_user.first_name} #{current_user.last_name}","#{current_user.id}","#{current_user.avatar}","client", client, client.created_at) }
@your_activity += current_user.schools.map { |school| ActivityDashboard.new("#{current_user.first_name} #{current_user.last_name}","#{current_user.id}","#{current_user.avatar}","school", school, school.created_at) }
@your_activity += current_user.statuses.map { |status| ActivityDashboard.new("#{current_user.first_name} #{current_user.last_name}","#{current_user.id}","#{current_user.avatar}","status", status, status.created_at) }

sorted_activities = @your_activity.sort_by(&:date).reverse
@your_activity = sorted_activities[0..(35)]
4

1 回答 1

1

有几种方法可以真正做到。您可以将其卸载到模型中,您可以在 lib 目录中创建一个模块并使用它来构建活动数组。还有一件重要的事情是确保使用帮助程序在页面上显示它,并根据您的需要将其加载到视图或部分视图中。

您还可以通过一些元编程来减少重复。您可能需要根据您的要求进行调整。您还需要为第一组代码设置它

["skill", "union", "company", "client", "school", "status"].each do |type|
  plural = ActiveSupport::Inflector.pluralize(type)
  @your_activity += current_user.send(plural.to_sym).map do |object|
    ActivityDashboard.new("#{current_user.first_name} #{current_user.last_name}","#{current_user.id}","#{current_user.avatar}",type, object, object.created_at)
  end
end

我的工作示例的要点:https ://gist.github.com/3669117

于 2012-09-07T19:45:55.047 回答