class GovUkDateFields::FormDate

Attributes

date[RW]
dd[RW]
mm[RW]
temp[RW]
yyyy[RW]

Public Class Methods

nil_date() click to toggle source
# File lib/gov_uk_date_fields/form_date.rb, line 41
def self.nil_date
  new('', '', '')
end
set_date_part(date_part, object, attr_name, value) click to toggle source
# File lib/gov_uk_date_fields/form_date.rb, line 33
def self.set_date_part(date_part, object, attr_name, value)
  form_date = object.instance_variable_get("@_#{attr_name}".to_sym) || FormDate.nil_date
  form_date.send("#{date_part}=", value)
  form_date.create_date_from_date_parts
  object.__send__("#{attr_name}=", form_date.date)
  object.instance_variable_set("@_#{attr_name}", form_date)
end

Private Class Methods

all_blanks_in_params?(attr_name, params) click to toggle source
# File lib/gov_uk_date_fields/form_date.rb, line 99
def self.all_blanks_in_params?(attr_name, params)
  params["#{attr_name}_dd"].blank? &&  params["#{attr_name}_mm"].blank? &&  params["#{attr_name}_yyyy"].blank?
end
attr_missing_in_params?(attr_name, params) click to toggle source
# File lib/gov_uk_date_fields/form_date.rb, line 95
def self.attr_missing_in_params?(attr_name, params) 
  params["#{attr_name}_dd"].nil? &&  params["#{attr_name}_mm"].nil? &&  params["#{attr_name}_yyyy"].nil?
end
attr_not_present_in_params?(attr_name, params) click to toggle source
# File lib/gov_uk_date_fields/form_date.rb, line 91
def self.attr_not_present_in_params?(attr_name, params)
  attr_missing_in_params?(attr_name, params)  || all_blanks_in_params?(attr_name, params)
end
new(dd, mm, yyyy, date = nil) click to toggle source

This method cannot be called directly: use .new_from_date or .new_from_params

# File lib/gov_uk_date_fields/form_date.rb, line 11
def initialize(dd, mm, yyyy, date = nil)
 @dd            = dd
 @mm            = mm
 @yyyy          = yyyy
 @date          = date
 @valid         = date.nil? ? validate_date : true
end

Public Instance Methods

create_date_from_date_parts() click to toggle source
# File lib/gov_uk_date_fields/form_date.rb, line 46
def create_date_from_date_parts
  @valid = true
  if @dd.blank? && @mm.blank? && @yyyy.blank?
    @date = nil
  else
    months = %w{ jan feb mar apr may jun jul aug sep oct nov dec }
    mm_as_int = months.include?(@mm.downcase) ? months.index(@mm.downcase) + 1 : @mm.to_i
    begin
      @date = Date.new(@yyyy.to_i, mm_as_int, @dd.to_i)
    rescue ArgumentError => err
      raise err unless err.message == 'invalid date'
      @valid = false
    end
  end
end
valid?() click to toggle source

Creates a FormDate object from the params, and writes it to the instance variable @_<attr_name> on the object, and updates the attr with the real date object

If the dob_dd, dob_mm, dob_yy are not in the params hash, or if they are all blank, the object is not updated.

def self.set_from_params(object, attr_name, params)

params = HashWithIndifferentAccess.new(params) unless params.is_a?(HashWithIndifferentAccess)
return if attr_not_present_in_params?(attr_name, params)
form_date = new(params["#{attr_name}_dd"], params["#{attr_name}_mm"], params["#{attr_name}_yyyy"] )
update_object(object, attr_name, form_date)

end

# File lib/gov_uk_date_fields/form_date.rb, line 76
def valid?
  @valid
end

Private Instance Methods

transfer_temp_fields_to_live() click to toggle source
# File lib/gov_uk_date_fields/form_date.rb, line 85
def transfer_temp_fields_to_live
  @dd = @temp[:dd]
  @mm = @temp[:mm]
  @yyyy = @temp[:yyyy]
end
validate_date() click to toggle source
# File lib/gov_uk_date_fields/form_date.rb, line 104
def validate_date
  return true if @dd.blank? && @mm.blank? && @yyyy.blank?
  months = %w{ jan feb mar apr may jun jul aug sep oct nov dec }

  mm_as_int = months.include?(@mm.downcase) ? months.index(@mm.downcase) + 1 : @mm.to_i
  begin
    @date = Date.new(@yyyy.to_i, mm_as_int, @dd.to_i)
  rescue ArgumentError => err
    return false if err.message == 'invalid date'
    raise err
  end
end