我必须在 ActiveRecord 模型上实现这些方法以支持某些所需的视图功能。
我认为必须有一种更紧凑的方式来声明这些方法,可能涉及符号数组和Array#each
调用。
有什么建议么?
(这些是旧列名,顺便说一句。)
class Contract < ActiveRecord::Base
def start_date_displayed
self.start_date.present? ? self.start_date.strftime("%-m/%-d/%Y") : ''
end
def start_date_displayed=(input)
if input.present?
self.start_date = Date.strptime input, '%m/%d/%Y'
end
end
def end_date_displayed
self.end_date.present? ? self.end_date.strftime("%-m/%-d/%Y") : ''
end
def end_date_displayed=(input)
if input.present?
self.end_date = Date.strptime input, '%m/%d/%Y'
end
end
def ArrivalDate_displayed
self.ArrivalDate.present? ? self.ArrivalDate.strftime("%-m/%-d/%Y") : ''
end
def ArrivalDate_displayed=(input)
if input.present?
self.ArrivalDate = Date.strptime input, '%m/%d/%Y'
end
end
def Contract_ReceivedDate_displayed
self.Contract_ReceivedDate.present? ? self.Contract_ReceivedDate.strftime("%-m/%-d/%Y") : ''
end
def Contract_ReceivedDate_displayed=(input)
if input.present?
self.Contract_ReceivedDate = Date.strptime input, '%m/%d/%Y'
end
end
def Contract_FinalizedDate_displayed
self.Contract_FinalizedDate.present? ? self.Contract_FinalizedDate.strftime("%-m/%-d/%Y") : ''
end
def Contract_FinalizedDate_displayed=(input)
if input.present?
self.Contract_FinalizedDate = Date.strptime input, '%m/%d/%Y'
end
end
def Committee_Date_displayed
self.Committee_Date.present? ? self.Committee_Date.strftime("%-m/%-d/%Y") : ''
end
def Committee_Date_displayed=(input)
if input.present?
self.Committee_Date = Date.strptime input, '%m/%d/%Y'
end
end
# ...
end