清理重复的控制器代码

时间:2012-09-07 19:03:00

标签: ruby-on-rails ruby-on-rails-3

Dillema:

我处于需要跨多个视图和控制器显示用户活动的情况。代码开始闻到一点点,所以我真的很感激清理它的一些指导。

我有一个用户控制器有一个“show”(/ 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)]

1 个答案:

答案 0 :(得分: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