Ruby (Rails) 将属性委托给另一个模型的方法?
-编辑-
在阅读第一个答案中的委托方法后,我的问题是,是否可以将两个不同的方法委托给另一个单个方法。
IE:我目前有:@photo.attachment.file.url、@photo.attachment.height 和 @photo.attachment.width
我希望能够通过 @photo.file.url 访问所有这些, @照片.文件.高度,@照片.文件.宽度。
语法的原因是 Attachment 是一个使用 Paperclip 管理文件的模型,Paperclip 正在生成 .file 方法(该模型称为 Attachment,该模型使用 Paperclip 的 has_attached_file :file
)。
-原始问题-
我想知道 Ruby 中的别名方法和属性(我认为这是一个常见的 Ruby 问题,尽管我的应用程序是在 Rails 3 中):
我有两个模型:Photo has_one Attachment。
附件具有“高度”和“宽度”属性,以及“文件”方法(来自 Paperclip)。
因此,默认情况下,我可以像这样访问附件模型的位:
photo.attachment.width # returns width in px
photo.attachment.height # returns height in px
photo.attachment.file # returns file path
photo.attachment.file.url #returns url for the default style variant of the image
photo.attachment.file.url(:style) #returns the url for a given style variant of the image
现在,在我的照片类中,我创建了这个方法:
def file(*args)
attachment.file(*args)
end
所以,现在我可以简单地使用:
photo.file # returns file path
photo.file.url # returns file url (or variant url if you pass a style symbol)
我的问题是,我能够直接 photo.attachment。 file
只是 photo.file
,但我是否也可以将高度和宽度映射到 photo.file
,这样,为了保持一致性,我可以访问通过 photo.file.height
和 photo.file.width
获取高度和宽度属性?
这样的事情可能吗?如果可能的话,它是什么样子的?
-EDIT-
After reading about the Delegate method from the first answer, my question is this, is it possible to delegate two different methods to another single method.
IE: I currently have: @photo.attachment.file.url, and @photo.attachment.height, and @photo.attachment.width
I'd like to be able to access all of these via @photo.file.url, @photo.file.height, @photo.file.width.
The reason for the syntax is Attachment is a model that uses Paperclip to manage files, Paperclip is generating the .file method (the model is called Attachment, the model uses Paperclip's has_attached_file :file
).
-ORIGINAL QUESTION-
I was wondering about aliasing methods and attributes in Ruby (I think this is a general ruby question, although my application is in Rails 3):
I have two models: Photo has_one Attachment.
Attachment has "height" and "width" attributes, and a "file" method (from Paperclip).
So by default I can access bits of the Attachment model like so:
photo.attachment.width # returns width in px
photo.attachment.height # returns height in px
photo.attachment.file # returns file path
photo.attachment.file.url #returns url for the default style variant of the image
photo.attachment.file.url(:style) #returns the url for a given style variant of the image
Now, in my photo class I have created this method:
def file(*args)
attachment.file(*args)
end
So, now I can simply use:
photo.file # returns file path
photo.file.url # returns file url (or variant url if you pass a style symbol)
My question is, I was able to direct photo.attachment.file
to just photo.file
, but can I also map height and width to photo.file
, so that, for the sake of consistency, I could access the height and width attributes through photo.file.height
and photo.file.width
?
Is such a thing possible, and if so what does it look like?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
所以你要问的是,
你无法用委托来解决这个问题,因为你希望该文件根据接下来的内容来表示不同的东西。要实现此目的,您需要重新打开回形针,我不建议这样做(因为我相信 api 本身就很好)。
我能想到解决这个问题的唯一方法是添加消除
file
级别。像这样:然后您可以通过对每个所需方法使用
委托
来解决这个问题。所以你写
希望这有帮助。
So what you are asking is that
You can't solve this with delegates, because you want that
file
to mean different things based on what follows next. To achieve this you would need to reopen paperclip, which i would not recommend (because i believe the api is good the way it is).The only way i can think of to solve this, is to add eliminate the
file
level too. Like so:This you could then solve by using a
delegate
for each of the wanted methods.So you write
Hope this helps.
您可以使用 Rails 的“委托”方法。看看我对这个问题的回答:
执行此命令的更类似于 Ruby 的方式是什么?
You can use Rails 'delegate' method. Have a look at my answer for this question:
What is a more Ruby-like way of doing this command?
我想到的最简单的方法是将附件中的 url 方法委托给文件:
这样您就可以调用 photo.attachment.url、photo.attachment.width、photo.attachment.height,这对我来说似乎非常一致。您可以选择为文件添加别名附件 - 这样您就可以获得您要求的确切方法名称(photo.file.width、photo.file.url),但我不建议这样做,因为它看起来很混乱(调用附件) “文件”)。
The simplest way that comes to mind is to delegate url method in attachment to file:
This way you can call photo.attachment.url, photo.attachment.width, photo.attachment.height, which for me seems pretty consistent. You could optionally alias attachment to file - this way you'd get the exact method names you asked for (photo.file.width, photo.file.url), but I would not recommend that, because it seems confusing (calling an attachment "file").
对于普通 Ruby,您可以使用 Forwardable:
With plain Ruby you can use Forwardable: