1

I had to implement these methods on an ActiveRecord model in order to support some desired view functionality.

I'm thinking there has to be a more compact way to declare these methods, probably involving an Array of symbols and an Array#each call.

Any suggestions?

(These are legacy column names, BTW.)


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
1
  • Yossi & xzgyb Thanks. I wish I could accept both answers. I went with Yossi because it's the one I actually implemented. Commented May 3, 2012 at 17:11

2 Answers 2

2
class Contract < ActiveRecord::Base

  %w(start_date end_date).each do |attr_name|
     class_eval <<-METHOD_DEF
         def #{attr_name}_displayed
             self.#{attr_name}.present? ? self.#{attr_name}.strftime("%-m/%-d/%Y") : ''
         end

         def #{attr_name)_displayed=(input)
            if input.present?
              self.#{attr_name} = Date.strptime #{attr_name}, '%m/%d/%Y'
            end
         end


     METHOD_DEF
  end
end

If only define these methods for those columns that name ends with date, then

class Contract < ActiveRecord::Base

   self.column_names.grep(/date$/i).each do |column_name|

     class_eval <<-METHOD_DEF
         def #{column_name}_displayed
             self.#{column_name}.present? ? self.#{column_name}.strftime("%-m/%-d/%Y") : ''
         end

         def #{column_name)_displayed=(input)
            if input.present?
              self.#{column_name} = Date.strptime #{column_name}, '%m/%d/%Y'
            end
         end             
     METHOD_DEF

  end
end
Sign up to request clarification or add additional context in comments.

Comments

1

It's better to avoid plain evals when possible.

class Contract < ActiveRecord::Base
  class << self
    def attr_displayed *attrs
      attrs.each do |attr|
        displayed = :"#{attr}_displayed"

        define_method displayed do
          send(attr).present? ? send(attr).strftime("%-m/%-d/%Y") : ''
        end

        define_method :"#{displayed}=" do |input|
          if input.present?
            send :"#{attr}=", Date.strptime(input, '%m/%d/%Y')
          end
        end
      end
    end
  end

  attr_displayed :start_date, :end_date, :ArrivalDate, :Committee_Date,
                 :Contract_ReceivedDate, :Contract_FinalizedDate
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.