I have a data structure like this in one of my models:
我的一个模型中有这样的数据结构:
def image_size_limits
{
"web" => {"maxheight" => 480, "maxwidth" => 680, "minheight" => 400, "minwidth" => 600},
"phone" => {"height" => 345, "width" => 230, "minheight" => 300, "minwidth" => 200},
"tablet" => {"height" => 680, "width" => 480, "minheight" => 600, "minwidth" => 400},
"other" => {"height" => 680, "width" => 480, "minheight" => 600, "minwidth" => 400}
}
end
I use this in a custom validator I have to validate the sizes of images uploaded. I would like to be able to use this data structure not just in this one model - but everywhere. In all my models, views and controllers.
我在自定义验证器中使用它,我必须验证上传图片的大小。我希望能够不仅在这个模型中,而且在任何地方使用这个数据结构。在我所有的模型、视图和控制器中。
How would I go about doing that, where would I put it?
我该怎么做,把它放在哪里?
2 个解决方案
#1
2
I would use a module.
我会用一个模块。
Stick it in your lib
directory. (You may need to configure Rails 3 to autoload classes and modules from your lib
folder. See this question.)
把它放在你的库目录中。(您可能需要从lib文件夹中配置Rails 3到autoload类和模块。见这个问题。)
# lib/image_sizes.rb
module ImageSizes
def image_size_limits
{
"web" => {"maxheight" => 480, "maxwidth" => 680, "minheight" => 400, "minwidth" => 600},
"phone" => {"height" => 345, "width" => 230, "minheight" => 300, "minwidth" => 200},
"tablet" => {"height" => 680, "width" => 480, "minheight" => 600, "minwidth" => 400},
"other" => {"height" => 680, "width" => 480, "minheight" => 600, "minwidth" => 400}
}
end
end
Then in your models or controllers:
然后在你的模型或控制器中:
class MyModel < ActiveRecord::Base
include ImageSizes
# ...
end
class MyController < ApplicationController
include ImageSizes
# ...
end
Now, each model or controller that includes the ImageSizes
module will have access to the module's methods, namely image_size_limits
.
现在,包括ImageSizes模块的每个模型或控制器都可以访问模块的方法,即image_size_limit。
#2
2
Another option is to create an initializer file and declare a constant there.
另一个选项是创建初始化文件并在其中声明一个常量。
# config/initializers/image_size_limits.rb
IMAGE_SIZE_LIMITS = {
"web" => {"maxheight" => 480, "maxwidth" => 680, "minheight" => 400, "minwidth" => 600},
"phone" => {"height" => 345, "width" => 230, "minheight" => 300, "minwidth" => 200},
"tablet" => {"height" => 680, "width" => 480, "minheight" => 600, "minwidth" => 400},
"other" => {"height" => 680, "width" => 480, "minheight" => 600, "minwidth" => 400}
}
Then in your model or controller, just use IMAGE_SIZE_LIMITS['web']['maxheight']
to get 480
.
然后在您的模型或控制器中,只需使用image_size_limit ['web']['maxheight']为480。
#1
2
I would use a module.
我会用一个模块。
Stick it in your lib
directory. (You may need to configure Rails 3 to autoload classes and modules from your lib
folder. See this question.)
把它放在你的库目录中。(您可能需要从lib文件夹中配置Rails 3到autoload类和模块。见这个问题。)
# lib/image_sizes.rb
module ImageSizes
def image_size_limits
{
"web" => {"maxheight" => 480, "maxwidth" => 680, "minheight" => 400, "minwidth" => 600},
"phone" => {"height" => 345, "width" => 230, "minheight" => 300, "minwidth" => 200},
"tablet" => {"height" => 680, "width" => 480, "minheight" => 600, "minwidth" => 400},
"other" => {"height" => 680, "width" => 480, "minheight" => 600, "minwidth" => 400}
}
end
end
Then in your models or controllers:
然后在你的模型或控制器中:
class MyModel < ActiveRecord::Base
include ImageSizes
# ...
end
class MyController < ApplicationController
include ImageSizes
# ...
end
Now, each model or controller that includes the ImageSizes
module will have access to the module's methods, namely image_size_limits
.
现在,包括ImageSizes模块的每个模型或控制器都可以访问模块的方法,即image_size_limit。
#2
2
Another option is to create an initializer file and declare a constant there.
另一个选项是创建初始化文件并在其中声明一个常量。
# config/initializers/image_size_limits.rb
IMAGE_SIZE_LIMITS = {
"web" => {"maxheight" => 480, "maxwidth" => 680, "minheight" => 400, "minwidth" => 600},
"phone" => {"height" => 345, "width" => 230, "minheight" => 300, "minwidth" => 200},
"tablet" => {"height" => 680, "width" => 480, "minheight" => 600, "minwidth" => 400},
"other" => {"height" => 680, "width" => 480, "minheight" => 600, "minwidth" => 400}
}
Then in your model or controller, just use IMAGE_SIZE_LIMITS['web']['maxheight']
to get 480
.
然后在您的模型或控制器中,只需使用image_size_limit ['web']['maxheight']为480。