如何搜索一长串属性并将其与 fontawesome 图标匹配?
How do I search through a long list of attributes and match it to fontawesome icon?
我有一个 PropertyFeatureOption
模型,其数据如下所示:
+-----+-------------------------+--------+----------+---------+-------------------------+-------------------------+
| id | name | shared | exterior | amenity | created_at | updated_at |
+-----+-------------------------+--------+----------+---------+-------------------------+-------------------------+
| 101 | Dining Room | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 102 | Security | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 103 | Common Area | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 104 | Swimming Pool | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 105 | Automatic Gate | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 106 | Panic Button | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 107 | Car Port | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 108 | Electric Generator | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 109 | Water Pump | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 110 | Air Conditioning | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 111 | Cable | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 112 | Kitchen | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 113 | Security Guard | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 114 | Family Room | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 115 | Other Room | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 116 | Patio | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 117 | Electronic Security | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 118 | Garage | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 119 | Dish | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 120 | Furnished | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 121 | Living Room | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 122 | Canteen | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 123 | Telephone Lines | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 124 | Restroom | false | false | false | 2014-01-30 00:07:38 UTC | 2014-01-30 00:07:38 UTC |
| 125 | Powder Room | false | false | false | 2014-01-30 00:07:38 UTC | 2014-01-30 00:07:38 UTC |
| 126 | Den/Study | false | false | false | 2014-01-30 00:07:38 UTC | 2014-01-30 00:07:38 UTC |
| 127 | Water Tank | false | false | false | 2014-01-30 00:07:38 UTC | 2014-01-30 00:07:38 UTC |
| 128 | Partitions | false | false | false | 2014-01-30 00:07:39 UTC | 2014-01-30 00:07:39 UTC |
| 129 | Fully Fenced | false | false | false | 2014-02-27 06:31:34 UTC | 2014-02-27 06:31:34 UTC |
| 130 | Garden Area | false | false | false | 2014-02-27 06:31:34 UTC | 2014-02-27 06:31:34 UTC |
| 131 | Ceiling Fans | false | false | false | 2014-02-27 06:31:34 UTC | 2014-02-27 06:31:34 UTC |
| 132 | Grilled | false | false | false | 2014-02-27 06:31:34 UTC | 2014-02-27 06:31:34 UTC |
| 133 | Kitchen Built-in(s) | false | false | false | 2014-02-27 06:31:34 UTC | 2014-02-27 06:31:34 UTC |
| 134 | Main Level Entry | false | false | false | 2014-02-27 06:31:34 UTC | 2014-02-27 06:31:34 UTC |
| 135 | Unfurnished | false | false | false | 2014-02-27 06:31:34 UTC | 2014-02-27 06:31:34 UTC |
| 136 | Barn | false | false | false | 2014-02-27 06:31:34 UTC | 2014-02-27 06:31:34 UTC |
共有117条记录。
我想做的是在 Property#Show
页面上,这取决于我正在查看的 property
对象上是否存在这些内容,我想显示超棒的字体与这些条目相关的图标。
所以,基本上我需要在这些记录的不同名称和各种超棒的字体图标之间对 link 进行硬编码。
例如,如果 property
有一个 property_feature_option.name.eql? "Car Port"
(即上面 table 中的 ID=107
),我想显示 fa fa-car
。可以有很多 property_feature_options
到超赞字体图标名称的映射。
例如Panic Button, Security Guard & Electronic Security
可以转到 fa fa-bullhorn
(仅举个例子)。
我该如何处理?不太确定从哪里开始。
我可能会做这样的事情,我定义了一个纯粹的 Ruby class 只是为了展示,但你可以调整 Rails.
class PropertyFeatureOption
attr_reader :name
ICON_MAP = { "fa fa-car" => ["Car Port"], "fa fa-bullhorn" => ["Panic Button", "Security Guard", "Electronic Security"] }.freeze
def initialize(name)
@name = name
end
def icon
icon = ICON_MAP.select { |k, v| v.include? name }.keys.first || "fa fa-whathever"
icon.to_s
end
end
property_feature_option = PropertyFeatureOption.new("Panic Button")
property_feature_option.icon #=> "fa fa-bullhorn"
property_feature_option = PropertyFeatureOption.new("Not in list")
property_feature_option.icon #=> "fa fa-whathever"
一个选项是@iGian 所说的,将它们放在哈希中。为了保持整洁,你应该把它们放在一个单独的命名空间中:
module FeatureIconizer
module_function
ICONS = {
'Car Port' => 'fa-car',
'Panic Button' => 'fa-bullhorn',
'Security Guard' => 'fa-bullhorn',
...
}
DEFAULT = 'fa-house'
def icon_class(feature)
'fa ' + ICONS.fetch(feature.name, DEFAULT)
end
end
另一个选项,因为您已经在数据库中定义了您的功能,所以也可以在数据库中添加图标。这样你不需要部署来改变一个图标。
我有一个 PropertyFeatureOption
模型,其数据如下所示:
+-----+-------------------------+--------+----------+---------+-------------------------+-------------------------+
| id | name | shared | exterior | amenity | created_at | updated_at |
+-----+-------------------------+--------+----------+---------+-------------------------+-------------------------+
| 101 | Dining Room | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 102 | Security | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 103 | Common Area | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 104 | Swimming Pool | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 105 | Automatic Gate | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 106 | Panic Button | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 107 | Car Port | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 108 | Electric Generator | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 109 | Water Pump | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 110 | Air Conditioning | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 111 | Cable | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 112 | Kitchen | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 113 | Security Guard | false | false | false | 2014-01-30 00:07:36 UTC | 2014-01-30 00:07:36 UTC |
| 114 | Family Room | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 115 | Other Room | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 116 | Patio | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 117 | Electronic Security | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 118 | Garage | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 119 | Dish | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 120 | Furnished | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 121 | Living Room | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 122 | Canteen | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 123 | Telephone Lines | false | false | false | 2014-01-30 00:07:37 UTC | 2014-01-30 00:07:37 UTC |
| 124 | Restroom | false | false | false | 2014-01-30 00:07:38 UTC | 2014-01-30 00:07:38 UTC |
| 125 | Powder Room | false | false | false | 2014-01-30 00:07:38 UTC | 2014-01-30 00:07:38 UTC |
| 126 | Den/Study | false | false | false | 2014-01-30 00:07:38 UTC | 2014-01-30 00:07:38 UTC |
| 127 | Water Tank | false | false | false | 2014-01-30 00:07:38 UTC | 2014-01-30 00:07:38 UTC |
| 128 | Partitions | false | false | false | 2014-01-30 00:07:39 UTC | 2014-01-30 00:07:39 UTC |
| 129 | Fully Fenced | false | false | false | 2014-02-27 06:31:34 UTC | 2014-02-27 06:31:34 UTC |
| 130 | Garden Area | false | false | false | 2014-02-27 06:31:34 UTC | 2014-02-27 06:31:34 UTC |
| 131 | Ceiling Fans | false | false | false | 2014-02-27 06:31:34 UTC | 2014-02-27 06:31:34 UTC |
| 132 | Grilled | false | false | false | 2014-02-27 06:31:34 UTC | 2014-02-27 06:31:34 UTC |
| 133 | Kitchen Built-in(s) | false | false | false | 2014-02-27 06:31:34 UTC | 2014-02-27 06:31:34 UTC |
| 134 | Main Level Entry | false | false | false | 2014-02-27 06:31:34 UTC | 2014-02-27 06:31:34 UTC |
| 135 | Unfurnished | false | false | false | 2014-02-27 06:31:34 UTC | 2014-02-27 06:31:34 UTC |
| 136 | Barn | false | false | false | 2014-02-27 06:31:34 UTC | 2014-02-27 06:31:34 UTC |
共有117条记录。
我想做的是在 Property#Show
页面上,这取决于我正在查看的 property
对象上是否存在这些内容,我想显示超棒的字体与这些条目相关的图标。
所以,基本上我需要在这些记录的不同名称和各种超棒的字体图标之间对 link 进行硬编码。
例如,如果 property
有一个 property_feature_option.name.eql? "Car Port"
(即上面 table 中的 ID=107
),我想显示 fa fa-car
。可以有很多 property_feature_options
到超赞字体图标名称的映射。
例如Panic Button, Security Guard & Electronic Security
可以转到 fa fa-bullhorn
(仅举个例子)。
我该如何处理?不太确定从哪里开始。
我可能会做这样的事情,我定义了一个纯粹的 Ruby class 只是为了展示,但你可以调整 Rails.
class PropertyFeatureOption
attr_reader :name
ICON_MAP = { "fa fa-car" => ["Car Port"], "fa fa-bullhorn" => ["Panic Button", "Security Guard", "Electronic Security"] }.freeze
def initialize(name)
@name = name
end
def icon
icon = ICON_MAP.select { |k, v| v.include? name }.keys.first || "fa fa-whathever"
icon.to_s
end
end
property_feature_option = PropertyFeatureOption.new("Panic Button")
property_feature_option.icon #=> "fa fa-bullhorn"
property_feature_option = PropertyFeatureOption.new("Not in list")
property_feature_option.icon #=> "fa fa-whathever"
一个选项是@iGian 所说的,将它们放在哈希中。为了保持整洁,你应该把它们放在一个单独的命名空间中:
module FeatureIconizer
module_function
ICONS = {
'Car Port' => 'fa-car',
'Panic Button' => 'fa-bullhorn',
'Security Guard' => 'fa-bullhorn',
...
}
DEFAULT = 'fa-house'
def icon_class(feature)
'fa ' + ICONS.fetch(feature.name, DEFAULT)
end
end
另一个选项,因为您已经在数据库中定义了您的功能,所以也可以在数据库中添加图标。这样你不需要部署来改变一个图标。