简体   繁体   中英

Ruby on Rails: DRYing a repeated code block that initializes a few variables

I've got a repeated code block that initializes a few variables in a bunch of different controller methods. Is there a way for me to make this DRY with a model method as opposed to repeating the same code block in each controller method?

Basically, it's for a social site, and it's pulling up a user's list of friends, and then building buckets of friends based on the permissions the user has which are stored in a friendship model. This repeated initialization of buckets is what I'm trying to make DRY.

Normally I would use a model method, but in this case, 3 separate variables are being initialized based on one database pull, and this is called often enough I don't want to make it unnecessarily inefficient by hitting the database 3 times. In C, I would just use pointers passed in as variables.

It goes something like this:

def example_method
  friendships = @user.friendships
  view_permission_friends = []
  write_permission_friends = []
  message_permission_friends = []
  for friendship in friendships
    if friendship.view_permission then view_permission_friends << friendship.friend_id end
    if friendship.write_permission then write_permission_friends << friendship.friend_id end
    if friendship.message_permission then message_permission_friends << friendship.friend_id end
  end
  #Do something with the 3 initialized arrays here
end

I thought about it for a bit, and I think this code should go into your User model. (Or whatever class @user is in your example above.) Two reasons:

  1. It's very specific to the User, its relationships, and most importantly, how they're stored in and retrieved from the database.
  2. You only need to write the code once: in the User model.

The quick and easy way which relies on Rails' internal query caching would look like this. Added into the User model:

def view_permission_friends
  return friendships.select{|f| f.view_permission}
end

(etc.)

Your controllers then simply do this:

@viewers = @user.view_permission_friends
(etc.)

(Note - there's more room for optimization and better flexibility here via lazy caching and parameterizing the permission.)

How about...

rv = {}
%w(view write message).each do |type|
  rv["#{type}_permission_friends"] = @user.friendships.select{|f|f.send(:"#{type}_permission")}.collect(&:friend_id)
end

This gives you a hash with keys instead of individual arrays, but should suffice.

Use Enumerable#inject , Enumerable#find_all and Object#instance_variable_set :

def example_method
  %w{view write message}.inject({}) do |memo, s|
    friendships = @user.friendships.find_all{ |f| f.send("#{s}_permission") }
    memo["#{s}_permission_friends"] = friendships
  end.each do |key, value|
    instance_variable_set "@#{key}", value
  end

  # now you have 3 arrays:
  # @view_permission_friends, @write_permission_friends and @message_permission_friends
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM