[英]How can I make DRY repeated code used in model methods and in controllers in Rails 3?
I have the following methods for a single model, and may have more.对于单个 model,我有以下方法,可能还有更多。 I may also have some of the repeated code in a helper.
我也可能在助手中有一些重复的代码。 How can I make it DRY?
我怎样才能让它干燥?
25 def full_name
26 client = LinkedIn::Client.new(ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'])
27 client.authorize_from_access(self.atoken, self.asecret)
28 client.profile(id => self.uid)
29 client.profile.first_name + " " + client.profile.last_name
30 end
31
32 def image_url
33 client = LinkedIn::Client.new(ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'])
34 client.authorize_from_access(self.atoken, self.asecret)
35 client.profile(id => self.uid)
36 client.profile(:fields => "picture-url").picture_url
37 end
The code where I instantiate the client and access the profile id for the most part is repeated everytime I need to make a method call on the API.每次我需要在 API 上进行方法调用时,都会重复我在大部分情况下实例化客户端并访问配置文件 ID 的代码。 It's just the API that changes.
只是 API 发生了变化。
What happens when I also need to call in the controller (of a different model?)当我还需要调用 controller(不同的 model?)时会发生什么?
29 if @review.save
30 flash[:notice] = "Successfully created review."
31 # check if allowed to post to LinkedIn and post
32 if @review.post_linkedin?
33 client = LinkedIn::Client.new(ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'])
34 client.authorize_from_access(current_user.atoken, current_user.asecret)
35 debugger
36 client.update_network("has shared expertise on <a")
37 end
How could I make it more dry?我怎样才能让它更干燥?
I agree with @thekindofme, but I would add some caching, so you don't have to do a LinkedIn API call every time:我同意@thekindofme,但我会添加一些缓存,因此您不必每次都调用 LinkedIn API:
def linkedin_profile
@linkedin_profile ||= set_linkedin_profile
end
def set_linkedin_profile
client = LinkedIn::Client.new(ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'])
client.authorize_from_access(self.atoken, self.asecret)
client.profile(id => self.uid)
client.profile
end
def full_name
linkedin_profile.first_name + " " + linkedin_profile.last_name
end
def image_url
linkedin_profile(:fields => "picture-url").picture_url
end
General rule is: extract the code that is repeated everywhere put it in a reusable class/method..etc.一般规则是:提取到处重复的代码,将其放入可重用的类/方法..等。
for the above code.对于上面的代码。 you could do something like:
您可以执行以下操作:
def get_client_profile
client = LinkedIn::Client.new(ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'])
client.authorize_from_access(self.atoken, self.asecret)
client.profile(id => self.uid)
client.profile
end
def full_name
p=get_client_profile
p.first_name + " " + p.last_name
end
...etc ...ETC
In this case you can have the get_client_profile in your model(may be in model Client?).在这种情况下,您可以在模型中使用 get_client_profile(可能在 model 客户端中?)。 As 'it belongs their'.
因为“它属于他们”。
I would add the following methods to your user class:我将为您的用户 class 添加以下方法:
class User
def linkedin_client
@linkedin_client || = get_linkedin_client
end
def linkedin_profile
linkedin_client.profile(id => self.uid)
linkedin_client.profile
end
def full_name
linkedin_profile.first_name + " " + user.linkedin_profile.last_name
end
def image_url
linkedin_profile(:fields => "picture-url").picture_url
end
private
def get_linkedin_client
client = LinkedIn::Client.new(ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'])
client.authorize_from_access(atoken, asecret)
client
end
end
and inside your controller you would write:在你的 controller 里面你会写:
if @review.save
flash[:notice] = "Successfully created review."
# check if allowed to post to LinkedIn and post
if @review.post_linkedin?
current_user.linkedin_client.update_network("has shared expertise on <a")
end
end
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.